title | keywords | f1_keywords | ms.prod | api_name | ms.assetid | ms.date | ms.localizationpriority |
---|---|---|---|---|---|---|---|
Find.HitHighlight method (Word) |
vbawd10.chm162529725 |
vbawd10.chm162529725 |
word |
Word.Find.HitHighlight |
11f6a7e5-7aba-a374-db39-327f6427364b |
06/08/2017 |
medium |
Find.HitHighlight method (Word)
Highlights all found matches and returns a Boolean that represents whether matches were found.
Syntax
expression.HitHighlight (FindText, HighlightColor, TextColor, MatchCase, MatchWholeWord, MatchPrefix, MatchSuffix, MatchPhrase, MatchWildcards, MatchSoundsLike, MatchAllWordForms, MatchByte, MatchFuzzy, MatchKashida, MatchDiacritics, MatchAlefHamza, MatchControl, IgnoreSpace, IgnorePunct, HanjaPhoneticHangul)
expression An expression that returns a Find object.
Parameters
Name | Required/Optional | Data type | Description |
---|---|---|---|
FindText | Required | Variant | Specifies the text to find. Use an empty string («») to search for formatting only. You can search for special characters by specifying appropriate character codes. For example, "^p" corresponds to a paragraph mark and "^t" corresponds to a tab character. |
HighlightColor | Optional | Variant | Specifies the highlight color for the text. Can be any RGB color or one of the WdColor constants. |
TextColor | Optional | Variant | Specifies the color of the text. Can be any RGB color or one of the WdColor constants. |
MatchCase | Optional | Variant | True to specify that the find text be case-sensitive. Corresponds to the Match case check box in the Find and Replace dialog box. |
MatchWholeWord | Optional | Variant | True to have the find operation locate only entire words, not text that is part of a larger word. Corresponds to the Find whole words only check box in the Find and Replace dialog box. |
MatchPrefix | Optional | Variant | True to match words beginning with the search string. Corresponds to the Match prefix check box in the Find and Replace dialog box. |
MatchSuffix | Optional | Variant | True to match words ending with the search string. Corresponds to the Match suffix check box in the Find and Replace dialog box. |
MatchPhrase | Optional | Variant | True ignores all white space and control characters between words. |
MatchWildcards | Optional | Variant | True to have the find text be a special search operator. Corresponds to the Use wildcards check box in the Find and Replace dialog box. |
MatchSoundsLike | Optional | Variant | True to have the find operation locate words that sound similar to the find text. Corresponds to the Sounds like check box in the Find and Replace dialog box. |
MatchAllWordForms | Optional | Variant | True to have the find operation locate all forms of the find text (for example, «sit» locates «sitting» and «sat»). Corresponds to the Find all word forms check box in the Find and Replace dialog box. |
MatchByte | Optional | Variant | True to distinguish between full-width and half-width letters or characters during a search. |
MatchFuzzy | Optional | Variant | True to use the nonspecific search options for Japanese text during a search. Read/write. |
MatchKashida | Optional | Variant | True if find operations match text with matching kashidas in an Arabic-language document. This argument may not be available to you, depending on the language support (U.S. English, for example) that you have selected or installed. |
MatchDiacritics | Optional | Variant | True if find operations match text with matching diacritics in a right-to-left language document. This argument may not be available to you, depending on the language support (U.S. English, for example) that you have selected or installed. |
MatchAlefHamza | Optional | Variant | True if find operations match text with matching alef hamzas in an Arabic-language document. This argument may not be available to you, depending on the language support (U.S. English, for example) that you have selected or installed. |
MatchControl | Optional | Variant | True if find operations match text with matching bidirectional control characters in a right-to-left language document. This argument may not be available to you, depending on the language support (U.S. English, for example) that you have selected or installed. |
IgnoreSpace | Optional | Variant | True ignores all white space between words. Corresponds to the Ignore white-space characters check box in the Find and Replace dialog box. |
IgnorePunct | Optional | Variant | True ignores all punctuation characters between words. Corresponds to the Ignore punctuation check box in the Find and Replace dialog box. |
HanjaPhoneticHangul | Optional | Variant | True ignores phonetic hangul and hanja characters. Available only if you have support for Korean languages. |
Return value
Boolean
Remarks
The HitHighlight method applies primarily to search highlighting in Office Outlook when Word is specified as the email editor. However, you can use this method on documents inside Word if you want to highlight found text. Otherwise, use the Execute method.
See also
- Find Object
[!includeSupport and feedback]
i have the codes below that is already working but still needs to be fine tuned. Its a function that finds matches for a wildcard search string and highlights the occurences. But i believe that it can still be done in a single line using replace all. I have tried almost everything that i could possibly think of and i think its time to ask the experts about this. Please show me how this can be done in a yet shorter way. Any help will be greatly appreciated. Thanks!
Sub findfunction()
If (findHL(activedocument.Range, "[aeiou]")) = True Then MsgBox "Highlight vowels Done", vbInformation + vbOKOnly, "Vowels Highlight Result"
End Sub
Function findHL(r As Range, s As String) As Boolean
Dim rdup As Range
Set rdup = r.Duplicate
rdup.Find.Wrap = wdFindStop
Do While rdup.Find.Execute(findtext:=s, MatchWildcards:=True) = True
If (Not rdup.InRange(r)) Then Exit Do
rdup.HighlightColorIndex = wdBlue
rdup.Collapse wdCollapseEnd
Loop
findHL = True
End Function
Deduplicator
44.3k7 gold badges65 silver badges115 bronze badges
asked Mar 16, 2011 at 6:27
Hidden very deep inside google:
Options.DefaultHighlightColorIndex = wdYellow
Selection.find.HitHighlight( string )
answered Feb 22, 2012 at 10:18
I managed to find my own solution doing a few trials. Here is my solution just for reference to others who might be looking for the same solution to my previous problem:
Sub findfunction()
If (findHL(activedocument.Content, "[aeiou]")) = True Then MsgBox "Highlight vowels Done", vbInformation + vbOKOnly, "Vowels Highlight Result"
End Sub
Function findHL(r As Range, s As String) As Boolean
Options.DefaultHighlightColorIndex = wdBlue
r.Find.Replacement.highlight = True
r.Find.Execute FindText:=s, MatchWildcards:=True, Wrap:=wdFindContinue, Format:=True, replacewith:="", replace:=wdReplaceAll
findHL = True
End Function
Simple yet it stripped down my previous code to a few lines.
KatieK
13.4k17 gold badges75 silver badges89 bronze badges
answered Mar 16, 2011 at 8:26
decrementordecrementor
1633 silver badges14 bronze badges
1
10-29-2012, 07:49 AM |
|||
|
|||
Help with VBA code to find and highlight text I did a search before posing this but did not find a solution for my particular request. I have the following code which is working beautifully however I want not only the found words to be highlighted but all the text in that row, which may include spaces and text. For example, after I find Procedure Step: I want it to highlight all the information following it. Procedure Step: Inbound Staging Here is the code I have so far: Code: Sub Highlight_Word() Dim sFindText As String 'Start from the top of the document Selection.HomeKey wdStory sFindText = "Procedure Step:" Selection.Find.Execute sFindText Do Until Selection.Find.Found = False Selection.Range.HighlightColorIndex = wdYellow Selection.MoveRight Selection.Find.Execute Loop End Sub Thanks
Last edited by macropod; 10-29-2012 at 02:40 PM. Reason: Added code tags & formatting
|
10-29-2012, 02:38 PM |
By «all the text in that row» do you mean an entire paragraph, all the text in a table row (including in other cells on that row), or a single line within a paragraph? PS: When posting code, please use code tags.
__________________ |
10-30-2012, 08:42 AM |
|||
|
|||
It would be a sentence in this case. I can select the text I want hightlighted manually by moving the cursor to that row and pressing shift End. That selects the entire text string I would like to highlight. Problem is when I record that and insert the code in my macro when it loops through the logic again the new serach string includes all the text which is selected instead of just «Procedure Step:» as it should. Hope this helps. Thanks |
10-30-2012, 10:15 AM |
|||
|
|||
I think I got it. It may be crude but it works. I think within a week or two I can be an MPV. Code: Sub Highlight_WordN() Dim sFindText As String 'Start from the top of the document Selection.HomeKey wdStory sFindText = "Procedure Step:" Selection.Find.Execute sFindText Do Until Selection.Find.Found = False Selection.EndKey Unit:=wdLine, Extend:=wdExtend Selection.Range.HighlightColorIndex = wdYellow Selection.MoveRight Selection.Find.Execute Loop End Sub |
10-30-2012, 01:48 PM |
I think you’ll find the following does what you want far more efficiently: Code: Sub Highlight_Words() Application.ScreenUpdating = False Options.DefaultHighlightColorIndex = wdYellow With ActiveDocument.Content.Find .ClearFormatting .Text = "Procedure Step:[!^13]{1,}" With .Replacement .Text = "^&" .ClearFormatting .Highlight = True End With .Forward = True .Wrap = wdFindContinue .Format = True .MatchWildcards = True .Execute Replace:=wdReplaceAll End With Application.ScreenUpdating = True End Sub
__________________ |
10-31-2012, 08:38 AM |
|||
|
|||
OK, you win. That was brilliant. My code was crude in comparison. Not quite MVP yet it turns out. Can you explain what this is doing? I’ve never seen this logic before. What is happening with the carat «^» and brackets? Code: Text = "Procedure Step:[!^13]{1,}" Thanks! Humbly, M |
10-31-2012, 02:03 PM |
The ‘[!^13]{1,}’ is a wildcard expression (you’ll see that I have ‘.MatchWildcards = True’). The [] tells Word to find any of the enclosed character(s). In this case, the ‘!’ says it’s a character other than what follows. The ‘^13’ represents a paragraph break. The {1,} says there is at least one, such character, but we don’t necessarily know what the upper limit is. So Word is being told to find ‘Procedure Step:’ followed by any number of chracters other than a paragraph break. Wildcard expressions greatly enhance Word’s Find/Replace capacity. See: http://word.mvps.org/FAQs/General/UsingWildcards.htm FWIW, you don’t even need the macro in this case — it’s doing nothing more than you can do through the Find/Replace dialogue.
__________________ |
09-11-2014, 06:29 AM |
Could you expand if one was to use find and replace and have all the replaced characters/ words highlighted. |
09-11-2014, 03:55 PM |
I don’t understand your question — the macro already highlights the found text.
__________________ |
This is a simpler version of a previous blog post—it doesn’t require you to set up and store a separate file of the words you want to highlight.
I wanted to create a list of vague words (such as some, most, often, frequently, sometimes—words that aren’t explicit or specific enough in technical writing and editing) that a simple Microsoft Word macro could find and highlight for me. I found some macros online that each partly solved the problem, and adapted one from Macropod (Paul Edstein) to suit my purposes as it was the closest to what I wanted. Full thanks to Macropod for this one.
There are several ways you can adapt this macro—I’ve commented some of my ideas in the code. I think the words are case sensitive, so if you want one with a capital (e.g. Some), you should add it as another entry.
NOTE: Copy all the code below to the clipboard—it goes off the page, so don’t type it out as you’ll miss some of it or could make a typo—then paste it into your VBA window.
Sub VagueWords() ' Source: Paul Edstein (Macropod), 8 Aug 2015: https://answers.microsoft.com/en-us/msoffice/forum/all/how-to-search-and-replace-multiple-wordsletters-in/af4753a0-7afd-433b-910d-a148da66f2bf ' Original macro name: MultiReplace ' Adapted by Rhonda Bracey, Cybertext Consulting, 22 Feb 2020 ' You could duplicate this macro with a different name (e.g. LegalWords [for must, shall, etc.]) using a different list of words in the StrFind and StrRepl lists Dim StrFind As String Dim StrRepl As String Dim i As Long ' In StrFind and StrRepl, add words between the quote marks, separate with a comma, NO spaces ' To only highlight the found words (i.e. not replace with other words), either use StrRepl = StrFind OR use the SAME words in the same order in the StrRepl list as for the StrFind list; comment/uncomment to reflect the one you're using ' To replace a word with another and highlight it, put the new word in the StrRepl list in the SAME position as the word in the StrFind list you want to replace; comment/uncomment to reflect the one you're using StrFind = "very,just,rarely,often,frequently,majority,most,minority,some,perhaps,maybe,regularly,sometimes,occasionally,best,worst,worse,better,seldom,few,many" StrRepl = StrFind ' StrRepl = "very,just,rarely,often,frequently,majority,most,minority,some,perhaps,maybe,regularly,sometimes,occasionally,best,worst,worse,better,seldom,few,many" Set RngTxt = Selection.Range ' Set highlight color - options are listed here: https://docs.microsoft.com/en-us/office/vba/api/word.wdcolorindex ' main ones are wdYellow, wdTurquoise, wdBrightGreen, wdPink Options.DefaultHighlightColorIndex = wdTurquoise Selection.HomeKey wdStory ' Clear existing formatting and settings in Find and Replace fields Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With ActiveDocument.Content.Find .Format = True .MatchWholeWord = True .MatchAllWordForms = False .MatchWildcards = False .Wrap = wdFindContinue .Forward = True For i = 0 To UBound(Split(StrFind, ",")) .Text = Split(StrFind, ",")(i) .Replacement.Highlight = True .Replacement.Text = Split(StrRepl, ",")(i) .Execute Replace:=wdReplaceAll Next i End With End Sub
For another way to do this, but instead changing the colour of a selected set of words, see https://wordribbon.tips.net/T013773_Changing_the_Color_of_a_List_of_Words.html
Update Oct 2020: A professor has made a YouTube video for his students/colleagues of this in action: https://www.youtube.com/watch?v=OIg95ETFxMQ
[Links last checked June 2020]
- Remove From My Forums
-
Question
-
I am trying to highlight and then extract certain words from a MS Word document. I am able to select the Word file I want to open, but now the code will not search and highlight. I have attached my current code below.
Sub Highlight_SP_Tower() 'Open an existing Word Document from Excel Dim FileToOpen Dim appwd As Object ChDrive "C:" FileToOpen = Application.GetOpenFilename _ (Title:="Please choose a file to import", _ FileFilter:="Word Files *.docx (*.docx),") If FileToOpen = False Then MsgBox "No file specified.", vbExclamation, "Error" Exit Sub Else Set appwd = CreateObject("Word.Application") appwd.Visible = True appwd.Documents.Open Filename:=FileToOpen End If 'This code will highlight the word(s) inside the " " With appwd Dim sFindText As String Selection.HomeKey wdStory sFindText = "IBM" Selection.Find.Execute sFindText Do Until Selection.Find.Found = False Selection.Range.HighlightColorIndex = wdYellow Selection.MoveRight Selection.Find.Execute Loop 'The following code will now extract highlighted words into a specified excel worksheet, populating A1,A2,A3...etc. Selection.ClearFormatting Selection.HomeKey wdStory, wdMove Selection.Find.ClearFormatting 'set searching for highlighted words Selection.Find.Highlight = True Selection.Find.Execute 'open workbook within new Excel application Dim EXL As Object Set EXL = CreateObject("Excel.Application") Dim xlsWB As Object Dim xlsPath As String 'put path to file here xlsPath = "C:UsersDooleyJDesktopVBA TestBook1" Set xlsWB = EXL.Workbooks.Open(xlsPath) Dim xlsRow As Long Do Until Selection.Find.Found = False 'we will write found words to first sheet in your Workbook, consecutive rows in column A xlsRow = xlsRow + 1 xlsWB.Sheets(1).Cells(xlsRow, "B") = Selection.Text Selection.Find.Execute Loop 'show excel application EXL.Visible = True End With End Sub