Search through an email to find and highlight text

0

I'm working with Excel trying to automate the sending of some emails. Right now, the code creates an email based on the cells next to the button clicked, forwards the email to the listed in a cell, and then inserts a specific body message based on some more cells. What is in the cells isn't really important, but what I need to do is search the original forwarded message for specific text, and if found, it needs to highlight that text.

My code is as follows:

Sub Asset_email()
Dim olApp As Outlook.Application
Dim olNs As Namespace
Dim Fldr As MAPIFolder
Dim olMail As Outlook.MailItem
Dim i As Integer
Dim olMsg As Outlook.MailItem
Dim r As Range
Dim strLocation As String
Dim o As Outlook.Application
Dim strbody As String

'Dim olAtt As Outlook.Attachments
'Set olAtt = olMsg.Attachments

Set r = ActiveSheet.Buttons(Application.Caller).TopLeftCell
Range(Cells(r.Row, r.Column), Cells(r.Row, r.Column)).Select

Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox).Folders("Asset Notifications Macro")
i = 1


For Each olMail In Fldr.Items
If InStr(olMail.body, ActiveCell.Offset(rowOffset:=0, ColumnOffset:=-3).Value) <> 0 Then
olMail.display



    strbody = "<BODY style=font-size:11pt;font-family:Calibri>Team,<br><br>" & _
              "Please see the notice below regarding " & _
              ActiveCell.Offset(rowOffset:=0, ColumnOffset:=-2).Value & _
              ".<br><br> Feel free to email the CSG team at myemailhere@email.com with any questions.<br><br>" & _
                "Thank you!"

With olMail.Forward
.To = ActiveCell.Offset(ColumnOffset:=-1)
.display
SendKeys ("%")
SendKeys ("7")
 'Call Sleep
Application.Wait (Now + TimeValue("0:00:03"))
.HTMLBody = strbody & "<br>" & .HTMLBody

End With
End If
Next
End Sub

The code works 100%. I just don't know the correct syntax to search and highlight the results.

In the above example, lets say I wanted to find and highlight the words "Thank you". How would one go about this?

JGoldz75

Posted 2015-06-16T18:25:24.930

Reputation: 3

Answers

0

Try this:

.HTMLBody = Replace(.HTMLBody, "Thank you", "<FONT style=" & Chr(34) & "BACKGROUND-COLOR: yellow" & Chr(34) & ">" & "Thank you" & "</FONT>")

niton

Posted 2015-06-16T18:25:24.930

Reputation: 1 724

Works like a charm! Thank you! Annoyingly, the text I am searching for appears once in the 0000000 format and once in the 000-000-0 format. Can the same code logistics be applied to highlight both instances of the number? – JGoldz75 – 2015-06-16T20:20:00.683