picklefactory
Well-known Member
- Joined
- Jan 28, 2005
- Messages
- 508
- Office Version
- 365
- Platform
- Windows
Hi folks
I've found some code on Mr Excel (Courtesy of Sunjinsak back in 2020) which I think will do what I need. My problem is I get a compile error (User defined type not defined) on oApp As Outlook.Application
I'm guessing that as it was created in 2020 and I'm on 365 it maybe a version issue????
Could anyone advise please?
Thanks
I've found some code on Mr Excel (Courtesy of Sunjinsak back in 2020) which I think will do what I need. My problem is I get a compile error (User defined type not defined) on oApp As Outlook.Application
I'm guessing that as it was created in 2020 and I'm on 365 it maybe a version issue????
Could anyone advise please?
Thanks
VBA Code:
Sub SavePdfAndSendEmail()
On Error GoTo err_handler
Dim oApp As Outlook.Application
Dim oMail As Outlook.MailItem
Dim folderPath As String
Dim pdfFileName As String
'// Construct the file path and name
folderPath = "X:\Javelin.2018r1\Documents\HOLIDAY BOOKING\"
pdfFileName = Sheets("Sheet1").Range("C1").Value & ".pdf"
'// Export the workbook as PDF
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=folderPath & pdfFileName
'// Open Outlook and create a new email
Set oApp = New Outlook.Application
Set oMail = oApp.CreateItem(olMailItem)
With oMail
.To = "another@address.co.uk"
'.CC = "example@myemail.com"
.Subject = "Holiday booking" & pdfFileName
.body = "This is a test..." 'This is the text that will appear in the body of the email. Remove it if not needed.
.Attachments.Add Source:=folderPath & pdfFileName, Type:=xlTypePDF
.Send 'This will display the email so you can review it before sending. If you want to send it automatically replace .Display with .Send
End With
clean_exit:
Set oMail = Nothing
Set oApp = Nothing
Exit Sub
err_handler:
'Something has gone wrong, spit out an error messsage
MsgBox Err.Number & ": " & Err.Description, vbCritical, "Error"
GoTo clean_exit
End Sub