Office Space: Tips and Tricks for Scripting Microsoft Office Applications
Welcome to Office Space, the column that offers tips and tricks for scripting Microsoft® Office applications. We’ll post new tips every Tuesday and Thursday; to see an archive of previous tips, visit the Office Space Archive. And if you have particular questions about Microsoft Office scripting, feel free to send them to email@example.com (in English, if possible). We can’t promise to answer all the questions we receive, but we’ll do our best.
Applying a Theme to a Microsoft Word Document
For the most part, we Scripting Guys are really the Bottom-Line Guys: our primary focus is on finding ways to help you get your job done as quickly and as efficiently as possible. If you’ve ever wondered why our scripts don’t include a lot of fancy error-handling or why our scripts don’t usually accept command-line arguments, that’s the reason right there: without downplaying the importance of, say, error-handling, a bunch of error-handling code can also distract you from the main point of the script, the part that actually backs up an event log or stops a service. From an educational standpoint, the simpler the script the better.
In other words, in the academic world a barebones script that merely echoes data back to the command window is perfect. In the real world, however, that isn’t always the case. Not only do you need to generate reports in the real world, but you also need to generate reports that are both professional-looking and eye-catching. (As everyone knows, books typically are judged by their covers.) Because of that, we Scripting Guys are always looking for ways to have our cake and eat it, too.
Oh, and we’re also looking for ways to quickly and easily create professional-looking reports and documents.
And we think we’ve found a very good way to quickly and easily create professional-looking reports and documents: applying themes to Microsoft Word documents. As you’re about to see, with very little effort you can take a drab-looking document like this one:
And turn it into something a little more attention-grabbing:
Best of all, to do this you just need is a script similar to this one:
Const wdStyleHeading1 = -2 Const wdStyleNormal = -1 Set objWord = CreateObject("Word.Application") objWord.Visible = True Set objDoc = objWord.Documents.Add() objDoc.ApplyTheme "water 111" Set objSelection = objWord.Selection i = 1 objDoc.Paragraphs(i).Range.Select objSelection.ParagraphFormat.Style = wdStyleHeading1 objSelection.TypeText "This is the Title Line" objSelection.TypeParagraph() i = i + 1 objDoc.Paragraphs(i).Range.Select objSelection.ParagraphFormat.Style = wdStyleNormal objSelection.TypeText "This is the first paragraph." objSelection.TypeParagraph()
We begin by defining a pair of constants – wdStyleHeading1 and wdStyleNormal – which we’ll use to apply styles to the paragraphs in our document. As you might expect, there are plenty of styles we can use besides these two; for more information take a look at the wdBuiltInStyle documentation in the Microsoft Word VBA Language Reference.
Next we create an instance of the Word.Application object and set the Visible property to True. This gives us an instance of Microsoft Word that’s visible onscreen. We then call the Add() method to start a new, blank document.
And then we use a single line of code to apply a theme to our new document:
objDoc.ApplyTheme "water 111"
To apply a theme we call the aptly-named ApplyTheme method, passing two parameters: the name of the theme we want to apply (water) and an optional value (111) which indicates which theme formatting options to activate. The three digits correspond – in order – to the Vivid Colors, Active Graphics, and Background Image check boxes in the Theme dialog box. If the digit is a 1 the option is enabled; if the digit is a 0 the option is disabled. If this value is not specified a default value of 011 is used: Vivid Colors is disabled, and Active Graphics and Background Image are enabled.
So how did we know that the theme was named “water?” All we did was open up Word, click Format, click Theme, and then take a look at the themes installed on our computer:
After calling the ApplyTheme method our Word document looks like this:
Pretty cool, huh? The rest of the script simply types in a title line and a sample paragraph, just so we can show you what the text will look like in a themed document. To do this we create an instance of the Word Selection object, then assign the value 1 to a counter variable named i; we’ll use this counter variable to keep track of the paragraph we’re working with. For example, with i equal to 1, this code creates a Range object consisting of the first paragraph and sets the style to Heading 1 (using the constant wdStyleHeading1):
objDoc.Paragraphs(i).Range.Select objSelection.ParagraphFormat.Style = wdStyleHeading1
We type in some text and a paragraph return, and then repeat the entire process for paragraph number 2, this time assigning the paragraph the Normal style (wdStyleNormal). When we’re all done, our document looks like this:
Professionally-looking, attention-grabbing, and quick and easy to create. As Microsoft employees we don’t spend a lot of time in the real world, but if we did these are the kinds of reports we’d like to create.