"invalid or unqualified reference" on code that should work

Status
Not open for further replies.

Alex Cotton

New Member
Best answers
0
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
Best answers
0
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
Best answers
0
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
Best answers
0
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
Best answers
0
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
Best answers
0
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.
Top