{"id":66733,"date":"2006-08-08T19:07:00","date_gmt":"2006-08-08T19:07:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/08\/08\/how-can-i-replace-text-in-a-microsoft-word-document\/"},"modified":"2006-08-08T19:07:00","modified_gmt":"2006-08-08T19:07:00","slug":"how-can-i-replace-text-in-a-microsoft-word-document","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-replace-text-in-a-microsoft-word-document\/","title":{"rendered":"How Can I Replace Text in 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 search a Microsoft Word document for all occurrences of the string <B>&lt;computername&gt;<\/B> and replace those occurrences with the actual name of a computer (e.g., <B>atl-ws-01<\/B>)?<BR><BR>&#8212; WC<\/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, WC. And by the way, thanks for your question. Microsoft employees will be getting their yearly performance reviews back sometime in the next few weeks; in a completely unrelated matter, the Scripting Guy who writes this column needs a way to open a Word document and replaces all instances of the word <I>underachieved<\/I> with the word <I>overachieved<\/I>. Your question gives him a chance to practice those search-and-replace skills.<\/P>\n<P>So how <I>do<\/I> you search a Word document for all occurrences of the string <I>&lt;computername&gt;<\/I> and replace it with the actual name of a computer? Here\u2019s how:<\/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;&lt;computername&gt;&#8221;\nobjSelection.Find.Forward = TRUE\nobjSelection.Find.MatchWholeWord = TRUE<\/p>\n<p>objSelection.Find.Replacement.Text = &#8220;atl-ws-01&#8221;<\/p>\n<p>objSelection.Find.Execute ,,,,,,,,,,wdReplaceAll\n<\/PRE>\n<P>As you can see, it\u2019s not a too terribly complicated script (with the possible exception of that weird-looking final line of code). We start out by defining a constant named wdReplaceAll and setting the value to 2; we\u2019ll use this constant to ensure that we replace each and every instance of <I>&lt;computername&gt;<\/I> found in the document. Following that we create an instance of the <B>Word.Application<\/B> object and then \u2013 just so we can watch the excitement unfold onscreen \u2013 we set the <B>Visible<\/B> property to True. Finally, we open the document C:\\Scripts\\Test.doc and use this line of code to create an instance of the <B>Selection<\/B> object (which has the effect of positioning our cursor at the very start of the document):<\/P><PRE class=\"codeSample\">Set objSelection = objWord.Selection\n<\/PRE>\n<P>Got all that? Good. Because now it\u2019s time to roll up our sleeves and get serious.<\/P>\n<TABLE id=\"EBE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Note to managers engaged in yearly performance reviews<\/B>. That, of course, is just a figure of speech. The Scripting Guy who writes this column is always serious, always has his sleeves rolled up, and is always working.<\/P>\n<P>Pretty much.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>As it turns out, the Selection object has a child object named <B>Find<\/B>; this child object is used to find text in a document. (Unlike the Scripting Guy\u2019s own child object, whose purpose in life \u2013 other than spending dad\u2019s money \u2013 remains unknown.) <\/P>\n<P>Before we can start the search, however, we need to configure a few properties of the Find object:<\/P><PRE class=\"codeSample\">objSelection.Find.Text = &#8220;&lt;computername&gt;&#8221;\nobjSelection.Find.Forward = TRUE\nobjSelection.Find.MatchWholeWord = TRUE\n<\/PRE>\n<P>As you can probably guess, the <B>Text<\/B> property represents the text we want to search for. The <B>Forward<\/B> property specifies the direction of the search; we want to go forward, which simply means that we want to start searching from our current location (the beginning of the document) and continue until we reach the end of the document. <B>MatchWholeWord<\/B> means that we want to match only entire words. In this case that\u2019s probably not all that important. However, suppose we were searching for the word <I>cat<\/I>. In that case, if MatchWholeWord was False we\u2019d not only match <I>cat<\/I>, but also <I>cat<\/I>alog, <I>cat<\/I>egory, <I>cat<\/I>acomb, va<I>cat<\/I>ion, and any other word containing the string <I>cat<\/I>. Like we said, in this case it probably doesn\u2019t matter: we don\u2019t know of very many words that have <I>&lt;computername&gt;<\/I> embedded in them. But just to be on the safe side \u2026.<\/P>\n<P>As it turns out, the Find object also has a child object: the <B>Replacement<\/B> object. For our purposes, the Replacement object has only one function: we use it to specify the replacement text. Because we want to replace all instances of <I>&lt;computername&gt;<\/I> with <I>atl-ws-01<\/I> we set the value of the Replacement object\u2019s <B>Text<\/B> property to, well, <I>atl-ws-01<\/I>:<\/P><PRE class=\"codeSample\">objSelection.Find.Replacement.Text = &#8220;atl-ws-01&#8221;\n<\/PRE>\n<P>After all that buildup it takes just one line of code to carry out the search and replace:<\/P><PRE class=\"codeSample\">objSelection.Find.Execute ,,,,,,,,,,wdReplaceAll\n<\/PRE>\n<P>We won\u2019t explain all those commas following the<B> Execute<\/B> method; if you\u2019d like to know more about those take a look at this <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/officetips\/may05\/tips0512.mspx\"><B>Office Space column<\/B><\/A>. Suffice to say that the Execute method takes a number of parameters, each of which must be passed in order. The commas simply represent parameters that we don\u2019t need in this script; however, we must pass blank parameters to ensure that wdReplaceAll appears in the proper spot. And that\u2019s all we have to do.<\/P>\n<P>Or at least all <I>you<\/I> have to do. We still have to modify the script so it replaces <I>underachieved<\/I> with <I>overachieved<\/I>. But you don\u2019t need to worry about that.<\/P>\n<TABLE id=\"EBH\" 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>. The Scripting Editor has suggested we tell people that the Scripting Guy who writes this column has no intention of cheating on his performance review. But that\u2019s easy for her to say: she\u2019s a highly-qualified and highly-competent employee. Unless he cheats, the Scripting Guy who writes this column could only get a passing score in one way: by working hard and doing a good job. What do you suppose the odds are of <I>that<\/I> happening?<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I search a Microsoft Word document for all occurrences of the string &lt;computername&gt; and replace those occurrences with the actual name of a computer (e.g., atl-ws-01)?&#8212; WC Hey, WC. And by the way, thanks for your question. Microsoft employees will be getting their yearly performance reviews back sometime in 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,395],"class_list":["post-66733","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 search a Microsoft Word document for all occurrences of the string &lt;computername&gt; and replace those occurrences with the actual name of a computer (e.g., atl-ws-01)?&#8212; WC Hey, WC. And by the way, thanks for your question. Microsoft employees will be getting their yearly performance reviews back sometime in the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66733","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=66733"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66733\/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=66733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}