"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.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
" & 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.
 

Alex Cotton

New Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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
 

niton

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server 2010
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.
 

Alex Cotton

New Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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?
 

niton

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server 2010
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
M Exchange Server 2010 Installation error ""The domain is invalid. Property Name: Domain" Exchange Server Administration 4
M Error message Path to file invalid Using Outlook 1
L Error when moving user mailboxes: The property value is invalid. Exchange Server Administration 4
N I set up contact group and when I try to send emails to groups, I get invalid recipient message (showing the group name) and no messages are sent Using Outlook 2
S Invalid Certificate Message Using Outlook 2
O Outlook 2007: Invalid Entry ID was passed in (all email address gone) Using Outlook 1
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
C "Name on the Security Certificate is Invalid or Does not Match..." using Outlok 2007 w/ Exchange 200 Using Outlook 59
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? Calendar Printing Assistant 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

Top