Run-time error '91': Object variable or With block variable not set

RBGDaboys

New Member
Joined
Jul 18, 2007
Messages
33
Hello,
We are seeing this error only on one computer. All 6 pc's are running same version Excel 2016. When we execute the macro below the error occurs at the "With OutlApp.CreateItem(0)" area. Any ideas?
Thank you

Code:
Sub AttachActiveSheetPDF()
  Dim IsCreated As Boolean
  Dim i As Long
  Dim PdfFile As String, Title As String
  Dim OutlApp As Object
 
  ' Not sure for what the Title is
  Title = "New Complaint Form " & Range("B3") & " attached."
 
  ' Define PDF filename
  PdfFile = ActiveWorkbook.FullName
  i = InStrRev(PdfFile, ".")
  If i > 1 Then PdfFile = Left(PdfFile, i - 1)
  PdfFile = PdfFile & "_" & ActiveSheet.Name & ".pdf"
 
  ' Export activesheet as PDF
  With ActiveSheet
    .ExportAsFixedFormat Type:=xlTypePDF, filename:=PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
  End With
 
  ' Use already open Outlook if possible
  On Error Resume Next
  Set OutlApp = GetObject(, "Outlook.Application")
  If Err Then
    Set OutlApp = CreateObject("Outlook.Application")
    IsCreated = True
  End If
  OutlApp.Visible = True
  On Error GoTo 0
 
  ' Prepare e-mail with PDF attachment
  With OutlApp.CreateItem(0)
   
    ' Prepare e-mail
    .Subject = Title
    .To = "xx.xx@xxx.com" ' <-- Put email of the recipient here
    '.CC = "" ' <-- Put email of 'copy to' recipient here
    .Body = "Hi," & vbLf & vbLf _
          & "A PDF copy is attached." & vbLf & vbLf _
          & "Regards," & vbLf _
          & Application.UserName & vbLf & vbLf
    .Attachments.Add PdfFile
   
    ' Try to send
    On Error Resume Next
    .Send
    Application.Visible = True
    If Err Then
      MsgBox "E-mail was not sent", vbExclamation
    Else
      MsgBox "File saved and e-mail successfully sent", vbInformation
    End If
    On Error GoTo 0
   
  End With
 
  ' Delete PDF file
  Kill PdfFile
 
  ' Quit Outlook if it was created by this code
  If IsCreated Then OutlApp.Quit
 
  ' Release the memory of object variable
  Set OutlApp = Nothing
 
 ActiveWorkbook.Save
 
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
This bit:
Code:
  On Error Resume Next
  Set OutlApp = GetObject(, "Outlook.Application")
  If Err Then
    Set OutlApp = CreateObject("Outlook.Application")
    IsCreated = True
  End If
  OutlApp.Visible = True
  On Error GoTo 0

You have an error handler on it so we don't know if it is successfully getting/creating the outlook instance. I would start by checking that out. Are you sure the machine has outlook on it?
 
Upvote 0
As Jon has already suspected, maybe the computer doesn't have Outlook. You can add an additional step to check whether you've successfully opened Outlook, maybe something like this...

Code:
  '
  '
  '


  ' Use already open Outlook if possible
  On Error Resume Next
  Set OutlApp = GetObject(, "Outlook.Application")
  If Err Then
    Err.Clear
    Set OutlApp = CreateObject("Outlook.Application")
    If Err Then
        MsgBox "Unable to open Outlook . . .", vbCritical
    Else
        IsCreated = True
    End If
  End If
  On Error GoTo 0
  
  If Not OutlApp Is Nothing Then
    OutlApp.Visible = True
    ' Prepare e-mail with PDF attachment
    With OutlApp.CreateItem(0)
        ' etc
        '
        '
    End With
  End If
  
  ' Delete PDF file
  Kill PdfFile
  
  '
  '
  '

Hope this helps!
 
Upvote 0
Thank you both for the replies.

Yes we have Outlook installed as they all have Office 365. I tried running the code with Outlook opened or closed and same error result.
 
Upvote 0
Try commenting out or temporarily deleting On Error Resume Next, and then try it again. Do you get an error? If so, on which line? And what type of error?
 
Upvote 0
In break mode, with the break point on With OutlApp.CreateItem(0), go to the immediate window and type:

?TypeName(OutlApp)

And hit return.

What do you get?
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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