{"id":64283,"date":"2007-08-08T01:19:00","date_gmt":"2007-08-08T01:19:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/08\/08\/how-can-i-search-for-and-modify-formatted-text-in-a-word-document\/"},"modified":"2007-08-08T01:19:00","modified_gmt":"2007-08-08T01:19:00","slug":"how-can-i-search-for-and-modify-formatted-text-in-a-word-document","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-search-for-and-modify-formatted-text-in-a-word-document\/","title":{"rendered":"How Can I Search For and Modify Formatted Text in a 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! How can I search for formatted text in a Word document and then apply HTML tags to that text?<BR><BR>&#8212; ES <\/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, ES. You know, it\u2019s funny that you should ask this question. Just last night at the dinner table, the Scripting Guy who writes this column finally had The Talk with the Scripting Son. No sooner had they started eating before the Scripting Son blurted out, \u201cListen, Dad, I\u2019m 17-years-old now, and there are some things I need to know before I become an adult.\u201d<\/P>\n<P>Needless to say, those are the words every father dreads to hear. But duty calls, right? \u201cOK, son,\u201d said the Scripting Dad. \u201cWhat is it you need to know?\u201d<\/P>\n<P>\u201cI need to know this, Dad: how can I search for formatted text in a Word document and then apply HTML tags to that text?\u201d<\/P>\n<P>Like we said, no parent (or at least no father) really looks forward to having to explain the facts of Microsoft Word scripting life to their child; to be honest, it\u2019s embarrassing to have to talk about things like that with your kids. Nevertheless, being a parent means more than just going to watch your son play baseball. (Although the Scripting Guy who writes this column wishes someone had told him that <I>before<\/I> he became a parent.) As awkward as it might have been, the Scripting Guy who writes this column sat down and patiently explained how the Scripting Son could search for formatted text in a Word document and then apply HTML tags to that text. (And he tried his best not to sound too preachy about this, although he did note that Microsoft Word search scripts should only be written by someone who truly loves system administration scripting. Admittedly, casual scripting and recreational scripting sound fun, but, more often than not, they simply lead to heartache. The Scripting Dad\u2019s advice? Get married first and <I>then<\/I> write a script that can search for formatted text in a Word document and then apply HTML tags to that text.) <\/P>\n<P>For those of you who have yet to have The Talk with your own children, here\u2019s a transcript of what the Scripting Dad told his son, beginning with the script itself:<\/P><PRE class=\"codeSample\">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.Bold = True<\/p>\n<p>Do While True\n    objSelection.Find.Execute\n    If objSelection.Find.Found Then\n        objSelection.Text = &#8220;&lt;b&gt;&#8221; &amp; objSelection.Text &amp; &#8220;&lt;\/b&gt;&#8221;\n    Else\n        Exit Do\n    End If\nLoop\n<\/PRE>\n<P>\u201cOK, son,\u201d the Scripting Dad continued. \u201cLet\u2019s see if we can figure out how the miracle of searching for text in a Microsoft Word document occurs. As you can see, we start out by creating an instance of the <B>Word.Application<\/B> object and setting the <B>Visible<\/B> property to True; that gives us a running instance of Microsoft Word that we can see onscreen. After using the <B>Open<\/B> method to open the file C:\\Scripts\\Test.doc, we then use this line of code to create an instance of Word\u2019s <B>Selection<\/B> object (which, by default, also positions the cursor at the beginning of the document):<\/P><PRE class=\"codeSample\">Set objSelection = objWord.Selection\n<\/PRE>\n<P>\u201cWhat\u2019s that? Are we done yet? No, not quite, but we\u2019re getting there. Next we need to define three key properties of the <B>Find<\/B> object (a child object of the Selection object). In particular, we need to:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Set the <B>Forward<\/B> property to True. This tells the script that we want the search to move forward through the document; because we\u2019re starting our search at the very beginning of the document (we know that, because that\u2019s where the cursor is positioned) that means that we\u2019ll end up searching the entire file.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Set the <B>Format<\/B> property to True. This tells the script that we want to search for formatting rather than a specific string value.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Set the <B>Font.Bold<\/B> property to True. This tells the script \u2013 hey, that\u2019s right: this tells the script that we want to search for boldface text. Say, if you don\u2019t mind my asking, who told you about the Font.Bold property? And is there any chance you could give <I>me<\/I> a few pointers about Font.Bold?<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>\u201cAfter we configure these property values we set up a Do While loop designed to run forever. Or, at any rate, designed to run as long as True is equal to True. That\u2019s why we can\u2019t say that the loop actually <I>will<\/I> run forever; U.S. politics being what they are these days, it\u2019s just a matter of time before True is no longer considered equal to True. But that\u2019s a lesson for another day.<\/P>\n<P>\u201cOnce inside the loop, we call the <B>Execute<\/B> method; this tells the script to start searching the document, and to keep searching until one of two things happens: it either finds an instance of the target text (something in boldface), or it reaches the end of the document.<\/P>\n<P>\u201cStill with me? Good. Now, let\u2019s suppose that some boldface text <I>is<\/I> found; in that case, the Find object\u2019s <B>Found<\/B> property will be set to True. That\u2019s something we can test for using this line of code:<\/P><PRE class=\"codeSample\">If objSelection.Find.Found Then\n<\/PRE>\n<P>\u201cAnd assuming that we <I>have<\/I> found some boldface text, that means we need to put the HTML <B>&lt;b&gt;<\/B> tag at the beginning of that value and the <B>&lt;\/b&gt;<\/B> at the end of the value. That\u2019s what we do with this line of code:<\/P><PRE class=\"codeSample\">objSelection.Text = &#8220;&lt;b&gt;&#8221; &amp; objSelection.Text &amp; &#8220;&lt;\/b&gt;&#8221;\n<\/PRE>\n<P>\u201cAs you can see, there\u2019s really nothing mysterious about this: in technical terms, we\u2019re simply replacing the value of the Selection object\u2019s <B>Text<\/B> property (which will be the boldface text; when executing a search any found item is automatically selected) with the current value of the selected text <I>plus<\/I> our HTML tags. What does that mean? That means that a phrase that starts out looking like this:<\/P>\n<P><B>Boldface text<\/B><\/P>\n<P>\u201cWill end up looking like this:<\/P>\n<P><B>&lt;b&gt;Boldface text&lt;\/b&gt;<\/B><\/P>\n<P>\u201cWe then loop around, call the Execute method, and search for the next instance of the target text. What happens if there <I>isn\u2019t<\/I> a next instance of the target text (that is, no more boldface text)? In that case the Found property will <I>not<\/I> be equal to True and, in turn, we call the <B>Exit Do <\/B>statement to exit the not-so-endless loop.<\/P>\n<P>\u201cAnd <I>that<\/I>, Scripting Son, explains how birds and bees manage to search a Word document for formatted text and then put HTML tags around any such text the search uncovers.\u201d<\/P>\n<P>That should do it, ES. Incidentally, the Scripting Guy who writes this column apologizes for not directly answering your question, but instead giving you a transcript of something he told the Scripting Son. The truth is, the Scripting Guy who writes this column was a little embarrassed about answering this question, particularly in front of a large audience. Yes, we know: scripts that search for formatted text in a Word document and then apply HTML tags to that text are a normal (some would argue a <I>beautiful<\/I>) part of life, and there\u2019s really nothing to be embarrassed about. Be that as it may, there are some subjects that the Scripting Guy who writes this column simply has problems discussing in this column.<\/P>\n<P>Well, we mean <I>besides<\/I> system administration scripting.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I search for formatted text in a Word document and then apply HTML tags to that text?&#8212; ES Hey, ES. You know, it\u2019s funny that you should ask this question. Just last night at the dinner table, the Scripting Guy who writes this column finally had The Talk with the [&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-64283","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! How can I search for formatted text in a Word document and then apply HTML tags to that text?&#8212; ES Hey, ES. You know, it\u2019s funny that you should ask this question. Just last night at the dinner table, the Scripting Guy who writes this column finally had The Talk with the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64283","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=64283"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64283\/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=64283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}