Runtime error 91 on Outlook.Application.ActiveInspector

ArrayON_56

New Member
Joined
Jul 9, 2021
Messages
9
Office Version
  1. 2019
Platform
  1. Windows
I would like to create a VBA script in excel that analyzes Subject of currently viewed mail in Outlook, parse it and put some info from it to excel cell.
However when I want to read the subject from Outlook by Outlook.Application.ActiveInspector, it gives me runtime error 91 to line "Set Inspector = OutlookApp.ActiveInspector" - Object variable or With block variable not set.
But I have initialised the variable it and I am trying to set it to be ActiveInspector of Outlook.

Here is my code:

Sub TestFunction()
Dim OutlookApp As Outlook.Application
Dim Inspector As Outlook.Inspector 'I initialise the variable here
Dim Item As Outlook.Inspector
Set Inspector = OutlookApp.ActiveInspector 'This is the error 91 line
Set Item = Inspector.CurrentItem
End Sub

What am I doing wrong?
Thanks in advance
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
You did not create an instance of the Outlook app, you only declared it. Add this line after your Dim statements:

VBA Code:
Set OutlookApp = New Outlook.Application
 
Upvote 0
Thanks for answer.
I created instance of Outlook application, so the code looks as this:
VBA Code:
Sub TestFunction()
   Dim OutlookApp As Outlook.Application
   Dim Inspector As Outlook.Inspector
   Dim Item As Outlook.Inspector
   Set OutlookApp = New Outlook.Application
   Set Inspector = OutlookApp.ActiveInspector
   Set Item = Inspector.CurrentItem
End Sub
But now it fails on last line ' Set Item = Inspector.CurrentItem' with the same error 91 - Object variable or With block variable not set. Do I need to do something else with Inspector variable?
All macros are turned on both in Excel and in Outlook, Outlook is open and there is selected message when I execute this function.
 
Upvote 0
You have Item declared as an Inspector. That is incorrect. Item should be declared as Object.
 
Upvote 0
You have Item declared as an Inspector. That is incorrect. Item should be declared as Object.
I Changed Outlook.Inspector to Object in Item declaration, but the code still fails with the same error on the same line.
the Code looks as this now:
VBA Code:
Sub TestFunction()
    Dim OutlookApp As Outlook.Application
    Dim Inspector As Outlook.Inspector
    Dim Item As Object
    Set OutlookApp = New Outlook.Application
    Set Inspector = OutlookApp.ActiveInspector
    Set Item = Inspector.CurrentItem
End Sub
Outlook still open with a selected message.
 
Upvote 0
That code works for me in Excel when I set a reference to the Outlook library (if you didn't have that, it wouldn't even compile). So at this point I am stuck without being able to put my hands on your file.
 
Upvote 0
I'm sending my file. It's just excel document with emty sheet and that macro inside.
As I don't know how to send it as attachment to this message, or if it can be done, i'm sending it as link to my own server: http://kybernado.com/document.xlsm
 
Upvote 0
That code works for me in Excel when I set a reference to the Outlook library (if you didn't have that, it wouldn't even compile). So at this point I am stuck without being able to put my hands on your file.

Is there anything else you did, or anything else I should keep my eye on? I tried to copy just that macro code to another document on another machine with office 365 and I got the same error after putting a reference to Outlook 16 to that document.
I also tried to put that code into outlook macro, but the same error on the same line occured, so I think I'm doing something very silly.
In outlook macro I tried also to change the Set OutlookApp to just Outlook.Application (without New), no change.
 
Upvote 0
Hi,

Try this code:
VBA Code:
Sub TestFunction()
  Dim OutlookApp As Outlook.Application
  Dim Inspector As Outlook.Inspector 'I initialise the variable here
  Dim Item As Outlook.MailItem
  Set OutlookApp = GetObject(, "Outlook.Application")
  Set Inspector = OutlookApp.ActiveInspector
  On Error Resume Next
  If Inspector.CurrentItem.Class <> olMail Then
    MsgBox "Current Iten is not open email", vbExclamation, "Exit"
    Exit Sub
  End If
  On Error GoTo 0
  Set Item = Inspector.CurrentItem
  Debug.Print Item.Subject
End Sub
Note: the active email window should be open.
 
Upvote 0
This code does not require the email window to be open, just selected
VBA Code:
Sub TestFunction1()
  
  Const olMail = 43
  Dim OutlookApp As Outlook.Application
  Dim MailItem As Outlook.MailItem
 
  Set OutlookApp = GetObject(, "Outlook.Application")
  With OutlookApp.ActiveExplorer.Selection
    If .Item(1).Class = olMail Then
      Set MailItem = .Item(1)
      Debug.Print MailItem.Subject
    Else
      MsgBox "Outlook's selected item(1) is not email", vbExclamation, "Exit"
    End If
  End With
  
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,044
Members
449,063
Latest member
ak94

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