Outlook Macro To Read Subject Line

gduron

Board Regular
Joined
Mar 27, 2006
Messages
94
Hello,

I have an excel macro that creates a CSV file and sends the file to a server for processing. Once the processing is complete, the server sends an email to the user to let them know the processing is done. What I would like to do is create a VBA Macro that triggers an event in Microsoft Outlook each time outlook receives a new email. From the reading I've done, this is possible, but I can't get it to work.

Here is the code that I've put into the VBA Editor in MS Outlook:

Code:
Private Sub Application_NewMail()
    MsgBox "hello world"
End Sub

If I can get this event to fire, then I think I can figure out how to look at the new mail message and get the information I need. I just can't figure out how to get a new message to trigger this procedure.

I've changed the security settings so macros should run and I'm using Outlook 2007.

Thanks In Advance.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
here is something to get you started, you need to create this in thisoutlooksession, and set the rule to execute code

Code:
'****
' Routine for a custom rule for incoming emails
' Assembled from various Internet resources, and trial and error
'
' Author Jim Ward
' Creation November 2007
'
' Modification History
'
'
'****
' Firstly give the routine a name it matters not the name what is important is defining
' the passed parameter as type MAILITEM. You can then create a rule and use the Run A Script
' option and the name of the routine is displayed. Without the parameter it will not be seen !!
'****

Sub IncomingCheck(IncomingMail As MailItem)
Dim strID As String
Dim olNS As Outlook.NameSpace
Dim olMail As Outlook.MailItem
Dim oParentFolder As Outlook.MAPIFolder

'****
' What is key about this method is that by grabbing the ID and processing it from there it
' circumvents any OUTLOOK intervention asking to be granted access to the ITEM for security
' purposes
'****

strID = IncomingMail.EntryID
Set olNS = Application.GetNamespace("MAPI")
Set olMail = olNS.GetItemFromID(strID)

If InStr(1, olMail.Subject, "Productivity Report", vbTextCompare) > 0 Then
    Set myattach = olMail.Attachments
    myattach(1).SaveAsFile "C:\fred.txt"
End If

'****
' Clear out memory, all done
'****

Set olMail = Nothing
Set olNS = Nothing

End Sub
 
Upvote 0

Forum statistics

Threads
1,213,484
Messages
6,113,924
Members
448,533
Latest member
thietbibeboiwasaco

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top