Runtime error: '91 Object variable or with block not set

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
626
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
I am trying to attach a converted pdf to an email but I get the error in the title. Here's my code:
Code:
Private Sub cmdbtnPDF_Save_Click()
    Dim sPath As String
    Dim sFilename As String
    Dim wbAnswer, EmailAnswer As Integer
    sPath = "C:\Users\username\Desktop\Saved PDF Test\"
    sFilename = Range("VendorName").Text & "_" & Format(Now, "mm-dd-yyyy_hhmm") & ".pdf"
    ActiveSheet.ExportAsFixedFormat xlTypePDF, sPath & sFilename
    ActiveWorkbook.Save
   
    EmailAnswer = MsgBox("Do you want to e-mail this pdf?", vbYesNo + vbDefaultButton1 + vbQuestion, "Email: " & sFilename)
    If EmailAnswer = vbYes Then
        Dim olApp As Outlook.Application
        Dim olMailItem As Outlook.MailItem
        Dim olAttachment As Outlook.Attachment
       
        Set olApp = CreateObject("Outlook.Application")
        Set olMailItem = olApp.CreateItem(olMailItem)
        Set olAttachment = olMailItem.Attachments
       
        With olMailItem
            .To = ""
            .CC = ""
            .Subject = ""
            .Body = ""
            olAttachment.Add sPath & sFilename
            .Display
        End With
        Set olMailItem = Nothing
        Set olApp = Nothing
    Else
        Exit Sub
    End If
    wbAnswer = MsgBox("Are you sure you want to close this workbook and Excel?", vbYesNo + vbDefaultButton1 + vbQuestion, "WORKBOOK: " & ActiveWorkbook.Name)
    If wbAnswer = vbYes Then
        ActiveWorkbook.Save
        Application.Quit
    Else
        Exit Sub
    End If
   
End Sub

I was wondering what I might of missed? The error occurs on this line:
VBA Code:
Set olMailItem = olApp.CreateItem(olMailItem)
I also tried:
Code:
Dim olApp As Object
Dim olMailItem As Object
Dim olAttachment As Object

When I change the current declaration to "As Object" I get a runtime error '13 Type mismatch. Thank you all who contribute to the solution.
 
Last edited:

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Change this line,
VBA Code:
     olAttachment.Add sPath & sFilename
to this.
Code:
    .Attachments.Add sPath & sFilename
 
Upvote 0
I followed your directions but I am still getting that runtime error. I even commented out these two lines of code:
VBA Code:
'Dim olAttachment As Outlook.Attachment
'Set olAttachment = olMailItem.Attachments
Thank you for the response.
 
Upvote 0
Can you post the code as it now is?
 
Upvote 0
With this current version I now receive a "runtime error 438: Object doesn't support this property or method". Nothing is highlighted. Thank You

VBA Code:
Private Sub cmdbtnPDF_Save_Click()
    Dim sPath As String
    Dim sFileName As String
    Dim wbAnswer, EmailAnswer As Integer
    sPath = "C:\Users\username\Desktop\Saved PDF Test\"
    
    sFileName = Range("VendorName").Text & "_" & Format(Now, "mm-dd-yyyy_hhmm") & ".pdf"
    ActiveSheet.ExportAsFixedFormat xlTypePDF, sPath & sFileName
    ActiveWorkbook.Save
    
    EmailAnswer = MsgBox("Do you want to e-mail this pdf?", vbYesNo + vbDefaultButton1 + vbQuestion, "Email: " & sFileName)
    If EmailAnswer = vbYes Then
        Dim olApp As Outlook.Application
        Dim olMailItem As Outlook.MailItem
        'Dim olAttachment As Outlook.Attachment
        
        Set olApp = Application.CreateItem(olMailItem)
        Set olMailItem = Application.CreateItem(olMailItem)
        'Set olAttachment = olMailItem.Attachments
              
        With olMailItem
            .To = ""
            .CC = ""
            .Subject = ""
            .Body = "Please allow me to purchase this."
            .Attachments.Add sPath & sFileName
            .Display
        End With
        Set olMailItem = Nothing
        Set olApp = Nothing
    Else
        Exit Sub
    End If
    wbAnswer = MsgBox("Are you sure you want to close this workbook and Excel?", vbYesNo + vbDefaultButton1 + vbQuestion, "WORKBOOK: " & ActiveWorkbook.Name)
    If wbAnswer = vbYes Then
        ActiveWorkbook.Save
        Application.Quit
    Else
        Exit Sub
    End If
    
End Sub
 
Upvote 0
Try this.
VBA Code:
Option Explicit

Private Sub cmdbtnPDF_Save_Click()
Dim olApp As Outlook.Application
Dim olMailItem As Outlook.MailItem
Dim sPath As String
Dim sFileName As String
Dim wbAnswer, EmailAnswer As Integer

    sPath = "C:\Users\username\Desktop\Saved PDF Test\"

    sFileName = Range("VendorName").Text & "_" & Format(Now, "mm-dd-yyyy_hhmm") & ".pdf"
    ActiveSheet.ExportAsFixedFormat xlTypePDF, sPath & sFileName
    ActiveWorkbook.Save

    EmailAnswer = MsgBox("Do you want to e-mail this pdf?", vbYesNo + vbDefaultButton1 + vbQuestion, "Email: " & sFileName)

    If EmailAnswer = vbYes Then

        Set olApp = olApp.CreateItem(olMailItem)
        Set olMailItem = olApp.CreateItem(olMailItem)

        With olMailItem
            .To = ""
            .CC = ""
            .Subject = ""
            .Body = "Please allow me to purchase this."
            .Attachments.Add sPath & sFileName
            .Display
        End With
        Set olMailItem = Nothing
        Set olApp = Nothing
    Else
        Exit Sub
    End If

    wbAnswer = MsgBox("Are you sure you want to close this workbook and Excel?", vbYesNo + vbDefaultButton1 + vbQuestion, "WORKBOOK: " & ActiveWorkbook.Name)
    If wbAnswer = vbYes Then
        ActiveWorkbook.Save
        Application.Quit
    Else
        Exit Sub
    End If

End Sub
 
Upvote 0
I copied your code and reran it and now I am getting the error in the title Runtime error 91
 
Upvote 0
Change olMailItem to something else, olMailItem is an Outlook VBA constant and you should use its name for a variable name,
 
Upvote 0
Do you mean like this? If so, I am still getting the same error message. Thank you for your help.
VBA Code:
Private Sub cmdbtnPDF_Save_Click()
    Dim olApp As Outlook.Application
    Dim MailItem As Outlook.MailItem
    Dim sPath As String
    Dim sFileName As String
    Dim wbAnswer, EmailAnswer As Integer

    sPath = "C:\Users\username\Desktop\Saved PDF Test\"

    sFileName = Range("VendorName").Text & "_" & Format(Now, "mm-dd-yyyy_hhmm") & ".pdf"
    ActiveSheet.ExportAsFixedFormat xlTypePDF, sPath & sFileName
    ActiveWorkbook.Save

    EmailAnswer = MsgBox("Do you want to email this pdf?", vbYesNo + vbDefaultButton1 + vbQuestion, "Email: " & sFileName)

    If EmailAnswer = vbYes Then

        Set olApp = olApp.CreateItem(MailItem)
        Set MailItem = olApp.CreateItem(MailItem)

        With MailItem
            .To = ""
            .CC = ""
            .Subject = ""
            .Body = "Please allow me to purchase this."
            .Attachments.Add sPath & sFileName
            .Display
        End With
        Set MailItem = Nothing
        Set olApp = Nothing
    Else
        Exit Sub
    End If

    wbAnswer = MsgBox("Are you sure you want to close this workbook and Excel?", vbYesNo + vbDefaultButton1 + vbQuestion, "WORKBOOK: " & ActiveWorkbook.Name)
    If wbAnswer = vbYes Then
        ActiveWorkbook.Save
        Application.Quit
    Else
        Exit Sub
    End If

End Sub
 
Upvote 0
Here's the most recent change:
Code:
Private Sub cmdbtnPDF_Save_Click()
    Dim olApp As Outlook.Application
    Dim MI As Outlook.MailItem
    Dim sPath As String
    Dim sFileName As String
    Dim wbAnswer, EmailAnswer As Integer

    sPath = "C:\Users\username\Desktop\Saved PDF Test\"
    
    sFileName = Range("VendorName").Text & "_" & Format(Now, "mm-dd-yyyy_hhmm") & ".pdf"
    ActiveSheet.ExportAsFixedFormat xlTypePDF, sPath & sFileName
    ActiveWorkbook.Save

    EmailAnswer = MsgBox("Do you want to e-mail this pdf?", vbYesNo + vbDefaultButton1 + vbQuestion, "Email: " & sFileName)

    If EmailAnswer = vbYes Then

        Set olApp = olApp.CreateItem(MI)
        Set MI= olApp.CreateItem(MI)

        With MI
            .To = ""
            .CC = ""
            .Subject = ""
            .Body = "Please allow me to purchase this."
            .Attachments.Add sPath & sFileName
            .Display
        End With
        Set MI = Nothing
        Set olApp = Nothing
    Else
        Exit Sub
    End If

    wbAnswer = MsgBox("Are you sure you want to close this workbook and Excel?", vbYesNo + vbDefaultButton1 + vbQuestion, "WORKBOOK: " & ActiveWorkbook.Name)
    If wbAnswer = vbYes Then
        ActiveWorkbook.Save
        Application.Quit
    Else
        Exit Sub
    End If

End Sub
Stepping through the program the error '91 occurs on this line of code.
Code:
        Set olApp = olApp.CreateItem(MI)
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

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