{"id":64573,"date":"2007-06-27T00:13:00","date_gmt":"2007-06-27T00:13:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/06\/27\/how-can-i-search-for-red-text-in-a-microsoft-word-document\/"},"modified":"2007-06-27T00:13:00","modified_gmt":"2007-06-27T00:13:00","slug":"how-can-i-search-for-red-text-in-a-microsoft-word-document","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-search-for-red-text-in-a-microsoft-word-document\/","title":{"rendered":"How Can I Search For Red Text in a Microsoft Word Document?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! I have a huge document that features occasional lines printed in red. How can I extract just the lines that are printed in red?<BR><BR>&#8212; GH <\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, GH. To begin with, the Scripting Guy who writes this column would like to say just one thing: never mind. In <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/jun07\/hey0625.mspx\"><B>yesterday\u2019s column<\/B><\/A> the Scripting Guy who writes this column bemoaned the fact that nothing interesting had happened to him in a long, long time. So guess what? Shortly after finishing the column something interesting <I>did<\/I> happen to him. And, as it turns out, interesting things aren\u2019t always all that much fun.<\/P>\n<P>So what <I>did<\/I> happen? Well, if he owned a Rolls Royce, he would say that his car experienced a \u201cfailure to proceed.\u201d Surprisingly enough, however, the Scripting Guy who writes this column <I>doesn\u2019t<\/I> own a Rolls Royce. Therefore, he would say this: his car just plain died. <\/P>\n<P>If there\u2019s any solace to be taken in having a dead car, at least there\u2019s this: the death was quick and painless. One moment the Scripting Guy was driving along without a care in the world, the next moment he was driving along without a <I>car<\/I> in the world. Pulling out of the grocery store parking lot the engine suddenly stopped running and would not restart, period. And before you ask, yes, the radio, the power windows, and all the other electrical stuff continued to work just fine; it was just the engine that no longer worked. Unfortunately, though, an engine that no longer works is about all it takes to bring a car to a dead stop. Talk about a single point of failure, huh?<\/P>\n<P>The preliminary diagnosis is that the car has a broken ECM. The Scripting Guy who writes this column doesn\u2019t know much about ECMs; he didn\u2019t even know what ECM stood for. Since then, however, he has learned at least two things: 1) the ECM is the central computing system that controls pretty much everything in the car, and 2) replacing an ECM will cost at least $2,000. <\/P>\n<P>Hurray!<\/P>\n<P>Anyway, if you\u2019ve been busy trying to think up ways to make this Scripting Guy\u2019s life more interesting, well, thanks but no thanks. Granted, a life where nothing interesting ever happens can be a bit boring at times. Nevertheless, it\u2019s a heck of a lot cheaper.<\/P>\n<P>In fact, from now on the Scripting Guy who writes this column is going to limit his interests to scripting (something his manager has been trying to get him to do for years). For example, wouldn\u2019t it be interesting to have a script that could extract only the lines in a Word document that were printed in red? Let\u2019s see if we can determine just <I>how<\/I> interesting that would be:<\/P><PRE class=\"codeSample\">Const wdColorRed = 255<\/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 objSelection = objWord.Selection<\/p>\n<p>objSelection.Find.Forward = True\nobjSelection.Find.Format = True\nobjSelection.Find.Font.Color = wdColorRed<\/p>\n<p>Do While True\n    objSelection.Find.Execute\n    If objSelection.Find.Found Then\n        Wscript.Echo objSelection.Text\n    Else\n        Exit Do\n    End If\nLoop\n<\/PRE>\n<P>Gosh; that\u2019s way more interesting than a broken ECM, isn\u2019t? As you can see, we start out by defining a constant named wdColorRed and setting the value to 255; we\u2019ll use this constant later on to tell Word the color of the text we\u2019re searching for. What if you wanted to search for text of a different color? Tell you what: send us, oh, say, $2000, and we\u2019ll help you out with that. <\/P>\n<P>Or, better yet, seeing as how the Scripting Editor is giving us that look again, <I>don\u2019t<\/I> send us $2000; instead just check out the <B>wdColor<\/B> enumeration in the <A href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/aa211923(office.11).aspx\" target=\"_blank\"><B>Microsoft Word VBA Language Reference<\/B><\/A>.<\/P>\n<TABLE class=\"dataTable\" id=\"EKE\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. But if you <I>do<\/I> feel like sending us $2000, well \u2026.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>After defining the constant wdColorRed we next create an instance of the <B>Word.Application<\/B> object and then set the <B>Visible<\/B> property to true; that gives us a running instance of Word that we can view onscreen. We then use these two lines of code to open the document C:\\Scripts\\Test.doc and create an instance of the Word <B>Selection<\/B> object, the object we need in order to search the document:<\/P><PRE class=\"codeSample\">Set objDoc = objWord.Documents.Open(&#8220;C:\\Scripts\\Test.doc&#8221;)\nSet objSelection = objWord.Selection\n<\/PRE>\n<P>Cool; this just keeps getting more and more interesting, doesn\u2019t it?<\/P>\n<P>Our next step is to configure the properties of the <B>Find<\/B> object, which happens to be a child object of the Selection object. That\u2019s what these three lines of code are for:<\/P><PRE class=\"codeSample\">objSelection.Find.Forward = True\nobjSelection.Find.Format = True\nobjSelection.Find.Font.Color = wdColorRed\n<\/PRE>\n<P>What does all this mean? Well, to begin with, setting the <B>Forward<\/B> property to True tells the script to search forward through the document; in other words, start searching at the beginning of the document and continue on through the end of the document. Setting the <B>Format <\/B>property to True tells the script that we want to search for formatting in addition to (or, in this case, instead of) text. Finally, we specify <I>which<\/I> formatting we\u2019re interested in: fonts that have a color equal to the constant wdColorRed (<B>Font.Color<\/B>).<\/P>\n<P>At this point we\u2019re ready to start searching. To that end, the first thing we do is set up a Do While loop that continues to run as long as True is equal to True. (That sounds like an endless loop, but don\u2019t worry: we\u2019ll show you a way out in a minute.) Inside that loop we then use the following command to start the search process and ferret out the first instance of red text:<\/P><PRE class=\"codeSample\">objSelection.Find.Execute\n<\/PRE>\n<P>So how will we know if the Find object actually finds any red text? That\u2019s easy; in that case, the value of the <B>Found<\/B> property will be True:<\/P><PRE class=\"codeSample\">If objSelection.Find.Found Then\n<\/PRE>\n<P>If the Found property <I>is<\/I> equal to True then the found text (that is, the red text) will automatically be selected; in turn, all we have to do to report what we found is to echo back the <B>Text<\/B> property of our Selection object:<\/P><PRE class=\"codeSample\">Wscript.Echo objSelection.Text\n<\/PRE>\n<P>And then we simply loop around, call the <B>Execute<\/B> method, and then search for the next instance of our target text. This process continues until the Found property is no longer True, which can mean only one thing: there\u2019s no more red text left to find. When that happens we simply call the <B>Exit Do<\/B> statement and exit our not-so-endless loop:<\/P><PRE class=\"codeSample\">Exit Do\n<\/PRE>\n<P>As you just saw, this script merely echoes back the found text. In his email, though, GH asked how he could go about extracting this text and adding it to a brand-new Word document. Here\u2019s a revised script that can do just that:<\/P><PRE class=\"codeSample\">Const wdColorRed = 255<\/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 objSelection = objWord.Selection<\/p>\n<p>Set objDoc2 = objWord.Documents.Add()\nSet objSelection2 = objWord.Selection<\/p>\n<p>objSelection.Find.Forward = True\nobjSelection.Find.Format = True\nobjSelection.Find.Font.Color = wdColorRed<\/p>\n<p>Do While True\n    objSelection.Find.Execute\n    If objSelection.Find.Found Then\n        strText = strText &amp; objSelection.Text &amp; vbCrLf\n    Else\n        Exit Do\n    End If\nLoop<\/p>\n<p>objSelection2.TypeText strText\n<\/PRE>\n<P>So what\u2019s the difference between this script and the first script we showed you? Well, for one thing, this script features <I>two<\/I> Word documents: C:\\Scripts\\Test.doc (with an object reference of objDoc) and a new document created using the <B>Add<\/B> method (with an object reference of objDoc2):<\/P><PRE class=\"codeSample\">Set objDoc2 = objWord.Documents.Add()\n<\/PRE>\n<P>In addition, this time around we don\u2019t echo back the text as we find it; instead, we store all the found text in a variable named strText (tacking a carriage return-linefeed onto the end of each segment):<\/P><PRE class=\"codeSample\">strText = strText &amp; objSelection.Text &amp; vbCrLf\n<\/PRE>\n<P>And then, last but surely not least, after we\u2019ve finished searching Test.doc we use the <B>TypeText<\/B> method to add all the found text to our second document:<\/P><PRE class=\"codeSample\">objSelection2.TypeText strText\n<\/PRE>\n<P>And there you have it.<\/P>\n<P>As for the ECM, well, the Scripting Guy who writes this column is already learning way more about ECMs than he ever wanted to know (starting with the fact that ECM is apparently short for <I>Electronic Control Module<\/I>). For example, Volkswagen Passats can\u2019t be hot-wired; you have to start them with the key fob because the fob sends a signal to the ECM that says, \u201cIt\u2019s OK; go ahead and start the car.\u201d Unfortunately, though, the ECM will sometimes malfunction and refuse to start the car even though you <I>have<\/I> the key fob. (And yes, this unnecessary layer of security <I>does<\/I> remind us of something. But we\u2019re not going to say what.) At other times the ECM can malfunction and apply a \u201cvirtual speed governor\u201d that limits the car to a top speed of 5 miles an hour. Needless to say, that\u2019s bad.<\/P>\n<P>Well, unless you live in Seattle. With the traffic we have around here, you rarely reach a top speed of 5 miles an hour anyway. <\/P>\n<P>At any rate, we\u2019ll keep you posted about this \u2026 interesting \u2026 new development. And remember, if you <I>do<\/I> want to send us $2,000 go right ahead. Just make sure you address the envelope to The Scripting Guy Who Writes That Column and <I>not<\/I> to The Scripting Editor. Thanks!<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have a huge document that features occasional lines printed in red. How can I extract just the lines that are printed in red?&#8212; GH Hey, GH. To begin with, the Scripting Guy who writes this column would like to say just one thing: never mind. In yesterday\u2019s column the Scripting Guy [&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],"class_list":["post-64573","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-word","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I have a huge document that features occasional lines printed in red. How can I extract just the lines that are printed in red?&#8212; GH Hey, GH. To begin with, the Scripting Guy who writes this column would like to say just one thing: never mind. In yesterday\u2019s column the Scripting Guy [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64573","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=64573"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64573\/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=64573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}