Office Space: Tips and Tricks for Scripting Microsoft Office Applications
Welcome to Office Space, the new 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.
Creating a Bulleted List in Microsoft Word
One thing people throughout the world worry about is their legacy. For example, a thousand years from now, how will people remember Microsoft? Most likely it will be like this:
We didn’t invent the bulleted list.
We did popularize it, though.
We can thank (or blame) PowerPoint and Word for that.
Ah, yes, bulleted lists: at Microsoft we have people who even speak in bulleted lists. But while bulleted lists are oft-times the butt of jokes they also serve a useful purpose: they help separate and distinguish important bits of information. When you use Microsoft Word to create reports you probably want to separate and distinguish important bits of information. And that leads us to the subject of today’s column: how can you use a script to create a bulleted list in Microsoft Word?
Let’s take a look:
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() Set objRange = objDoc.Paragraphs(2).Range objRange.ListFormat.ApplyBulletDefault 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() Set objRange = objDoc.Paragraphs(7).Range objRange.Style = "Normal" objSelection.TypeText "No longer in a bulleted list."
To create our bulleted list we begin by creating an instance of the Word.Application object; we then set the Visible property to True, the better to see the action unfold onscreen. We use the Add method to add a new document, and then create an instance of the Selection object at the very beginning of that document.
With setup out of the way we then type a single sentence followed by a paragraph return; that’s what we do with these two lines of code:
objSelection.TypeText "Here is a bulleted list." objSelection.TypeParagraph()
We’re now ready to begin with the second paragraph in our document, which will also mark the start of our bulleted list.
As it turns out, you convert any range of text into a bulleted list simply by referencing the ListFormat property and then calling the ApplyBulletDefault method. We want our bulleted list to begin with paragraph 2 so we use this line of code to create a new range beginning with paragraph 2:
Set objRange = objDoc.Paragraphs(2).Range
Now that we have a Range object to work with (one with an object reference of objRange) we can use a single line of code to apply the bulleted list format:
And then we just start typing. We add the first bulleted item – Item 1 – and then enter a paragraph return. That’s what we do here:
objSelection.TypeText "Item 1" objSelection.TypeParagraph()
We then use similar code to add items 2 through 5. Note that the process of adding items to a bulleted list works pretty much the same way in a script as it does when you type a bulleted list in Word. Suppose you start a bulleted list in Word. You type in the first item and then hit ENTER. Your screen should look something like this:
In other words, the bullet format – by default – carries over to the next paragraph; you don’t have to type item 2 and apply the bullet style and then type item 3 and apply the bullet style and…. Instead, each time you hit ENTER the resulting paragraph will automatically be part of the bulleted list.
The same thing is true with our script. We apply the bulleted list format only to paragraph 2; subsequent paragraphs will automatically inherit this format. In fact, all our paragraphs will be part of the bulleted list unless we remove the bullet format. That’s what we do here:
Set objRange = objDoc.Paragraphs(7).Range objRange.Style = "Normal"
In our script we’re now at paragraph 7. (We’ve hard-coded this value in, but you could implement a counter to keep track of the current paragraph. For an example, see the Office Space column Changing the Background Color of a Paragraph.) Therefore, we create a new range consisting of paragraph 7 and set the Style property to Normal. That resets the paragraph formatting, and the next line of code we type in will not be part of the bulleted list.
Note. Could we have simply set the Style of the paragraph to a bulleted list in the first place rather than calling ApplyBulletDefault? Yes; the one problem with that, however, is that style names for bulleted lists aren’t necessarily the same across templates. All templates have a Normal style, however, and the ApplyBulletDefault method will work in every template as well. This seemed like a more generic solution.
When we run the script we should end up with a Word document that looks like this:
Definitely something to remember us by.