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 scripter@microsoft.com (in English, if possible). We can’t promise to answer all the questions we receive, but we’ll do our best.
Adding WordArt to a Microsoft Word Document
If you’ve read our columns or viewed one of our webcasts, you no doubt see the Scripting Guys as serious, no-nonsense types, writers who just like to give you the facts and then call it good. Well, believe it or not the Scripting Guys do have a lighter side; once in a while we like to let our hair down and do something just because it’s kind of fun or kind of cool, and not because it has any real practical value. Hey, all work and no play, right?
Which brings us to today’s Office Space column: adding WordArt to a Microsoft Word document. In the great scheme of system administration life, adding WordArt to a document is probably not as important as backing up file servers or creating user accounts. But that’s all right: sometimes you should do things just because you can, not because it will change your life in any way.
On the other hand, some of that WordArt is pretty cool, so who can really say whether or not it will change your life?
If you aren’t familiar with WordArt, in Microsoft Word go to the Insert menu, point to Picture, and then click WordArt. You be rewarded for that effort by seeing a dialog box like this one:
Note. In this dialog box, the first shape in the first row has an index number of 0, the second shape in the first row has an index number of 1, and so on. Keep an eye on shape 16, which is the fifth shape in row 3; we’ll be using that index number in a minute or two. |
Basically WordArt is a way to add some snazzy-looking text to a document; as a system administrator, you might want to use it to, say, slap an eye-catching cover on a report generated by your script. It’s kind of fun and it might even be worth doing … provided, of course, that adding this WordArt was quick and easy. Well, guess what:
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection
objSelection.TypeParagraph()
Set objShape = objDoc.Shapes.AddTextEffect _
(16, "Script Center", "Arial Black", 54, -1, 0, 30, 30)
That’s all you have to do. The script starts out by creating an instance of the Word.Application object and setting the Visible property to True. We then use the Add method to create a new document to work with.
Following that we create an instance of the Selection object, then use the TypeParagraph() method to enter a blank paragraph return (equivalent to pressing ENTER on the keyboard). Why do we do that? Well, by default our WordArt will be “anchored” to the first paragraph in the document. If that first paragraph contains text and we add more text to the document, the WordArt will drift: as the text moves down the page the WordArt will follow it. As a result, our snazzy-looking title ends up in the middle of the page instead of at the top. An easy way to guard against that is to anchor the WordArt to a blank paragraph, which is exactly what we’ve done.
Now we get to the good stuff:
Set objShape = objDoc.Shapes.AddTextEffect _
(16, "Script Center", "Arial Black", 54, -1, 0, 30, 30)
What we’ve done here is call the AddTextEffect method, a method belonging to the document’s Shapes collection. In addition, we pass AddTextEffect the following parameters:
Parameter |
Description |
16 |
The index number of the WordArt style we want to use. Remember, the fifth shape in row 3. |
“Script Center” |
The text for the WordArt. |
“Arial Black” |
The font type for the WordArt. In general, thicker, bolder fonts work better than thinner fonts. |
54 |
The font size for the WordArt. With WordArt, bigger font sizes tend to display better; with smaller fonts you can’t always see the fancy special effects. |
-1 |
Indicates whether or not the font should be boldfaced. Set the value to -1 for True, 0 for False. |
0 |
Indicates whether or not the font should be italicized. Set the value to -1 for True, 0 for False. |
30 |
The position, measured in points, of the left edge of the WordArt shape relative to the anchor. |
30 |
The position, measured in points, of the top edge of the WordArt shape relative to the anchor. |
And here’s what we get when we run the script:
That’s got to be more fun than backing up file servers!
You might have noticed that when we call the AddTextEffect method we actually create an object reference using the variable name objShape:
Set objShape = objDoc.Shapes.AddTextEffect _
(16, "Script Center", "Arial Black", 54, -1, 0, 30, 30)
We had a good reason for doing that. WordArt is just another shape within a Microsoft Word document; that means we can further modify and manipulate our WordArt after it’s been inserted. (In other words, you aren’t stuck with the 30 styles shown in the WordArt dialog box.) We won’t run through all the possible ways to modify a Word shape, but here’s a cool one you can try. Just add this line of code to the bottom of your script:
objShape.Fill.UserPicture "C:\Scripts\Sunset.jpg"
You should get back a document that looks like this, with the individual letters in Script Center “filled” with the picture Sunset.jpg (assuming you copied Sunset.jpg to your C:\Scripts folder):
You can also use code like this to change the “flow” of the text:
objShape.TextEffect.PresetShape = 9
The number 9 in the preceding line of code represents the index number from the shapes found in this dialog box:
In this dialog box, the first shape in the first row is number 1, the second shape in the first row is number 2, and so on. We’re using shape 9, the first shape in the second row. Give the script a try and see for yourself. Hey, it’ll be fun.