Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outlook Application_NewMail Problem

Status
Not open for further replies.

mmartinez74

Technical User
Nov 20, 2002
9
US
I am trying to understand how VBA in outlook works.

I created the following routine, which looks for a new message with the subject of %Test% and then displays a message box with the sender's name. But I get an error that says 'Object variable not set' What do I need to set?

Private Sub Application_NewMail()
Dim omMail As MailItem
Set ofFolder = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
If omMail.Subject = "%Test%" Then MsgBox (omMail.SenderName)
End Sub
 
You never assign omMail. I believe what you need is:

set omMail=ofFolder.items(1)

Check it out, if it doesn't work ask back.
Rob
[flowerface]
 
by adding this line, I get information on the first item in the mailbox....which is the oldest one. How can I get information on the last item in the mail box?
 
Okay - I guess it depends on how you've sorted your mailbox. For me, items(1) is the newest one. The outlook experts here will be able to give you a foolproof method that does not depend on the sort order. In your case, you could open .items(.items.count) and it would probably work.
Rob
[flowerface]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top