Email macro compile error

picklefactory

Well-known Member
Joined
Jan 28, 2005
Messages
508
Office Version
  1. 365
Platform
  1. 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

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
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi picklefactory,

You may need to have "Microsoft Outlook 16.0 Object Library" selected in Tools, References.

Hope that helps,

Doug
 
Upvote 1
Solution
Hi picklefactory,

You may need to have "Microsoft Outlook 16.0 Object Library" selected in Tools, References.

Hope that helps,

Doug
Absolutely spot on Doug, thank you for quick response. I just went back to check after posting and I had activated and somehow unticked it again. It works now.
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top