{"id":65313,"date":"2007-03-15T01:42:00","date_gmt":"2007-03-15T01:42:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/03\/15\/how-can-i-replace-a-specified-font-in-a-microsoft-word-document\/"},"modified":"2007-03-15T01:42:00","modified_gmt":"2007-03-15T01:42:00","slug":"how-can-i-replace-a-specified-font-in-a-microsoft-word-document","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-replace-a-specified-font-in-a-microsoft-word-document\/","title":{"rendered":"How Can I Replace a Specified Font in a Microsoft Word Document?"},"content":{"rendered":"<p><H2><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\"> <\/H2>\n<P>Hey, Scripting Guy! We have a folder full of Word documents in which the author had an unfortunate tendency to use a strange font from time-to-time. How can I use a script to go in and replace just that strange font with Arial?<BR><BR>&#8212; Z D-F<\/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, Z D-F. You know, when \u201cdesktop publishing\u201d first came into vogue, the Scripting Guy who writes this column was in graduate school at the University of Washington. As such, he remembers reading several scholarly articles showing how desktop publishing was perhaps the worst thing that ever happened to business, at least from a productivity standpoint. Why? Well, back in the old days, before desktop publishing, anyone who needed to produce a document simply rolled a sheet of paper into a typewriter and typed it up. There was no concern about layout, no fiddling with graphics, and, above all, no debates about which font to use.<\/P>\n<P>The font issue was especially interesting, because it led to more problems than just people wasting time trying different fonts and sizes. That was bad enough in and of itself, but the issue was exacerbated by the fact that most people simply aren\u2019t very good at graphic design. Not only did they waste time messing around with fonts, but more often than not they ended up choosing the <I>wrong<\/I> fonts, thus producing a document that was much harder to read. In other words, productivity was lost not only in writing the documents but also in reading the documents.<\/P>\n<P>Isn\u2019t progress wonderful?<\/P>\n<P>So can scripts help us to regain this lost productivity? Heck no, not unless you can write a script that will go around, bop people on the head, and tell them, \u201cHey, stop using these fonts that no one can read!\u201d But at least a script <I>can<\/I> help us go in and replace crazy fonts with something a little more readable and a little more professional:<\/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.Font.Name = &#8220;Gigi&#8221;\nobjSelection.Find.Forward = TRUE<\/p>\n<p>objSelection.Find.Replacement.Font.Name = &#8220;Arial&#8221;<\/p>\n<p>objSelection.Find.Execute &#8220;&#8221;, ,False, , , , , , , ,wdReplaceAll\n<\/PRE>\n<P>To begin with, Z D-F, we\u2019re assuming that you have a document that looks something like this:<\/P><IMG height=\"285\" alt=\"Microsoft Word\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/newfont1.jpg\" width=\"400\" border=\"0\"> \n<P><BR>As you can see, the main body of the document is written in Times New Roman, which is fine. However, the section headings are written in a font named Gigi; a pretty good argument can be made that fonts with names like Gigi should never be used in business proposals or other formal documents. Because of that what you\u2019d like to do is search this document and replace the Gigi font with Arial. The tricky part, of course, is that you only want to replace Gigi; anything written in Times New Roman should be left alone. Believe it or not, our script does just that.<\/P>\n<P>So <I>how<\/I> does our script do that? Well, to begin with, we define a constant named wdReplaceAll and set the value to 2; we\u2019ll use this constant to tell the script to replace any and all the instances of the Gigi font. We then use this block of code to create a visible instance of the <B>Word.Application<\/B> object, open the document C:\\Scripts\\Test.doc, and then create an instance of the Word <B>Selection<\/B> object:<\/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\n<\/PRE>\n<P>You\u2019re right: that <I>is<\/I> a lot of stuff packed into four lines of code, isn\u2019t it? No wonder we\u2019re so tired this morning!<\/P>\n<P>At this point we\u2019re ready to start having some fun. In order to do a find-and-replace operation in Word we need to assign values to two different objects: the <B>Find<\/B> and <B>Replacement<\/B> objects. With that in mind, we thus use these two lines of code to assign values to the Find object:<\/P><PRE class=\"codeSample\">objSelection.Find.Font.Name = &#8220;Gigi&#8221;\nobjSelection.Find.Forward = TRUE\n<\/PRE>\n<P>Nothing too terribly complicated here. We want to search for all instances of the Gigi font, so in line one we assign the value <I>Gigi<\/I> to the <B>Find.Font.Name<\/B> property. And because we\u2019re starting with the cursor positioned at the beginning of the document we then assign the value <I>True<\/I> to the <B>Find.Forward<\/B> property; this tells the script to move forward through the document when searching (that is, to start at page 1 and continue on through the end of the document).<\/P>\n<P>That\u2019s the only search criteria we need to specify. As you can see, we don\u2019t even specify the text we want to search for; that\u2019s because we don\u2019t care about the text itself. All we\u2019re looking for here is the formatting.<\/P>\n<P>As everyone knows, it takes two to tango; turns out that it also takes two to do a find and replace operation. That means that, having configured the Find object, our next step is to configure the Replacement object. This turns out to very easy; all we need to do is specify that the replacement font is Arial:<\/P><PRE class=\"codeSample\">objSelection.Find.Replacement.Font.Name = &#8220;Arial&#8221;\n<\/PRE>\n<P>In other words, we\u2019ve now told the script to start at the beginning of the document, look for all instances of the Gigi font, and replace all those instances with Arial. But rather than sit here and talk about what our script <I>could<\/I> do why don\u2019t we just go ahead and do it:<\/P><PRE class=\"codeSample\">objSelection.Find.Execute &#8220;&#8221;, ,False, , , , , , , ,wdReplaceAll\n<\/PRE>\n<P>All we\u2019re doing here is calling the <B>Execute<\/B> method, which tells the script to actually go do the find and replace operation. As you can see, we need to pass a crazy-looking set of parameters to this method:<\/P><PRE class=\"codeSample\">&#8220;&#8221;, ,False, , , , , , , ,wdReplaceAll\n<\/PRE>\n<P>What does all that mean? Well, for detailed information about the Execute method and its parameters you should check the <A href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/Aa171990\" target=\"_blank\"><B>Microsoft Word VBA Language Reference<\/B><\/A> on MSDN. For now we\u2019ll note that the very first parameter indicates the text we want to search for. Because we\u2019re searching only for formatting, the Word convention is to use an empty string (\u201c\u201d) to represent the search text. And you know the Scripting Guys: we <I>always<\/I> follow the standard conventions.<\/P>\n<P>The second parameter has to do with case sensitivity. For this script case doesn\u2019t matter; however, it\u2019s crucial that the Execute parameters be passed by position. Admittedly, we aren\u2019t going to use the second parameter. However, we can\u2019t leave this parameter out altogether; if we did then the third parameter, which we <I>do<\/I> need, wouldn\u2019t be the third item in the parameter list. Therefore, we leave the second parameter blank, inserting a blank space followed by a comma to indicate that this represents parameter 2.<\/P>\n<P>And yes, that <I>is<\/I> kind of confusing, isn\u2019t it? Well, suppose we had a parameter list that looked like this:<\/P><PRE class=\"codeSample\">1, 2, 3, 4\n<\/PRE>\n<P>Suppose, further, that some hypothetical method requires four parameters; however, we only need to use parameters 1 and 4. Could we pass this hypothetical method a parameter string that looks like this:<\/P><PRE class=\"codeSample\">1, 4\n<\/PRE>\n<P>No, we can\u2019t; after all, we list only two parameters here, and our method requires four parameters. Because of that we need to pass a parameter string that looks like this, with the blank spaces indicating that parameters 2 and 3 should be left alone:<\/P><PRE class=\"codeSample\">1, , , 4\n<\/PRE>\n<P>It\u2019s the same thing with the Execute method.<\/P>\n<P>Now, where we were? Oh, right. After inserting a blank space for parameter 2 we then set parameter 3 to <I>False<\/I>. The third parameter tells Word to search for whole words; if this is set to True (the default value) the font for certain items (like punctuation marks) might not get changed. We then have a series of blank parameters (again, representing parameters we don\u2019t need for this script). Our parameter list then culminates with the constant wdReplaceAll, which tells Word to replace every instance of the Gigi font. Without <I>this<\/I> parameter Word will find and replace only the first instance of Gigi.<\/P>\n<P>And what are we going to get when we run this script? We\u2019re going to get this:<\/P><IMG height=\"281\" alt=\"Microsoft Word\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/newfont2.jpg\" width=\"400\" border=\"0\"> \n<P><BR>Hasta la vista, Gigi!<\/P>\n<P>Actually you\u2019re right: we shouldn\u2019t gloat. After all, Gigi might be gone, but it\u2019s not forgotten; no doubt this crazy font will turn up again sometime. But that\u2019s all right. We\u2019ll just keep running our script and \u2013 in the true spirit of the modern age \u2013 spend our time undoing everything that our fellow employees spent hours and hours doing in the first place.<\/P>\n<P>Isn\u2019t progress wonderful?<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! We have a folder full of Word documents in which the author had an unfortunate tendency to use a strange font from time-to-time. How can I use a script to go in and replace just that strange font with Arial?&#8212; Z D-F Hey, Z D-F. You know, when \u201cdesktop publishing\u201d first came [&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-65313","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! We have a folder full of Word documents in which the author had an unfortunate tendency to use a strange font from time-to-time. How can I use a script to go in and replace just that strange font with Arial?&#8212; Z D-F Hey, Z D-F. You know, when \u201cdesktop publishing\u201d first came [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65313","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=65313"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65313\/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=65313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}