How Can I Apply a Theme to a Microsoft Word Document?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I apply a theme to a Microsoft Word document?

— YP

SpacerHey, Scripting Guy! AnswerScript Center

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 should be in the mid-80s, but ….) The University of Washington men’s basketball team, marked by injuries and inconsistency all season long, finally put it all together and closed the regular season by whipping 17th-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.

Oh: and we’re now 5 games into Major League Baseball’s exhibition season and the Seattle Mariners already have 5 losses. Like we said, everything’s back to normal.

And if everything is 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:

Set objWord = CreateObject(“Word.Application”)
objWord.Visible = True

Set objDoc = objWord.Documents.Add() Set objSelection = objWord.Selection objSelection.TypeText “Here is a bulleted list.” objSelection.TypeParagraph() objSelection.TypeParagraph()

Set objRange = objDoc.Paragraphs(1).Range objRange.Style = “Normal”

Set objRange = objDoc.Paragraphs(3).Range objRange.ListFormat.ApplyBulletDefault

objSelection.TypeText “Item 1” objSelection.TypeParagraph() objSelection.TypeText “Item 2” objSelection.TypeParagraph() objSelection.TypeText “Item 3” objSelection.TypeParagraph()

Set objRange = objDoc.Paragraphs(6).Range objRange.Style = “Normal”

objSelection.TypeParagraph() objSelection.TypeText “No longer in a bulleted list.”

objDoc.ApplyTheme “Balance”

Whatever you do, don’t panic: this script is nowhere near as complicated as it might look. In fact, as you’re 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’s 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.

In other words, don’t panic because there’s no need to panic. The Scripting Guys have everything under control.

Just like we always do.

We start out by using these two lines of code to create a running instance of Microsoft Word that we can see onscreen:

Set objWord = CreateObject(“Word.Application”)
objWord.Visible = True

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 Selection object:

Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection

Note. Why do we need an instance of the Word Selection object? That’s easy: we need the Selection object so we can start typing text into the document.

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’s what all this is for:

objSelection.TypeText “Here is a bulleted list.”
objSelection.TypeParagraph()
objSelection.TypeParagraph()

Set objRange = objDoc.Paragraphs(1).Range objRange.Style = “Normal”

Set objRange = objDoc.Paragraphs(3).Range objRange.ListFormat.ApplyBulletDefault

objSelection.TypeText “Item 1” objSelection.TypeParagraph() objSelection.TypeText “Item 2” objSelection.TypeParagraph() objSelection.TypeText “Item 3” objSelection.TypeParagraph()

Set objRange = objDoc.Paragraphs(6).Range objRange.Style = “Normal”

objSelection.TypeParagraph() objSelection.TypeText “No longer in a bulleted list.”

We’re not going to take the time to explain everything that takes place here; if you’re interested in understanding how this all works (and, in particular, how to add a bulleted list in a Word document), well, that’s what this Office Space article is for. Suffice to say that the preceding code gives us a Word document that looks like this:

Microsoft Word


What’s that? No, this isn’t a boring old Word document, it – well, OK, maybe it is a little boring. But, then again, that’s why we included this final line of code in the script:

objDoc.ApplyTheme “Balance”

All we’re doing here is calling the ApplyTheme method and changing the document theme to Balance. 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 folder, which is not necessarily the name of the theme as seen in the Word UI. For example, in Word, if you click Format and then click Theme you’ll see (depending on your setup) a theme named Blueprint. However, to apply that theme you use this line of code:

objDoc.ApplyTheme “Blueprnt”

We specifyBlueprnt as the theme because that’s the name of the folder where the Blueprint theme is stored.

Note. Good question: where do you find these folders? This can vary depending (among other things) on the version of Office you have, but here’s 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.

Here’s something else to keep in mind. Themes actually have three different formatting options:

Vivid Colors

Active Graphics

Background Image

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:

objDoc.ApplyTheme “Blueprnt 111”

What if you only wanted to enable Active Graphics? In that case you’d set the Active Graphics option value (the second digit) to 1 and the other two digits to 0:

objDoc.ApplyTheme “Blueprnt 010”

If you don’t specify a three-digit value then only Active Graphics and Background Images are enabled. In other words, the default value is equivalent to this:

objDoc.ApplyTheme “Blueprnt 011”

And what will our document look like after we apply the Balance theme? With any luck it will look just like this:

Microsoft Word

What’s that? You say you don’t necessarily want to change the theme that’s been applied to a document, you’d just like to know which theme has been applied to a document? No problem:

Set objWord = CreateObject(“Word.Application”)
objWord.Visible = True

Set objDoc = objWord.Documents.Open(“C:\Scripts\Test.doc”)

Wscript.Echo objDoc.ActiveTheme Wscript.Echo objDoc.ActiveThemeDisplayName

Oh, we almost forgot: here’s something else that’s 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’t 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 – repair the existing viaduct or build a plain old “surface” road – aren’t even on the ballot. But that’s 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.

Just like we said: everything is back to normal.

0 comments

Discussion is closed.

Feedback usabilla icon