VBA Macro to highlight some Keyword in mail body?

Status
Not open for further replies.

yjsfox

New Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server
Hi, I have been looking for a method to do keyword (including whole line) highlight (coloring is better) if the incoming mail body matched some rules/expr.

Say, I want to highlight the line "05/12/2017: 2 Hours On Fri; Category: Vacation - SJ" by matching rule: lines with date in start and has "Category:"

Is that doable in outlook VBA macro?
Thanks!!!
(I am using outlook 2016 and outlook 2013)

-- mail body--
..
Time Off Request Details:
05/12/2017: 2 Hours On Fri; Category: Vacation - SJ

Time Off Plan Balances as of 05/12/2017:
...
----mail body--
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
you can use regex to match, but you would need to rewrite the html to highlight it. Adding the category is no problem. If the line has special formatting already, it would probably be easier to do (since the html formatting already exists, just need to add to the style.)
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Actually... the simple script in this url works - as long as that line has unique formatting you can look for and replace.
How to Change the Font used for Outlook's RSS Feeds

(ETA: i just tested it - works fine - just make sure you use the right value for oldfont. I should change the order of the strings so oldfont is entered first, then new. :))
 
Last edited:

yjsfox

New Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server
Hi, Thanks for the hint. Sorry that I didn't try it until these days.
I am not very familiar with the regex. That's what need help for.

My purpose is to highlight the name and date/time in the Mailbody so it will look like this:
(Note the name and date/time has been bold faced to make it easier to read)

You have a new Workday business process pending approval for John Smith. You can now process this request via email reply or by logging into Workday to approve or reject this request.
Time Off Request Details:
08/25/2017: 8 Hours On Fri; Category: Floating Holiday - SJ
The bold face is not fixed so I definitely need regex to match the strings Ineed (name and date/time).
I have managed to do it using the following code. "nearly right" but need to refine the target string (wordToSearch) to be a regex extracted string.
The folloiwng code only get the "business process pending approval for" highlighted


Sub HighlightString (MyMail As Outlook.MailItem)
Dim strID As String
Dim objMail As Outlook.MailItem
strID = MyMail.EntryID
Set objMail = Application.Session.GetItemFromID(strID)
wordToSearch = "business process pending approval for "
' Ineed to find a way to match keyword from this line, assigned to wordToSearch

If InStr(1, objMail.HTMLBody, wordToSearch, vbTextCompare) > 0 Then
strData = objMail.HTMLBody
strData = Replace(strData, wordToSearch, "<FONT style=" & Chr(34) & "BACKGROUND-COLOR: yellow" & Chr(34) & ">" & wordToSearch & "</FONT>")
objMail.HTMLBody = strData
objMail.Save
End If
Set objMail = Nothing
End Sub
Thanks much for the help in advance!
 
Status
Not open for further replies.
Top