My code is not finding an exported file to attach to an email.
I'm sure I'm doing something wrong in the myattachments.add line.
The file name and file directory will always be different based on each client, that's why I have specified a cell specifically for the filename in each quote.
I'm going to copy this Excel file to each client folder and run the code from there.
Sub sendremindermail()
ChDir ThisWorkbook.Path & "\"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Range("'Costing'!C1"), Openafterpublish:=True
Dim outlookapp As Object
Dim outlookmailitem As Object
Dim myattachments As Object
Set outlookapp = CreateObject("outlook.application")
Set outlookmailitem = outlookapp.createitem(0)
Set myattachments = outlookmailitem.Attachments
With outlookmailitem
.To = Range("'Costing'!C8")
myattachments.Add ThisWorkbook.Path & Range("'Costing'!C1") & ".pdf" ' it cant find the pdf in the same directory
'.send
.Display
End With
Set outlookmailitem = Nothing
Set outlookapp = Nothing
End Sub
I'm new to VBA for Excel.
You could refer to the below code:
Sub Mail_Workbook_1()
' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, Outlook 2010.
' This example sends the last saved version of the Activeworkbook object .
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
' Change the mail address and subject in the macro before you run it.
With OutMail
.To = "ron@debruin.nl"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = "Hello World!"
.Attachments.Add ActiveWorkbook.FullName
' You can add other files by uncommenting the following line.
'.Attachments.Add ("C:\test.txt")
' In place of the following statement, you can use ".Display" to
' display the mail.
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
For more information, Please refer to this link:
I suggest Outlook VBA does not know Excel VBA's Range.
You could pass the info from Range like this:
Sub sendremindermail()
Dim fName As String
Dim pathFileName As String
ChDir ThisWorkbook.Path & "\"
fName = Range("'Costing'!C8")
'ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Range("'Costing'!C1"), Openafterpublish:=True
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=fName ', Openafterpublish:=True
Dim outlookapp As Object
Dim outlookmailitem As Object
Dim myattachments As Object
Set outlookapp = CreateObject("outlook.application")
Set outlookmailitem = outlookapp.createitem(0)
Set myattachments = outlookmailitem.Attachments
With outlookmailitem
.To = fName
pathFileName = ThisWorkbook.Path & "\" & fName & ".pdf"
Debug.Print "With fName not Range: " & pathFileName
myattachments.Add pathFileName
'.send
.Display
End With
Set outlookmailitem = Nothing
Set outlookapp = Nothing
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.