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.

Creating Multi-Column Documents in Microsoft Word

Considering the fact that the Scripting Guys once wrote a 1,300-page introductory book on system administration scripting, you might find it hard to believe that we care about the environment and about preserving trees and other natural resources. (We do care, but you know how it goes: you write 2 pages on scripting and before you know it you’ve written 1,300 pages on scripting.) We’re not naïve enough to believe that the paperless office will be here any time soon, but we do believe in trying to cut down on wasted space – and, by extension, wasted paper – whenever possible.

For better or worse, Microsoft Word documents are notorious for using up more paper than they need to. (It’s not Word’s fault, mind you, it’s just that people tend to waste space when creating any kind of word processing document.) For example, here’s a script that grabs the names of all the fonts installed on a computer and then writes those font names to a Word document:

Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add()

Set objSelection = objWord.Selection

For Each strFont in objWord.FontNames
    objSelection.TypeText strFont
    objSelection.TypeParagraph()
Next

When you run the script, you get something that looks like this:

Microsoft Word

What’s wrong with this? Well, nothing…unless, of course, you’re planning to print this document. In that case, there’s a lot of wasted space: there’s more than enough room to have two columns of font names on each page. With a two-column document you’d get the exact same results, but the document would use half as many pages when printed. In the greater scheme of things this might be small potatoes, but if you can print something out on two sheets of paper rather than four, well, why not?

We know; you’re skeptical: “That’s great, Scripting Guys, and we’re all for cutting down on paper use, too. But I generated this report using a script. How am I supposed to configure a two-column format using a script?”

Well, here’s one suggestion:

Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add()

objDoc.PageSetup.TextColumns.SetCount(2)
objDoc.PageSetup.TextColumns.LineBetween = True

Set objSelection = objWord.Selection

For Each strFont in objWord.FontNames
    objSelection.TypeText strFont
    objSelection.TypeParagraph()
Next

Let’s talk about this script and what it does. We begin by creating an instance of the Word.Application object and then setting the Visible property to True; that gives us an instance of Word that we can see onscreen. After that we use the Add method to create a new, blank document:

Set objDoc = objWord.Documents.Add()

Notice that, when we do this, we create an object reference named objDoc. We can then use this object reference to do all sorts of cool things, such as configure page setup properties for this particular document.

And that’s exactly what we do with these two lines of code:

objDoc.PageSetup.TextColumns.SetCount(2)
objDoc.PageSetup.TextColumns.LineBetween = True

In the first line, we use the SetCount method to set the number of columns for the current document to two. What if we wanted to have a three column document? Then we’d just pass the value 3 to the SetCount method:

objDoc.PageSetup.TextColumns.SetCount(3)

That one line of code is all we really need; that gives us our multi-column document. However, just for the heck of it, we then set the value of the LineBetween property to True (by default, LineBetween is False). That puts a dividing line between the columns, making it easier to distinguish between column 1 and column 2.

After that the rest of the script simply grabs the font names and types them into the document. We won’t discuss that code today; for more information, see this previous Office Space column on using Word to retrieve font information.

When we run our modified script we end up with a document similar to this:

Microsoft Word

Two columns per page, meaning we’re using half as much paper. To the trees of the world: you’re welcome.

One thing we should add is that the two columns will be visible as two columns only if you’re in Print Layout view or Print Preview; if Word happens to be in Normal view you’ll have two columns but it will appear as though you have only one column. (That’s just the way things work in Normal view; switch to Print Layout and everything will look fine.) Therefore, as an added bonus we’ve modified the script to ensure that the document is shown in Print Layout view. Here’s the modified script:

Const wdPrintView = 3

Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add()

objDoc.PageSetup.TextColumns.SetCount(2)
objDoc.PageSetup.TextColumns.LineBetween = TRUE

Set objSelection = objWord.Selection

For Each strFont in objWord.FontNames
    objSelection.TypeText strFont
    objSelection.TypeParagraph()
Next

Set objView = objDoc.ActiveWindow.View
objView.Type = wdPrintView

All we’ve done here is add a new constant – wdPrintView – and set the value to 3. We’ve then tacked these two lines of code on to the end of the script:

Set objView = objDoc.ActiveWindow.View
objView.Type = wdPrintView

Line 1 creates an instance of the View object for the current document, and line 2 sets the view Type to Print Layout. That’s all you have to do. If we wanted a different view we could have configured the Type property using any of these constants and their values:

Constant

Value

wdMasterView

5

wdNormalView

1

wdOutlineView

2

wdPrintPreview

4

wdPrintView

3

wdReadingView

7

wdWebView

6

Like we said, it might save only a few sheets of paper, but every little bit helps. As for the Scripting Guys, we’ve dramatically cut down on paper use by deciding not to do any more work. Oddly enough, no one has been able to tell the difference….