Log in

View Full Version : [SOLVED:] Script to change subjects



SpriteMonkey
01-08-2016, 10:01 AM
Having a problem with VBA scripting in Outlook. I have these emails that come in and they all have a generic subject header followed by a specific external part number. I want to run the script so it strips out all the junk and replaces the entire subject with just our internal part number. This script should examine the email subject, and if the subject contains XXXXXX001, then replaces the entire subject with YYYYYY001. Here are some examples of the input and the result:


Incoming Subject: "Automated XYZ Company Order: Part Description (02246744)"
Resulting Subject: "YYYYYY001"
Incoming Subject: "Automated ABC Plant Order: Part Description (02293577)"
Resulting Subject: "YYYYYY002"
Incoming Subject: "New 123 B2B Handling Order: Part Description (02267241)"
Resulting Subject: "YYYYYY003"

I started out with some code like this:

Sub EditSubject(Item As Outlook.MailItem)
Item.Subject = Replace(Item.Subject, "XXXXXX", "YYYYYY")
Item.Save
End Sub

But all that does is change the part number without stripping out all the junk. I need some kind of function that searches the subject for "02246744" or one of the other external part numbers, then replaces the whole subject with the matching internal part number.

skatonni
01-08-2016, 02:09 PM
Try this



Sub EditSubject(Item As mailItem)

Dim srchString As String
Dim origSubj As String

origSubj = Item.subject

srchString = Right(Item.subject, 9)
srchString = Left(srchString, 8)

Select Case srchString

Case "02246744"
Item.subject = "YYYYYY001"

Case "02293577"
Item.subject = "YYYYYY002"

Case "02267241"
Item.subject = "YYYYYY003"

End Select

If Item.subject <> origSubj Then Item.Save

End Sub

Private Sub EditSubject_test()
' open a mailitem first
EditSubject ActiveInspector.currentItem
End Sub

SpriteMonkey
01-12-2016, 07:14 AM
Try this

Wow, that's amazing! I will try it today!

SpriteMonkey
01-12-2016, 09:19 AM
Worked perfectly! Solved a huge problem for us.