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
 

ArrayON_56

New Member
Joined
Jul 9, 2021
Messages
9
Office Version
  1. 2019
Platform
  1. Windows
I tried both your codes and the error about non-email item was really triggered.

error.png


But I have outlook open with a message selected and opened.
outlook.png


Just in case I tried your codes both in Excel and Outlook, with the same result.
What am I doing wrong?
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

ZVI

MrExcel MVP
Joined
Apr 9, 2008
Messages
3,875
Office Version
  1. 2019
  2. 2016
  3. 2010
Platform
  1. Windows
Hi,

Thank you for the testing details.

Well, first try to test the code in Outlook but replace the Set OutlookApp = GetObject(, "Outlook.Application")
with Set OutlookApp = Application

If it works then the issue is security/privilege for the GetObject().
To resolve this issue, try to run both Excel and Outlook as administrator.
 

ArrayON_56

New Member
Joined
Jul 9, 2021
Messages
9
Office Version
  1. 2019
Platform
  1. Windows
Still doesn't work. It raises the same error. I changed OutlookApp value to Application and ran Outlook as Administrator.
Just in case i'm sending the code:

VBA Code:
Sub TestFunction()
  Dim OutlookApp As Outlook.Application
  Dim Inspector As Outlook.Inspector
  Dim Item As Outlook.MailItem
  Set OutlookApp = 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

PS: I was running the code in Outlook, not in Excel.
 

ZVI

MrExcel MVP
Joined
Apr 9, 2008
Messages
3,875
Office Version
  1. 2019
  2. 2016
  3. 2010
Platform
  1. Windows
Test this code in Outlook:
VBA Code:
Sub TestFunction1()
  Dim OutlookApp As Outlook.Application
  Dim Inspector As Outlook.Inspector
  Dim Item As Outlook.MailItem
  Set OutlookApp = Application
  Set Inspector = OutlookApp.ActiveInspector
  'On Error Resume Next
  If Inspector.CurrentItem.Class <> olMail Then
    MsgBox "Current Iten is not open email, its class = " & Inspector.CurrentItem.Class, vbExclamation, "Exit"
    Exit Sub
  End If
  'On Error GoTo 0
  Set Item = Inspector.CurrentItem
  MsgBox "Subject: " & vbLf & Item.Subject
End Sub
If it shows the class number of the open object, then select olMail , press F2 and find that number in the shown list to understand that class name.
 

Forum statistics

Threads
1,185,967
Messages
5,955,062
Members
438,181
Latest member
viv1

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
Top