A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Outlook and VBA
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Newmail macro



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old July 15th 09, 10:19 AM posted to microsoft.public.outlook.program_vba
Swapnil G.
external usenet poster
 
Posts: 1
Default Newmail macro

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?
Ads
  #2  
Old July 15th 09, 03:19 PM posted to microsoft.public.outlook.program_vba
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default Newmail macro

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.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Swapnil G." Swapnil wrote in message
...
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?


 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting Subject and body information using Application.NewMail eve Al_21_11[_2_] Add-ins for Outlook 3 January 12th 09 08:42 PM
outlook newmail event for different version causing problems kedar_vj via OfficeKB.com Add-ins for Outlook 3 January 16th 07 04:05 PM
Outlook macro abends but Word macro runs successfully Jreue Outlook and VBA 0 December 13th 06 11:55 PM
Call macro stored in Excel workbook from Outlook's macro Gvaram Outlook and VBA 5 October 4th 06 06:26 AM
How to fire NewMail&NewMailEx Event? baryon Outlook - General Queries 0 August 17th 06 04:31 PM


All times are GMT +1. The time now is 12:30 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.