How do I script to change the subject line of incoming mail?

G

Glenn Harless

I wish to create a script to alter the subject line of incoming mail to
remove certain character strings.

Google has not been much help...any suggestions or guidence is appreciatied
 
S

Sue Mosher [MVP-Outlook]

One way is to use a "run a script" rule, a Rules Wizard action that invokes a VBA procedure with a MailItem or MeetingItem as its parameter, then uses that item in the code:

Sub RunAScriptRuleRoutine(MyMail As MailItem)
Dim strID As String
Dim olNS As Outlook.NameSpace
Dim olMail As Outlook.MailItem

strID = MyMail.EntryID
Set olNS = Application.GetNamespace("MAPI")
Set olMail = olNS.GetItemFromID(strID)
' do stuff with olMail, e.g.
olMail.SUbject = "whatever you want the subject to be"
olMaIl.Save

Set olMail = Nothing
Set olNS = Nothing
End Sub

See http://www.outlookcode.com/d/code/zaphtml.htm#ol2002 for another example.


--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
D

D-Man

Good Morning Sue,

I am new to scripting in VBA but I have done some vbs scripting. We receive
about 25,000 faxes and they are sent to an Outlook Mailbox. The subject of
the e-mails contain the senders fax number. We would like to have a rule
change the fax number to the customers name when the e-mail arrives. I tried
your code below with the changes below but I am unable to get it to work.
Would you be so kind as to help me through this?

Sub RunAScriptRuleRoutine(MyMail As MailItem)
Dim strID As String
Dim olNS As Outlook.NameSpace
Dim olMail As Outlook.MailItem

strID = MyMail.EntryID
Set olNS = Application.GetNamespace("MAPI")
Set olMail = olNS.GetItemFromID(strID)
' do stuff with olMail, e.g.
If olMail.Subject = "555-1212" Then
olMail.Subject = "New Customer"
olMail.Save
End If

Set olMail = Nothing
Set olNS = Nothing
End Sub

Thanks In advance
 
D

D-Man

I did go through that document. Could the problem be that if the faxes are
not going to the default inbox but rather than another user account mailbox?
Do I need to make changes in the script to reflect that?

Thanks in advance.
 
S

Sue Mosher [MVP-Outlook]

The code you cited works with an Outlook rule, which means it affects only messages coming into your own mailbox. To handle items in another mailbox with Outlook code, you would have to leave a machine running 24/7 and use a different coding approach -- the MAPIFolder.Items.ItemAdd event.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 

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