"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.
Thread starter Similar threads 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
B Windows 7 Home Premium installed but Outlook says invalid product key Using Outlook 2
Y invalid user name - password / exchange mail client Using Outlook 10
K Invalid Username and Password in Facebook Social Network Setup Using Outlook 4
S When user send mail , Getting NDR 5.1.2 .invalid Email address. Using Outlook 6
M This message was not processed because it contains an invalid recipient. Using Outlook 1
R Sent Items says the handle is invalid and the Calendar is unable to display the folder Using Outlook 1
B OWA Invalid Service Address Error Exchange Server Administration 3
S After installing 3rd party Certificate for IIS , prompt warning for mapi clients the name on security certificate is invalid , Exchange Server Administration 3
M Exchange 2010 and Outlook 2010 - The name on the security certificate is invalid Exchange Server Administration 3
T OWA broken after Exchange 2010 SP1 update (invalid accepted domain fixes already applied) Exchange Server Administration 2
W Mail Tip says invalid recipient for valid addresses Exchange Server Administration 8
M LinkedIn provider indicates "invalid user name or Password" although same credentials work at websit Using Outlook 1
O I keep getting outlook connector sync errors #4201 Client. Invalid folder name. Using Outlook 2
K Error:This message was not processed because it contains an invalid recipient. Using Outlook 8
D Emails sent using quick step bounce back as "undeliverable" because of "invalid recipient" Using Outlook 10
B #550 5.1.3 STOREDRV.Submit; invalid recipient address #SMTP# Using Outlook 2
D Outlook 2010 says product key is invalid when updating from the trial Using Outlook 2
N Exchange 2007 Supplied Credentials invalid Exchange Server Administration 3
R User Account Locks Out after 2 invalid logon attempts Using Outlook 2
A The certificate is invalid for Exchange Server usage Exchange Server Administration 5
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
S wildcard certifictae invalid for Exchange use Exchange Server Administration 13
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
F Saving mail for future reference. Using Outlook 1
M BCM error: Object reference not set to an instance of the object. Got this msg. BCM database will Using Outlook 2
K Exchange 2010 ECP Reports - search for emails from external recepient get 'Object Reference not set to an instance of an object' error Exchange Server Administration 4
I An error occurred with error code '3221685232' and message 'An attempt was made to reference a token that does not exist. Exchange Server Administration 4
S redemption reference Outlook VBA and Custom Forms 1
C Shared Calendar Recipient Reference Outlook VBA and Custom Forms 1
Similar threads


















































Top