"invalid or unqualified reference" on code that should work

Status
Not open for further replies.

Alex Cotton

New Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Hello, I am reasonably experienced in using VBA for Excel, but new in Outlook.
I copied some basic inbox new mail trigger code that should work. However, I get an "invalid or unqualified reference" refering to "item".
Can someone please give me a pointer as to where to look. I have googled for a long time, but cannot get this working.
Using Office 365 / Outlook 2016 (16.0.9126.2282) 32 bit.
The code I have:

Private Sub Application_Startup()
Dim objMyInbox As Outlook.MAPIFolder
Set objNS = Application.GetNamespace("MAPI")
Set objMyInbox = objNS.GetDefaultFolder(olFolderInbox)
Set objNewMailItems = objMyInbox.Items
Set objMyInbox = Nothing
End Sub

Private Sub objNewMailItems_ItemAdd(ByVal Item As Object)
'Ensure we are only working with e-mail items
If Item.Class <> olMail Then Exit Sub
Debug.Print "Message subject: " & Item; .Subject
Debug.Print "Message sender: " & Item; .SenderName & " (" & Item; .SenderEmailAddress & ")";
End Sub

I have put the relevant "item" in bold and blue.
Thanks in advance, and apologies for what has to be a really naïve obvious question.
 
" & Item; .Subject
that should be item period subject, not item semicolon space subject.
" & Item.Subject

As an FYI - when you copy from web pages, it's not unusual for &, <, > to be converted to the html version
HTML:
&amp;  &lt;  &gt;
(especially with wordpress and some other site management software). It doesn't look like that here - but something to be aware of.
 
that should be item period subject, not item semicolon space subject.


As an FYI - when you copy from web pages, it's not unusual for &, <, > to be converted to the html version
HTML:
&amp;  &lt;  &gt;
(especially with wordpress and some other site management software). It doesn't look like that here - but something to be aware of.

Thank you Diane. I had a feeling it was something rather amateur. The error is now gone.
Unfortunately, the VBA does not do anything (the macro is not even listed when I try to manual run it using F5).
I wish that VBA worked as easily in Outlook as in Excel. Do you have any clue as to why?
Thanks in advance
 
Whether in Excel or Outlook you cannot run code that requires a parameter as standalone.

You can drag a mailitem into the folder or add a mailitem to the folder with other VBA code

or for testing, except for the adding part, you can pass a mailitem like this:

Code:
Private Sub test()
' First open a mailitem
objNewMailItems_ItemAdd ActiveInspector.currentItem
End Sub

Your code should be in the ThisOutlookSession module.
 
Whether in Excel or Outlook you cannot run code that requires a parameter as standalone.

You can drag a mailitem into the folder or add a mailitem to the folder with other VBA code

or for testing, except for the adding part, you can pass a mailitem like this:

Code:
Private Sub test()
' First open a mailitem
objNewMailItems_ItemAdd ActiveInspector.currentItem
End Sub

Your code should be in the ThisOutlookSession module.

Thanks for the input Niton. I tried this code, but got an error 91. I am just having immense trouble getting started with VBA in Outlook.
All I have in the ThisOutlookSession is:
Code:
Option Explicit
Private objNS As Outlook.NameSpace
Private WithEvents objNewMailItems As Outlook.Items

Private Sub Application_Startup()

Dim objMyInbox As Outlook.MAPIFolder

Set objNS = Application.GetNamespace("MAPI")
Set objMyInbox = objNS.GetDefaultFolder(olFolderInbox)
Set objNewMailItems = objMyInbox.Items
Set objMyInbox = Nothing
End Sub

Private Sub objNewMailItems_ItemAdd(ByVal Item As Object)
'Ensure we are only working with e-mail items
If Item.Class <> olMail Then Exit Sub

Debug.Print "Message subject: " & Item.Subject
Debug.Print "Message sender: " & Item.SenderName & " (" & Item.SenderEmailAddress & ")"
End Sub

Private Sub test()
' First open a mailitem
objNewMailItems_ItemAdd ActiveInspector.CurrentItem
End Sub

But I just cannot get anything started. Probably I'm missing something simple that allows code to actually run?
 
Runtime Error 91 - Object variable not set.

With the assumption that the error occurs on the line objNewMailItems_ItemAdd ActiveInspector.CurrentItem the object is an open mailitem "ActiveInspector.CurrentItem ". Did you open a mailitem?
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
J Is it no longer possible to suppress Outlook 2019 Invalid Certificate name mismatch security alert via Registry? Using Outlook 1
D Multiple mailboxes, 1 certificate. How to prevent message "Invalid Certificate" Using Outlook 0
K Error: Paper Size Invalid Using Outlook 0
C OWA External Access - No URL / URL Invalid Exchange Server Administration 2
R Cannot Install Outlook Invalid XML Using Outlook 2
Q I cannot start Outlook 2007 I keep getting this error: __'Cannot start outlook window. Invalid XML,the view cannot be loaded.' Using Outlook 3
P SBS2008 / Exchange with POP3 Connector / Invalid Header Fields -problem Exchange Server Administration 20
T recipient email address is invalid, but resolved property is true Outlook VBA and Custom Forms 3
U Can't remove or delete invalid PST file Using Outlook 4
S Reference Custom Fields with VBA Outlook VBA and Custom Forms 2
K How to reference the selected folder Outlook VBA and Custom Forms 1
F want inbox on toolbar to reference icloud messages Using Outlook 2
N VBA Outlook reference to Excel and assigning category Outlook VBA and Custom Forms 13
U Reference Text for VBA for outlook Outlook VBA and Custom Forms 1
M Is there a reference for the valid XML tags and their options? Using Outlook 3
N Providing a reference to incoming mail in outlook 2007 BCM (Business Contact Manager) 2
S redemption reference Outlook VBA and Custom Forms 1
C Shared Calendar Recipient Reference Outlook VBA and Custom Forms 1

Similar threads

Back
Top