Newmail macro

S

Swapnil G.

I have a Outlook macro that checks for perticular words on receiving new
mail. If that word is found, it desplays a msg in msgbox. see below the code;

Private Sub Application_NewMail()
Dim oNS As NameSpace
Dim oFolder As MAPIFolder
Dim oItem As Object
Dim oNewMail As MailItem
Dim var As Variant

Set oNS = GetNamespace("MAPI")
Set oFolder = oNS.GetDefaultFolder(olFolderInbox)
Set oNewMail = oFolder.Items.GetLast

With oNewMail
'MsgBox .Subject & " " & .SenderName

For Each var In Array("Good Work", "Great Job", "Excellent", "Great
Work", "Keep it up", "Very Good")
If InStr(1, .Body, var, vbTextCompare) <> 0 Then
MsgBox "Congrats! " & .ReceivedByName & " Your work just got
appriciated by " & .SenderName, vbOKOnly
MsgBox "Go ahead and record it in CED. ", vbOKOnly
End If
Next
End With
End Sub

This code work fine. However I am getting a weird problem. When I lock and
then unlock my PC, an error msg pops up 'Run-time error 13: Type mismatch'
and on clicking 'debug' button it points out to line " Set oNewMail =
oFolder.Items.GetLast" in above code. Even if I click 'cancel' button, it
opens VBA editior.
Can somebody help me to get rid of this bug please?
 
K

Ken Slovak - [MVP - Outlook]

Maybe that item isn't a mail item? Get it as an Object and test for
item.Class to see if it really is an email item.

Also, NewMail() is hardly the best event to use, it misses things and only
fires at intervals. Use NewMailEx(), which provides a delimited list of
EntryID's for every item that's come in since the last time that event
fired.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top