{"id":65373,"date":"2007-03-07T01:10:00","date_gmt":"2007-03-07T01:10:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/03\/07\/how-can-i-apply-a-theme-to-a-microsoft-word-document\/"},"modified":"2007-03-07T01:10:00","modified_gmt":"2007-03-07T01:10:00","slug":"how-can-i-apply-a-theme-to-a-microsoft-word-document","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-apply-a-theme-to-a-microsoft-word-document\/","title":{"rendered":"How Can I Apply a Theme to 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! How can I apply a theme to a Microsoft Word document?<BR><BR>&#8212; YP<\/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, YP. You know, things finally seem to be getting back to normal here in the Seattle area. The weather over this past weekend was in the mid-50s, which is where it should be this time of year. (Actually it <I>should<\/I> be in the mid-80s, but \u2026.) The University of Washington men\u2019s basketball team, marked by injuries and inconsistency all season long, finally put it all together and closed the regular season by whipping 17<SUP>th<\/SUP>-ranked USC and No. 2-rated UCLA. The Seattle Supersonics are complaining that they need a brand-new $500 million arena, just a few years after almost $200 million was spent revamping their current home (Key Arena) to their exact specifications.<\/P>\n<P>Oh: and we\u2019re now 5 games into Major League Baseball\u2019s exhibition season and the Seattle Mariners already have 5 losses. Like we said, everything\u2019s back to normal.<\/P>\n<P>And if everything <I>is<\/I> back to normal then surely the Scripting Guys must have a script that shows you how to apply a theme to a Microsoft Word document. You know, a script just like this one:<\/P><PRE class=\"codeSample\">Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nobjWord.Visible = True<\/p>\n<p>Set objDoc = objWord.Documents.Add()\nSet objSelection = objWord.Selection\nobjSelection.TypeText &#8220;Here is a bulleted list.&#8221;\nobjSelection.TypeParagraph()\nobjSelection.TypeParagraph()<\/p>\n<p>Set objRange = objDoc.Paragraphs(1).Range\nobjRange.Style = &#8220;Normal&#8221;<\/p>\n<p>Set objRange = objDoc.Paragraphs(3).Range\nobjRange.ListFormat.ApplyBulletDefault<\/p>\n<p>objSelection.TypeText &#8220;Item 1&#8221;\nobjSelection.TypeParagraph()\nobjSelection.TypeText &#8220;Item 2&#8221;\nobjSelection.TypeParagraph()\nobjSelection.TypeText &#8220;Item 3&#8221;\nobjSelection.TypeParagraph()<\/p>\n<p>Set objRange = objDoc.Paragraphs(6).Range\nobjRange.Style = &#8220;Normal&#8221;<\/p>\n<p>objSelection.TypeParagraph()\nobjSelection.TypeText &#8220;No longer in a bulleted list.&#8221;<\/p>\n<p>objDoc.ApplyTheme &#8220;Balance&#8221;\n<\/PRE>\n<P>Whatever you do, don\u2019t panic: this script is nowhere near as complicated as it might look. In fact, as you\u2019re about to see, once you have a document up and running you can apply a theme using a single line of code. Obviously we have way more than one line of code in this script, but that\u2019s simply so we can create a document that has some text and a bulleted list; we need those elements to make it obvious the effect our new theme has. <\/P>\n<P>In other words, don\u2019t panic because there\u2019s no need to panic. The Scripting Guys have everything under control. <\/P>\n<P>Just like we always do.<\/P>\n<P>We start out by using these two lines of code to create a running instance of Microsoft Word that we can see onscreen:<\/P><PRE class=\"codeSample\">Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nobjWord.Visible = True\n<\/PRE>\n<P>Once we have Word up and running we then use the following two lines of code to create both a new document and an instance of the Word <B>Selection<\/B> object:<\/P><PRE class=\"codeSample\">Set objDoc = objWord.Documents.Add()\nSet objSelection = objWord.Selection\n<\/PRE>\n<TABLE class=\"dataTable\" id=\"EUD\" 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>. Why do we need an instance of the Word Selection object? That\u2019s easy: we need the Selection object so we can start typing text into the document.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>After that we have a big section of code that types some text, adds a bulleted list, and then tacks a bit more text on to the end of the document; that\u2019s what all this is for:<\/P><PRE class=\"codeSample\">objSelection.TypeText &#8220;Here is a bulleted list.&#8221;\nobjSelection.TypeParagraph()\nobjSelection.TypeParagraph()<\/p>\n<p>Set objRange = objDoc.Paragraphs(1).Range\nobjRange.Style = &#8220;Normal&#8221;<\/p>\n<p>Set objRange = objDoc.Paragraphs(3).Range\nobjRange.ListFormat.ApplyBulletDefault<\/p>\n<p>objSelection.TypeText &#8220;Item 1&#8221;\nobjSelection.TypeParagraph()\nobjSelection.TypeText &#8220;Item 2&#8221;\nobjSelection.TypeParagraph()\nobjSelection.TypeText &#8220;Item 3&#8221;\nobjSelection.TypeParagraph()<\/p>\n<p>Set objRange = objDoc.Paragraphs(6).Range\nobjRange.Style = &#8220;Normal&#8221;<\/p>\n<p>objSelection.TypeParagraph()\nobjSelection.TypeText &#8220;No longer in a bulleted list.&#8221;\n<\/PRE>\n<P>We\u2019re not going to take the time to explain everything that takes place here; if you\u2019re interested in understanding how this all works (and, in particular, how to add a bulleted list in a Word document), well, that\u2019s what <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/officetips\/apr05\/tips0428.mspx\"><B>this Office Space article<\/B><\/A> is for. Suffice to say that the preceding code gives us a Word document that looks like this:<\/P><IMG height=\"398\" alt=\"Microsoft Word\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/theme1.jpg\" width=\"467\" border=\"0\"> \n<P><BR>What\u2019s that? No, this isn\u2019t a boring old Word document, it \u2013 well, OK, maybe it <I>is<\/I> a little boring. But, then again, that\u2019s why we included this final line of code in the script:<\/P><PRE class=\"codeSample\">objDoc.ApplyTheme &#8220;Balance&#8221;\n<\/PRE>\n<P>All we\u2019re doing here is calling the <B>ApplyTheme<\/B> method and changing the document theme to <I>Balance<\/I>. As you can see, calling the ApplyTheme method can be as simple as passing the name of the theme folder to the method. Note that we said you had to pass the name of the theme <I>folder<\/I>, which is not necessarily the name of the theme as seen in the Word UI. For example, in Word, if you click <B>Format<\/B> and then click <B>Theme<\/B> you\u2019ll see (depending on your setup) a theme named <I>Blueprint<\/I>. However, to apply that theme you use this line of code:<\/P><PRE class=\"codeSample\">objDoc.ApplyTheme &#8220;Blueprnt&#8221;\n<\/PRE>\n<P>We specify<B><\/B><I>Blueprnt<\/I> as the theme because that\u2019s the name of the folder where the Blueprint theme is stored.<\/P>\n<TABLE class=\"dataTable\" id=\"EPF\" 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>. Good question: where <I>do<\/I> you find these folders? This can vary depending (among other things) on the version of Office you have, but here\u2019s a good place to start your search: C:\\Program Files\\Common Files\\Microsoft Shared\\THEMES11. Incidentally, you can use themes that are stored in locations other than the default Themes folder; in that case, you just need to specify the complete folder path.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Here\u2019s something else to keep in mind. Themes actually have three different formatting options:<\/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>Vivid Colors<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Active Graphics<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Background Image<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>You can individually enable or disable any of these options by passing the appropriate three-digit value along with the theme folder name. To enable an option, set the appropriate digit to 1; to disable an option, set the appropriate digit to 0. For example, to enable all three options for the Blueprnt theme use this line of code:<\/P><PRE class=\"codeSample\">objDoc.ApplyTheme &#8220;Blueprnt 111&#8221;\n<\/PRE>\n<P>What if you only wanted to enable Active Graphics? In that case you\u2019d set the Active Graphics option value (the second digit) to 1 and the other two digits to 0:<\/P><PRE class=\"codeSample\">objDoc.ApplyTheme &#8220;Blueprnt 010&#8221;\n<\/PRE>\n<P>If you don\u2019t specify a three-digit value then only Active Graphics and Background Images are enabled. In other words, the default value is equivalent to this:<\/P><PRE class=\"codeSample\">objDoc.ApplyTheme &#8220;Blueprnt 011&#8221;\n<\/PRE>\n<P>And what will our document look like after we apply the Balance theme? With any luck it will look just like this:<\/P><IMG height=\"398\" alt=\"Microsoft Word\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/theme2.jpg\" width=\"467\" border=\"0\"> \n<P>What\u2019s that? You say you don\u2019t necessarily want to change the theme that\u2019s been applied to a document, you\u2019d just like to know which theme <I>has<\/I> been applied to a document? No problem:<\/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;)<\/p>\n<p>Wscript.Echo objDoc.ActiveTheme\nWscript.Echo objDoc.ActiveThemeDisplayName\n<\/PRE>\n<P>Oh, we almost forgot: here\u2019s something else that\u2019s going on in Seattle. Right now the city is conducting a special election on replacing the Alaskan Way Viaduct (a much-traveled section of highway). The ballot includes two options: a tunnel (which the state of Washington refuses to pay for) and a new elevated roadway (which the city of Seattle refuses to build). And Seattle being Seattle, voters aren\u2019t asked to choose between one option or the other: instead, they can vote for both options. Everyone can be a winner! Best of all, the two options favored by a lot of people \u2013 repair the existing viaduct or build a plain old \u201csurface\u201d road \u2013 aren\u2019t even on the ballot. But that\u2019s OK; after all, this election (which will cost $1 million to conduct) is only advisory; no one has to pay any attention to the results anyway.<\/P>\n<P>Just like we said: everything is back to normal.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I apply a theme to a Microsoft Word document?&#8212; YP Hey, YP. You know, things finally seem to be getting back to normal here in the Seattle area. The weather over this past weekend was in the mid-50s, which is where it should be this time of year. (Actually it [&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-65373","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 apply a theme to a Microsoft Word document?&#8212; YP Hey, YP. You know, things finally seem to be getting back to normal here in the Seattle area. The weather over this past weekend was in the mid-50s, which is where it should be this time of year. (Actually it [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65373","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=65373"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65373\/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=65373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}