{"id":66903,"date":"2006-07-14T10:42:00","date_gmt":"2006-07-14T10:42:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/07\/14\/how-can-i-search-for-and-reformat-highlighted-text-in-a-word-document\/"},"modified":"2006-07-14T10:42:00","modified_gmt":"2006-07-14T10:42:00","slug":"how-can-i-search-for-and-reformat-highlighted-text-in-a-word-document","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-search-for-and-reformat-highlighted-text-in-a-word-document\/","title":{"rendered":"How Can I Search For (and Reformat) Highlighted Text in a Word Document?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I find text in a Word document that has been highlighted in a particular color (say, turquoise), and then both remove the highlight and change the font color of that text to something else (say, blue)? I\u2019d be extremely chuffed if you can solve this one!<BR><BR>&#8212; KB<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, KB! Blimey! You know, when we first saw your message we thought, \u201cKB\u2019s off her trolley!\u201d But we decided to give it welly, and when we realized that things were tickety-boo we were absolutely gob-smacked; in fact, we said to ourselves, \u201cGuess the lass wasn\u2019t so barmy after all; this should make her bite her arm off!\u201d And then after all that everything ended up luvvly-jubbly. Brill!<\/P>\n<TABLE id=\"EYC\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. No, this is <I>not<\/I> all codswallop. If you don\u2019t believe us, check out <A href=\"http:\/\/www.effingpot.com\/slang.shtml\" target=\"_blank\"><B>this Web site<\/B><\/A> for more information.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Hmmm \u2026 You know, once you start talking like that, it\u2019s hard to stop (blooming hard). So maybe we should just belt up and show you the script (which, as it turns out, was a doddle after all):<\/P><PRE class=\"codeSample\">Const wdTurquoise = 3\nConst wdNoHighlight = 0\nConst wdBlue = 2<\/p>\n<p>Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nobjWord.Visible = True<\/p>\n<p>Set objDoc = objWord.Documents.Open(&#8220;C:\\Scripts\\Test.doc&#8221;)\nSet objRange = objDoc.Range<\/p>\n<p>objRange.Find.Highlight = True\nobjRange.Find.Forward = True<\/p>\n<p>Do while objRange.Find.Execute\n    If objRange.HighlightColorIndex = wdTurquoise Then\n        objRange.HighlightColorIndex = wdNoHighlight\n        objRange.Font.ColorIndex = wdBlue\n    End If\n    intPosition = objRange.End\n    objRange.Start = intPosition\nLoop\n<\/PRE>\n<P>Because we have no desire to drop a clanger, we\u2019ll set aside the British slang for a moment and try to explain how the script works. As you can see, we start out by defining three constants, one that we\u2019ll use to specify the highlight color we\u2019re searching for (wdTurquoise), and two to specify the new highlight and font colors for any text we find (wdNoHighlight and wdBlue, respectively):<\/P><PRE class=\"codeSample\">Const wdTurquoise = 3\nConst wdNoHighlight = 0\nConst wdBlue = 2\n<\/PRE>\n<P>After that we create an instance of the <B>Word.Application<\/B> object and set the <B>Visible<\/B> property to True; that simply gives us a running instance of Microsoft Word that we can see on screen. With Word up and running we use the <B>Open<\/B> method to open the document C:\\Scripts\\Test.doc, then use this line of code to create an instance of the Word <B>Range<\/B> object (which, by default, will be positioned at the beginning of the document:<\/P><PRE class=\"codeSample\">Set objRange = objDoc.Range\n<\/PRE>\n<P>Now we\u2019re ready for action. We won\u2019t discuss finding and replacing text in any detail today; that\u2019s because we do a much more thorough job of that in one of our <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/officetips\/may05\/tips0512.mspx\"><B>Office Space columns<\/B><\/A>. However, our next step <I>is<\/I> to configure two properties of the <B>Find<\/B> object: we want to find words that have been highlighted (in any color), and we want our search to start at the beginning of the range and then move forward throughout the document. That\u2019s what these two lines of code are for:<\/P><PRE class=\"codeSample\">objRange.Find.Highlight = True\nobjRange.Find.Forward = True\n<\/PRE>\n<P>Admittedly, the rest is a bit of a bodge; that\u2019s because, while it\u2019s easy enough to search for text that has <I>some<\/I> kind of highlight, we couldn\u2019t figure out a way to search for text that had a specific color of highlight. Rather than faff around genning up on a way to search for a specific color highlight we decided to use this spot of code instead:<\/P><PRE class=\"codeSample\">Do While objRange.Find.Execute\n    If objRange.HighlightColorIndex = wdTurquoise Then\n        objRange.HighlightColorIndex = wdNoHighlight\n        objRange.Font.ColorIndex = wdBlue\n    End If\n    intPosition = objRange.End\n    objRange.Start = intPosition\nLoop\n<\/PRE>\n<P>What we\u2019re doing here is setting up a Do While loop that runs as long as the search command (<B>Find.Execute<\/B>) runs. When we call the Execute method, the script will start wherever the Range object is positioned and then search for the first item that meets the search criteria. (In this case, of course, our only criterion is that the text be highlighted.) If no items are found then the search automatically ends, which also means that we\u2019ll automatically exit the Do While loop.<\/P>\n<P>That\u2019s how we keep the script from simply searching the document over and over and over again.<\/P>\n<P>If we <I>do<\/I> find a highlighted item we check to see if the value of the <B>HighlightColorIndex<\/B> property is turquoise (incidentally, you can find information about all these properties and their allowed values in the <A href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/vbawd11\/html\/WordVBAWelcome_HV01135786.asp\" target=\"_blank\"><B>Microsoft Word VBA Language Reference<\/B><\/A>):<\/P><PRE class=\"codeSample\">If objRange.HighlightColorIndex = wdTurquoise Then\n<\/PRE>\n<P>If the highlight color is turquoise we execute these two lines of code:<\/P><PRE class=\"codeSample\">objRange.HighlightColorIndex = wdNoHighlight\nobjRange.Font.ColorIndex = wdBlue\n<\/PRE>\n<P>With the first line, we remove the highlight for the found text; with the second, we change the font color of that text to blue. To tell you the truth, this was a bit jammy, but it ended up working just fine.<\/P>\n<P>And to think that some of you have said that the Scripting Guys are gormless!<\/P>\n<P>Oh: and if text is highlighted but the color is something other than turquoise then we simply leave well enough alone. Quite.<\/P>\n<P>Of course, this only gets us as far as the first highlighted item in the text. To search for any subsequent items that meet our criteria we need to use these two lines of code:<\/P><PRE class=\"codeSample\">intPosition = objRange.End\nobjRange.Start = intPosition\n<\/PRE>\n<P>Why? Well, until we call these two lines of code our range will consist solely of the text that we just found. That\u2019s not what we want: after all, if we call the Execute method again we\u2019ll end up searching just that little block of text. And because that text is no longer highlighted, the search &#8211; and our script &#8211; will simply end. <\/P>\n<P>To avoid that, we assign the value of the <B>End<\/B> property (the last character in the range) to a variable named intPosition; we then set the <B>Start<\/B> property of the range to intPosition. <\/P>\n<P>So why do we do <I>that<\/I>? Well, suppose we found a highlight that covers characters 5 through 10; that means our range also covers characters 5 through 10. If we set intPosition to 10 (the last character in the range), and then assign that value to the range\u2019s Start property, that moves the beginning of our range to character position 10. And because we didn\u2019t specify an End value, the range will automatically encompass the remainder of the document. In other words, now when we call Execute we\u2019ll start searching at the point where the first search left off, and we\u2019ll continue searching until either we find a new item or we reach the end of the document. <\/P>\n<P>Which is exactly what we want to happen.<\/P>\n<P>Feeling knackered? We don\u2019t blame you. But even if you don\u2019t fully understand how it works, it\u2019s still a good flutter, especially for a script this smashing.<\/P>\n<P>Come on: would the Scripting Guys tell you porkies?<\/P>\n<P>At any rate, Bob\u2019s your uncle, KB. Cheerio<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I find text in a Word document that has been highlighted in a particular color (say, turquoise), and then both remove the highlight and change the font color of that text to something else (say, blue)? I\u2019d be extremely chuffed if you can solve this one!&#8212; KB Hey, KB! Blimey! [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[84,49,3,5,395],"class_list":["post-66903","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-word","tag-office","tag-scripting-guy","tag-vbscript","tag-word-application"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I find text in a Word document that has been highlighted in a particular color (say, turquoise), and then both remove the highlight and change the font color of that text to something else (say, blue)? I\u2019d be extremely chuffed if you can solve this one!&#8212; KB Hey, KB! Blimey! [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66903","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=66903"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66903\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=66903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}