countryfan_nt
Well-known Member
- Joined
- May 19, 2004
- Messages
- 765
Hello friends, hope all is well!
Please help having a bug, I am trying to send a sheet as a pdf via vba.
The bug occurs on line: .Attachments.Add mypath & "\" & PdfFile & ".pdf"
I think the problem is with the path, but I don't know what and how to edit it
thank you so much in advance!
Please help having a bug, I am trying to send a sheet as a pdf via vba.
The bug occurs on line: .Attachments.Add mypath & "\" & PdfFile & ".pdf"
I think the problem is with the path, but I don't know what and how to edit it
thank you so much in advance!
VBA Code:
Sub EMAIL()
Set OutApp = GetObject(, "Outlook.Application")
Dim IsCreated As Boolean
Dim i As Long
Dim ab, ac, ad, emTo, emCC As String
Dim PdfFile As String, Title As String
Dim OutlApp As Object
mypath = ActiveWorkbook.Path
Application.ScreenUpdating = False
emTo = Worksheets("Inputs").Range("T5").Value
emCC = Worksheets("Inputs").Range("T7").Value
TheTitle = Worksheets("Inputs").Range("AE2").Value
Set xSht = ThisWorkbook.Sheets("Payslip")
' Not sure for what the Title is
Title = TheTitle
TitleF = TheTitle
' Title & " - " & ab
' Define PDF filename
'PdfFile = ActiveWorkbook.FullName
i = InStrRev(PdfFile, ".")
If i > 1 Then PdfFile = Left(PdfFile, i - 1)
PdfFile = TheTitle & ".pdf"
' PdfFile
' Export activesheet as PDF
xSht.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=PdfFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
' Use already open Outlook if possible
On Error Resume Next
Set OutlApp = GetObject(, "Outlook.Application")
If Err Then
Set OutlApp = CreateObject("Outlook.Application")
IsCreated = True
End If
OutlApp.Visible = True
On Error GoTo 0
' Prepare e-mail with PDF attachment
With OutlApp.CreateItem(0)
' Prepare e-mail
.Subject = TitleF
.To = emTo ' <-- Put email of the recipient here
.CC = emCC ' <-- Put email of 'copy to' recipient here
.HTMLBody = "Greetings " & Range("T1").Value & ", " & vbLf _
& " Please find attached the medical Gas dashboard for your information and action if needed." & vbCrLf _
& "<p>" _
& "<p><p>Thank you and Best Regards,<br>" & vbLf _
& "<b>Payroll Department - Human Resources Group</b> <br>" & vbLf _
& " Phone: 7333 <br>" & vbLf _
& " Email: HR-Dept@sdl.com.sa"
.Attachments.Add mypath & "\" & PdfFile & ".pdf"
' Try to send
On Error Resume Next
Set .SendUsingAccount = OutlApp.Session.Accounts.Item(2) 'Use 2nd Account in the list
.send
Application.Wait (Now + TimeValue("0:00:01"))
Application.SendKeys "%s"
Application.Visible = True
On Error GoTo 0
End With
' Delete PDF file
Kill PdfFile
' Quit Outlook if it was created by this code
If IsCreated Then OutlApp.Quit
' Release the memory of object variable
Set OutlApp = Nothing
Application.ScreenUpdating = True
End Sub