{"id":68053,"date":"2006-02-01T12:03:00","date_gmt":"2006-02-01T12:03:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/02\/01\/how-can-i-boldface-a-specific-word-throughout-a-microsoft-word-document\/"},"modified":"2006-02-01T12:03:00","modified_gmt":"2006-02-01T12:03:00","slug":"how-can-i-boldface-a-specific-word-throughout-a-microsoft-word-document","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-boldface-a-specific-word-throughout-a-microsoft-word-document\/","title":{"rendered":"How Can I Boldface a Specific Word Throughout a Microsoft 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 boldface a specific word throughout a Microsoft Word document?<BR><BR>&#8212; SB<\/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, SB. You\u2019ll probably find this hard to believe, but when you work with scripts and scripting all day you tend to get a little jaded. You say you have a script that automatically backs up and clears all the event logs on all your computers? How nice. You have a script that monitors folders and lets you know when new files are added? Been there, done that. You have a script that can bring the dead back to life? Hey, who doesn\u2019t?<\/P>\n<P>For some reason, though, SB, your question piqued our interest. Maybe it\u2019s because we\u2019re always looking for a script that uses Microsoft Word; scripting with Word is actually kind of fun. Or maybe it\u2019s because this request offered a bit of a challenge: although we were pretty sure that we could write a script that would boldface specific words in a document we\u2019d never actually tried it. Or maybe it\u2019s because all the dead people we brought back to life have been pretty insistent that we go through <I>their<\/I> documents and boldface specific words. You know, things like the phrase <B>I am <\/B><B><I>too<\/I><\/B><B> alive<\/B>.<\/P>\n<P>Oh, well. Regardless of what motivated us, here\u2019s what we came up with:<\/P><PRE class=\"codeSample\">Const wdReplaceAll  = 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 objSelection = objWord.Selection<\/p>\n<p>objSelection.Find.Text = &#8220;Fabrikam&#8221;\nobjSelection.Find.Forward = TRUE\nobjSelection.Find.MatchWholeWord = TRUE<\/p>\n<p>objSelection.Find.Replacement.Font.Bold = True<\/p>\n<p>objSelection.Find.Execute ,,,,,,,,,,wdReplaceAll\n<\/PRE>\n<P>As you can see, not only was this a fun script to write but &#8211; as an added bonus &#8211; it was an easy script to write, too. After defining a constant named wdReplaceAll (we\u2019ll talk about that in a moment), we create an instance of the <B>Word.Application<\/B> object and then set the <B>Visible<\/B> property to True; this gives us an instance of Microsoft Word that we can see on screen. We use the <B>Open<\/B> method to open the document C:\\Scripts\\Test.doc, then create an instance of the Word <B>Selection<\/B> object (which, by default, positions the cursor at the beginning of the document).<\/P>\n<P>Now we\u2019re ready to roll. What we want to do is boldface any instances of the word<I> Fabrikam<\/I>; hence we set the value of the <B>Find<\/B> object\u2019s <B>Text<\/B> property to \u201cFabrikam\u201d (in other words, that\u2019s what we\u2019re searching for). Next we set the <B>Forward<\/B> property to True; this ensures that our search will start at the beginning of the document and end at, well, the end of the document. Finally, we set the <B>MatchWholeWord<\/B> property to True; we do this just in case there happens to be some crazy construction like FabrikamCorporation in the document. If MatchWholeWord was set to False, then <I>any<\/I> instance of the string Fabrikam would be boldfaced. That would give us something similar to this: <B>Fabrikam<\/B>Corporation. Which we probably don\u2019t want.<\/P>\n<TABLE id=\"ELE\" 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>. Yes, we <I>are<\/I> kind of zipping through the explanation here, aren\u2019t we? But that\u2019s OK: after all, we have an <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/officetips\/may05\/tips0512.mspx\"><B>Office Space article<\/B><\/A> that discusses the process of finding and replacing text in more detail.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>All that gives us a mechanism for finding all instances of the word Fabrikam. But once we find them how do we boldface each of those instances? Here\u2019s how:<\/P><PRE class=\"codeSample\">objSelection.Find.Replacement.Font.Bold = True\n<\/PRE>\n<P>Yes, it\u2019s that easy. Here we use the <B>Replacement<\/B> object (a child object of the Find object) and specify that the <B>Font.Bold<\/B> property should be True. That\u2019s all we have to do. If we wanted to un-boldface each instance of Fabrikam we would set the Font.Bold property to False. Needless to say, we could specify new replacement text, a new replacement font size, pretty much anything we want.<\/P>\n<P>See why we like scripting with Microsoft Word so much?<\/P>\n<P>After configuring the replacement object we then call the <B>Execute<\/B> method to start the find-and-replace operation:<\/P><PRE class=\"codeSample\">objSelection.Find.Execute ,,,,,,,,,,wdReplaceAll\n<\/PRE>\n<P>And, no, we didn\u2019t fall asleep with our finger on the comma key. Well, not this time anyway. As it turns out, there are <I>tons<\/I> of optional parameters available for the Execute method. For this script we didn\u2019t care about any of those. However, because parameters have to be specified in order, we couldn\u2019t just do something like this:<\/P><PRE class=\"codeSample\">objSelection.Find.Execute wdReplaceAll\n<\/PRE>\n<P>That would make the constant ReplaceAll (which tells Word we want to replace all instances of our search text) the first parameter passed to the Execute method, which would cause the script to fail. Hence all the \u201cempty\u201d parameters represented by the commas. And don\u2019t worry: if that doesn\u2019t make any sense to you, take a look at the documentation for the <A href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa142035(office.10).aspx\" target=\"_blank\"><B>Execute<\/B><\/A> method and you\u2019ll see a list of all the parameters we skipped.<\/P>\n<P>OK. That was fun, wasn\u2019t it? In fact, we\u2019d love to stay and chat about it some more, but Abe Lincoln and Genghis Khan are both out of iced tea. You know, writing a script to bring the dead back to life really wasn\u2019t all it\u2019s cracked up to be \u2026.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I boldface a specific word throughout a Microsoft Word document?&#8212; SB Hey, SB. You\u2019ll probably find this hard to believe, but when you work with scripts and scripting all day you tend to get a little jaded. You say you have a script that automatically backs up and clears all [&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-68053","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 boldface a specific word throughout a Microsoft Word document?&#8212; SB Hey, SB. You\u2019ll probably find this hard to believe, but when you work with scripts and scripting all day you tend to get a little jaded. You say you have a script that automatically backs up and clears all [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68053","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=68053"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68053\/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=68053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}