Share via


Office Space: Tips and Tricks for Scripting Microsoft Office Applications

Office Space

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 Picture Bullets to a Microsoft Word Document

As Scripting Guys we have taken a sacred oath to always provide script writers with useful information, information that will help these script writers carry out their system administration tasks. For the most part we abide by that oath; after all, being a Scripting Guy is not the sort of thing you take lightly. Nevertheless, every now and then we stumble upon something we think is just plain cool. And while it’s fair to question how practical this new thing might be for system administrators, some things are just hard to resist. Even for a Scripting Guy.

For example, take bulleted lists in Microsoft Word. Knowing how to programmatically create bulleted lists is actually quite useful for system administrators; after all, there isn’t a system administrator alive who doesn’t have to prepare reports and who doesn’t use bulleted lists somewhere in those reports. Because we all took the Scripting Guys oath, we were duty-bound to show you how to programmatically add bulleted lists to a Microsoft Word document, something we did in a previous Office Space column.

If you run the script we showed you in that previous column you’ll get a bulleted list that looks something like this:

Microsoft Word

Nice, but a trifle boring, especially after you’ve created hundreds of bulleted lists that all look the same. At some point you’re bound to think, “Wow, I wish I could create a different kind of bulleted list. You know, maybe one that uses pictures for the bullets.”

In other words, something like this:

Microsoft Word

As it turns out, it’s pretty easy to use picture bullets in Microsoft Word: just bring up the Bullets and Numbering dialog box, then, on the Bulleted tab, click Customize and go from there. But what if you want to create picture bullets using a script? That’s probably impossible, isn’t it?

Oh, ye of little faith. You want to create picture bullets using a script? All you had to do was ask:

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()

objSelection.TypeText "Item 1"
objSelection.TypeParagraph()
objSelection.TypeText "Item 2"
objSelection.TypeParagraph()
objSelection.TypeText "Item 3"
objSelection.TypeParagraph()
objSelection.TypeText "Item 4"
objSelection.TypeParagraph()
objSelection.TypeText "Item 5"
objSelection.TypeParagraph()
objSelection.TypeParagraph()
objSelection.TypeText "No longer in a bulleted list."

Set objRange = objDoc.Range(objDoc.Paragraphs(3).Range.Start, objDoc.Paragraphs(7).Range.End)
objSelection.InlineShapes.AddPictureBullet _
    "C:\Program Files\Microsoft Office\MEDIA\OFFICE11\BULLETS\BD21301_.GIF", objRange

Sure, this script looks a little complicated, but most of the code is used simply to type in text and thus give us something to create a bulleted list out of (take a look at all the TypeText and TypeParagraphs sprinkled throughout the script.) After creating an instance of the Word.Application object we set the Visible property to True (so we can see Word onscreen), then use the Add method to add a new, blank document to our Documents collection. As soon as we have a document to work with, we create an instance of the word Selection object, and then just type in a bunch of text.

After doing all that we’ll have a Word document that looks something like this:

Microsoft Word

You’re right: that’s even blander than a bulleted list. But that’s because we haven’t gotten to this block of code, the part where we actually turn our bland and boring list into something much cooler:

Set objRange = objDoc.Range(objDoc.Paragraphs(3).Range.Start, objDoc.Paragraphs(7).Range.End)
objSelection.InlineShapes.AddPictureBullet _
    "C:\Program Files\Microsoft Office\MEDIA\OFFICE11\BULLETS\BD21301_.GIF", objRange

We’re doing two things here. To begin with, we’re creating an instance of the Range object, and we’re specifying that the range will start with the third paragraph in the document and end after the seventh paragraph in the document. That’s what the two parameters are for. Our first parameter indicates the starting point for the range (paragraph 3):

objDoc.Paragraphs(3).Range.Start

And our second parameter indicates the end point for the range:

objDoc.Paragraphs(7).Range.End

When you look at it that way, it actually makes sense, doesn’t it?

After that all we have to do is reference the InlineShapes collection and call the AddPictureBullet method:

objSelection.InlineShapes.AddPictureBullet _
    "C:\Program Files\Microsoft Office\MEDIA\OFFICE11\BULLETS\BD21301_.GIF", objRange

As you can see we pass AddPictureBullet two parameters as well: the path to the image file we’re using as a bullet (C:\Program Files\Microsoft Office\MEDIA\OFFICE11\BULLETS\BD21301_.GIF) and the object reference to the range object we just created (objRange). That’s all we have to do; AddPictureBullet will take it from there, converting our boring list of paragraphs into a way cool bulleted list that uses pictures as the individual bullets.

Like we said this might not be the most practical and utilitarian script ever created. But, hey, all work and no play, right? If you can find a practical use for this, that’s great, but if all it does is make your reports a little cooler and a little more fun, well, there’s nothing wrong with that, either. (Hey, if that wasn’t true we couldn’t say it; our Scripting Guys oath forbids lying to our readers!)