VBA Script to Dynamically Change Subject Line on Incoming Emails for Outlook 2013
I have looked at this Outlook script example, which is similar and a great start for me: Outlook Script to edit subject
However, I am in need to change incoming message subject lines on arrival in a more difficult method which requires more complex string manipulation.
This is actually my base code I've used, which worked up to date:
Sub ConvertToPlain(MyMail As MailItem)
Dim strID As String
Dim objMail As Outlook.MailItem
strID = MyMail.EntryID
Set objMail = Application.Session.GetItemFromID(strID)
objMail.Subject = Left(objMail.Subject, 18)
objMail.Save
Set objMail = Nothing
End Sub
Now, I the original subjects are always in the following format (anything in brackets is a variable):
Ticket [#] - [SOMETHING] - [SOMETHING] - [TITLE]
I wish to remove the "[SOMETHING] - [SOMETHING] -" which is dynamically changing as you can see, and keep only:
Ticket [#] - [TITLE]
In this way, I can once again sort/filter with ease in outlook. How can this be accomplished in VBA? Thank you.
Your best bet is either to REGEX out the parts you want, or if you know the format is ALWAYS in this format, you could split the string on the - and take the first and last ones.
Along the lines of:
Sub ConvertToPlain(MyMail As MailItem)
Dim strID As String
Dim objMail As Outlook.MailItem
Dim splitSubject() as String
Dim concatSubject as String
strID = MyMail.EntryID
Set objMail = Application.Session.GetItemFromID(strID)
splitSubject = Split(objMail.Subject, "-")
concatSubject = splitSubject(LBound(splitSubject)) & " - " & splitSubject(UBound(splitSubject))
objMail.Subject = concatSubject
objMail.Save
Set objMail = Nothing
End Sub
That code is completely untested but hopefully puts you on track somewhat.