Trying to attach pdf to an email with VBA

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
563
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
On my spreadsheet I have a invoice template with an activex button. When that button is clicked it converts spreadsheet to a pdf and saves it to a designated location and attaches it to an email to be sent. Everything works except the attaching it to an email. My laptop has Office 365 built into it but the trial expired on it, so I installed my older version of Office 2010 since I already had the product key for it.

I created a module called Public_Variables and in that module I have a variable called
Code:
Public sPathSaved As String
Below is the rest of the code used.
VBA Code:
Private Sub btnSave_Email_Click()
    Dim sPath As String
    Dim sFilename As String
    
    'sPath = "C:\Users\username\Desktop\Saved PDF TEST\"
    sPath = "C:\Users\username\Desktop\Saved PDF Test\"
    sFilename = Range("A7").Text
    ActiveSheet.ExportAsFixedFormat xlTypePDF, sPath & sFilename
    sPathSaved = sPath & sFilename & ".pdf"
    Call send_Email
    
End Sub
I'm getting the error of Run-Time Error '287: Application-defined or object-defined error from the below sub on this line
Code:
    Set OutMail = OutApp.CreateItem(olMailItem)
Code:
Sub send_Email()

    Dim OutApp As Outlook.Application
    Dim OutMail As Outlook.MailItem
    
    Set OutApp = New Outlook.Application
    Set OutMail = OutApp.CreateItem(olMailItem)

        With OutMail
            .attachments.Add "C:\Users\Jacob B Cutler\Desktop\Saved PDF TEST\"
        End With
    
End Sub

Thank You
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
I forgot to also mention that I checked the box marked Microsoft Outlook 16.0 Object Library, under Tools--->References
 
Upvote 0
VBA Code:
Option Explicit

Sub sendReminderMail()

Dim OutLookApp As Object
Dim OutLookMailItem As Object
Dim myAttachments As Object

Set OutLookApp = CreateObject("Outlook.application")
Set OutLookMailItem = OutLookApp.CreateItem(0)


    With OutLookMailItem
        .To = ""
        .Subject = ""
        .Body = ""
        .Attachments.Add "C:\Users\Jacob B Cutler\Desktop\Saved PDF TEST\NameOfPDFfileHere.pdf"
        '.send
        .Display
    End With

Set OutLookMailItem = Nothing
Set OutLookApp = Nothing

End Sub
 
Upvote 0
I getting an RunTime error 287 Application-defined or object defined error on the following line:
VBA Code:
Set OutLookMailItem = OutLookApp.CreateItem(0)
Here is the rest of the code:
Code:
Sub sendReminderMail()

Dim OutLookApp As Object
Dim OutLookMailItem As Object
Dim myAttachments As Object

Set OutLookApp = CreateObject("Outlook.application")
Set OutLookMailItem = OutLookApp.CreateItem(0)


    With OutLookMailItem
        .To = ""
        .Subject = ""
        .Body = ""
        .Attachments.Add "C:\Users\Jacob B Cutler\Desktop\Saved PDF TEST\NameOfPDFfileHere.pdf"
        '.send
        .Display
    End With

Set OutLookMailItem = Nothing
Set OutLookApp = Nothing

End Sub
I'm not sure if this matters but I checked the box under
Tools--->References Microsoft Office 16.0 Object Library

Thanks
 
Upvote 0
vbaSnip2020-07-20 145331.png

Here is what is checked
 

Attachments

  • vbaSnip2020-07-20 145331.png
    vbaSnip2020-07-20 145331.png
    15 KB · Views: 3
Upvote 0
I'm at a loss. Everything I web searched shows the similar setup or the same setup from what you sent me. Thank You
 
Upvote 0
With that code you shouldn't need any references set.
Try removing the reference to outlook.
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,332
Members
449,077
Latest member
jmsotelo

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