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 Line Numbers to a Microsoft Word Document

Hey, glad to see you all here today. You know, the Scripting Guy who writes the Office Space column just flew in from Europe, and boy are his arms tired!

No, not because he literally flew in from Europe; he took a plane like everyone else. His arms are tired because he’s been frantically typing away, trying to make sure there would be an Office Space column for today. After all, you know the old saying: a day without an Office Space column is like a Monday, Wednesday, Friday, Saturday, Sunday, or any other day that we don’t publish an Office Space column.

Anyway, despite a combination of jet lag, reluctance to return to work, and sheer laziness, we’ve managed to put together a column for you today.

Editor’s Note: The rest of us Scripting Guys, who were left here to do all his work while he was out gallivanting around Europe, are not feeling especially sorry for this particular Scripting Guy. We hope you aren’t either.

Today’s column addresses an issue we Scripting Guys run into all the time: whether or not we should number individual lines in a document. We run into that a lot when it comes to code samples, and it’s not necessarily an easy decision to make. If we number each line then it’s easy to refer to a specific line of code: just tell people to look at, say, line 35. On the other hand, if we number each line, then we also have to provide ways to deal with the copying-and-pasting issue; that is, how can we make sure that whenever people copy the code samples they get only the code and not the line numbers? As you might expect, a script like this – replete with line numbers – won’t work too well:

1 For I = 1 to 5
2      Wscript.Echo i
3 Next

No doubt you’ve run into similar issues when generating reports in Microsoft Word: line numbers make your reports easier to read, but you typically don’t want those line numbers to actually be part of the report. How are you supposed to deal with that?

By the way, a numbered list is not the answer. Create a numbered list in Word, copy that list, then paste the list into Notepad. Here’s what you get:

1.Line 1
2.Line 2
3.Line 3
4.Line 4

Nice. Except that this is what we wanted to get:

Line 1
Line 2
Line 3
Line 4

A numbered list is not the answer.

Oh, well. The truth is, we’re probably asking for too much anyway. After all, we want line numbers to show up when we look at or print the document, yet we don’t want those line numbers to actually be part of the document. Even Microsoft Word can’t work miracles like that.

Oh ye of little faith:

On Error Resume Next

Const wdPrintView = 3

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

objDoc.PageSetup.LineNumbering.Active = TRUE
objDoc.PageSetup.LineNumbering.StartingNumber = 1
objDoc.PageSetup.LineNumbering.CountBy = 1
objDoc.PageSetup.LineNumbering.RestartMode = 0

Set objSelection = objWord.Selection

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

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

As it turns out, Word does have the ability to display line numbers, line numbers that aren’t really embedded in the document itself. Go to the File menu and click Page Setup, and then – on the Layout tab – click Line Numbers. You should see a dialog box that looks like this:

Microsoft Word

As you probably guessed, the script we just showed you replicates the capabilities of this dialog box. And here’s how.

Our script starts off by defining a constant named wdPrintView and setting the value to 3; we’ll use this constant later on to switch Word’s View mode to Print Layout. That’s important, too: if Word is in Normal mode, line numbering will be applied but it will not be visible on screen. To make sure we can see the effects of the script, we’ll switch Word into Print Layout view.

After creating an instance of the Word.Application object, setting the Visible property to True, and creating a new, blank document to work with we next encounter these lines of code:

objDoc.PageSetup.LineNumbering.Active = TRUE
objDoc.PageSetup.LineNumbering.StartingNumber = 1
objDoc.PageSetup.LineNumbering.CountBy = 1
objDoc.PageSetup.LineNumbering.RestartMode = 0

What we’re doing here is working with the LineNumbering object (which is a child object of the PageSetup object, which, in turn, is a child object of the Document object, which begat…). As you can see, all we’re doing is assigning values to four different properties of the LineNumbering object:

Property

Description

Active

This property simply turns line numbering on and off. Set the value to True to use line numbering; set the value to False to turn off line numbering.

And some people still think scripting is hard.

StartingNumber

Now this is really easy: the StartingNumber property simply indicates which number we want to start counting with. We want the first line in our document to be line 1, so we set StartingNumber to 1. Suppose we wanted to start with line number 1000. OK:

objDoc.PageSetup.LineNumbering.StartingNumber = 1000

CountBy

CountBy determines which lines will actually display a line number. We want a line number beside every line, so we set CountBy to 1. But suppose we wanted to count every line, but only place a number beside lines 1, 5, 10, 15, etc. No problem:

objDoc.PageSetup.LineNumbering.CountBy = 5

If you only want a number displayed beside every 20 lines, then set CountBy to 20. Play around with this a little and you’ll quickly catch on to how it works.

RestartMode

We want the first line in our document to have the number 1 and the last line in our document to be number whatever. In other words, we want the numbering to be continuous; it should never start over again. Therefore, we set the value of the RestartMode to 0 (which means, “Don’t ever restart”).

There are two other mode available. Suppose we want to restart numbering on each new page; that is, the first line of every new page will have the number 1. In that case, set the value of RestartMode to 2. If you’d like Word to restart numbering each time it encounters a new section in the document, then set RestartMode to 1.

After configuring our line numbering options we then run some code that retrieves a list of all the installed fonts on the computer and types them into the document, one font per line. We do that simply so we’ll have some text in our document. We then use these two lines of code to switch Word into Print Layout view:

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

And what do we get for all this effort? Why, we get a document that looks like this:

Microsoft Word

But here’s the best part. Copy some of that text and paste it into Notepad:

Times New Roman
Arial
Courier New
Symbol
Helvetica

No line numbers! The line numbers appear in Print Layout view and when we print the document, but they aren’t part of the document text.

Yes, very cool. Or as they say in Europe … um, very … cool ….