Microsoft Office Forums Adapt a script used in Word to highlight words, to work in Outlook?

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-22-2014, 01:44 PM
flatop flatop is offline Adapt a script used in Word to highlight words, to work in Outlook? Windows 7 64bit Adapt a script used in Word to highlight words, to work in Outlook? Office 2010 64bit
Novice
Adapt a script used in Word to highlight words, to work in Outlook?
 
Join Date: Jul 2013
Posts: 5
flatop is on a distinguished road
Smile Adapt a script used in Word to highlight words, to work in Outlook?

Hello, I am currently using the script below in Word 2010 to highlight specific words. Is it possible to adapt it to work in Outlook to search for the the same words in a Post? I believe the script was originally posted by Macropod in the Word forums and he suggested I post the question here. If it is not possible to adapt it, does anyone know how to write a similar script to accomplish the same?

Sub HiLightList()
Application.ScreenUpdating = False
Dim StrFnd As String, Rng As Range, i As Long
StrFnd = "display,e/monitor,e/port,mouse,keyboard,case,wide,screen,monitor,e-port"
On Error Resume Next
For i = 0 To UBound(Split(StrFnd, ","))
Set Rng = ActiveDocument.Range
With Rng.Find
.ClearFormatting
.Text = Split(StrFnd, ",")(i)
.Replacement.ClearFormatting
.Replacement.Highlight = True
.Replacement.Text = "^&"
.Forward = True


.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = True
.Execute Replace:=wdReplaceAll
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
Next
Set Rng = Nothing
Application.ScreenUpdating = True
End Sub
Reply With Quote
  #2  
Old 07-03-2014, 02:53 PM
niton niton is offline Adapt a script used in Word to highlight words, to work in Outlook? Windows 7 64bit Adapt a script used in Word to highlight words, to work in Outlook? Office 2010 64bit
Competent Performer
 
Join Date: Jul 2012
Posts: 102
niton is on a distinguished road
Default

From http://www.outlookcode.com/threads.a...essageid=33313

For the original you have to pass a mailitem

Code:
 
Sub CustomMailMessageRule(myMail As Outlook.mailItem)
 
Dim strID As String
Dim objMail As Outlook.mailItem
Dim wordToSearch As String
Dim strData As String
 
strID = myMail.EntryID
 
Set objMail = Application.Session.GetItemFromID(strID)
 
wordToSearch = "the"
 
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
You may prefer this which works on the current open item.

Code:
 
Sub SearchMailMessageHighlight()
    Dim strID As String
    Dim objMail As Outlook.mailItem
 
    Dim wordToSearch As String
    Dim strData As String
 
    ' Must have an open message
    Set objMail = Application.ActiveInspector.currentItem
 
    objMail.Display ' In case it is behind something or minimized
 
    wordToSearch = InputBox(Prompt:="Enter search string.", Title:="SearchMailMessage", Default:="the")
 
    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
 
        'To reverse highlighting when testing
        'strData = Replace(strData, wordToSearch, "<FONT style=" & Chr(34) & "BACKGROUND-COLOR: white" & Chr(34) & ">" & wordToSearch & "</FONT>")
        'objMail.HTMLBody = strData
 
        'objMail.save
 
    End If
    Set objMail = Nothing
End Sub
Reply With Quote
  #3  
Old 07-08-2014, 09:29 PM
flatop flatop is offline Adapt a script used in Word to highlight words, to work in Outlook? Windows 7 64bit Adapt a script used in Word to highlight words, to work in Outlook? Office 2010 32bit
Novice
Adapt a script used in Word to highlight words, to work in Outlook?
 
Join Date: Jul 2013
Posts: 5
flatop is on a distinguished road
Default

Quote:
Originally Posted by niton View Post
From http://www.outlookcode.com/threads.a...essageid=33313

For the original you have to pass a mailitem

Code:
 
Sub CustomMailMessageRule(myMail As Outlook.mailItem)
 
Dim strID As String
Dim objMail As Outlook.mailItem
Dim wordToSearch As String
Dim strData As String
 
strID = myMail.EntryID
 
Set objMail = Application.Session.GetItemFromID(strID)
 
wordToSearch = "the"
 
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
You may prefer this which works on the current open item.

Code:
 
Sub SearchMailMessageHighlight()
    Dim strID As String
    Dim objMail As Outlook.mailItem
 
    Dim wordToSearch As String
    Dim strData As String
 
    ' Must have an open message
    Set objMail = Application.ActiveInspector.currentItem
 
    objMail.Display ' In case it is behind something or minimized
 
    wordToSearch = InputBox(Prompt:="Enter search string.", Title:="SearchMailMessage", Default:="the")
 
    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
 
        'To reverse highlighting when testing
        'strData = Replace(strData, wordToSearch, "<FONT style=" & Chr(34) & "BACKGROUND-COLOR: white" & Chr(34) & ">" & wordToSearch & "</FONT>")
        'objMail.HTMLBody = strData
 
        'objMail.save
 
    End If
    Set objMail = Nothing
End Sub
Niton, thank you for replying to my post. I have been able to get your second option to work on Outlook Post items; however, I need to make the script loop so that it searches for multiple words as shown in my first post. Do you know how this can be accomplished? I am researching to see if I can find out to do it, but wanted to see if you already know how it can be accomplished. Thanks again.
Reply With Quote
  #4  
Old 07-09-2014, 02:05 PM
niton niton is offline Adapt a script used in Word to highlight words, to work in Outlook? Windows 7 64bit Adapt a script used in Word to highlight words, to work in Outlook? Office 2010 64bit
Competent Performer
 
Join Date: Jul 2012
Posts: 102
niton is on a distinguished road
Default

Add the loop from the Word code to the Outlook code.

Code:
 
Sub SearchMailMessageHighlight()
Dim objMail As Outlook.mailItem
 
Dim wordToSearch As String
Dim strData As String
 
Dim i As Long
Dim StrFnd As String
StrFnd = "display,e/monitor,e/port,mouse,keyboard,case,wide,screen,monitor,e-port"
 
' Must have an open message
Set objMail = Application.ActiveInspector.currentItem
 
objMail.Display ' In case it is behind something or minimized
 
For i = 0 To UBound(Split(StrFnd, ","))
    wordToSearch = Split(StrFnd, ",")(i)
 
    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
 
       'To reverse highlighting when testing
        'strData = Replace(strData, wordToSearch, "<FONT style=" & Chr(34) & "BACKGROUND-COLOR: white" & Chr(34) & ">" & wordToSearch & "</FONT>")
        'objMail.HTMLBody = strData
 
        'objMail.save
 
    End If
 
Next
Set objMail = Nothing
End Sub

Last edited by niton; 07-09-2014 at 02:08 PM. Reason: formatting the code
Reply With Quote
  #5  
Old 07-14-2014, 06:18 PM
flatop flatop is offline Adapt a script used in Word to highlight words, to work in Outlook? Windows 7 64bit Adapt a script used in Word to highlight words, to work in Outlook? Office 2010 32bit
Novice
Adapt a script used in Word to highlight words, to work in Outlook?
 
Join Date: Jul 2013
Posts: 5
flatop is on a distinguished road
Default

Ok, thank you very much for your help niton. I will mark this thread as Solved; however, I have one last question. Is there a way to make the scrip case insensitive, as the current one appears to be case sensitive? Thanks again.
Reply With Quote
  #6  
Old 07-15-2014, 01:07 PM
niton niton is offline Adapt a script used in Word to highlight words, to work in Outlook? Windows 7 64bit Adapt a script used in Word to highlight words, to work in Outlook? Office 2010 64bit
Competent Performer
 
Join Date: Jul 2012
Posts: 102
niton is on a distinguished road
Default

Try this

Code:
StrFnd = "Display,E/monitor,E/port,Mouse,Keyboard,Case,Wide,Screen,Monitor,E-port"
StrFnd = StrFnd & ",display,e/monitor,e/port,mouse,keyboard,case,wide,screen,monitor,e-port"
and

Code:
 
 If InStr(1, objMail.HTMLBody, wordToSearch, vbBinaryCompare) > 0 Then
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adapt a script used in Word to highlight words, to work in Outlook? Find and highlight multiple words in MS Word document AtaLoss Word VBA 33 10-10-2017 01:35 AM
Adapt a script used in Word to highlight words, to work in Outlook? VBA to highlight words if used too much aolszewski Word VBA 3 11-23-2013 02:07 AM
permanently highlight searched words in word 2013 arjay Word 4 08-16-2013 09:29 AM
Adapt a script used in Word to highlight words, to work in Outlook? Highlight all underlined words zdodson Word VBA 1 07-11-2013 04:53 PM
Adapt a script used in Word to highlight words, to work in Outlook? Macro to highlight words bertietheblue Word VBA 9 07-01-2013 12:39 PM


All times are GMT -7. The time now is 05:10 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2019 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft