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.

Putting a Border Around a Paragraph in Microsoft Word

In one of our columns last week we showed you how to put a border around cells in a Microsoft Excel spreadsheet. There’s nothing wrong with that, but no doubt some of you were a little disappointed: “Gee, I was hoping they’d show us how to put a border around a paragraph in Microsoft Word. But I guess now that they’ve done one column on putting borders around things it will be a long time before they do a column on putting borders around something else.”

You know, if the Scripting Guys were hard workers that might be true; however, you rarely hear “Scripting Guys,” “hard” and “work” in the same sentence. (Unless it’s a sentence like this: “How hard could it be to get the Scripting Guys to do some work?”) Instead, we’re always looking for ways to do the same thing over and over again. Was the first Scriptomatic a success? Great; let’s write another one. Were we able to write one column about putting borders around things? Well, then why not do the same thing all over again, this time using Microsoft Word:

Const wdLineStyleSingle = 1
Const wdLineStyleNone = 0

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

i = 1

objDoc.Paragraphs(i).Range.Select
objSelection.ParagraphFormat.Borders.OutsideLineStyle = wdLineStyleSingle
objSelection.ParagraphFormat.Borders.Shadow = TRUE
objSelection.TypeText "This is the first paragraph."
objSelection.TypeParagraph()
i = i + 1

objDoc.Paragraphs(i).Range.Select
objSelection.ParagraphFormat.Borders.OutsideLineStyle = wdLineStyleNone
objSelection.TypeText "This is the second paragraph."
objSelection.TypeParagraph()
i = i + 1

objSelection.TypeText "This is the third paragraph."
objSelection.TypeParagraph()
i = i + 1

OK, so what’s going on with this script? Well, we begin by defining a pair of constants – wdLineStyleSingle and wdLineStyleNone – that we’ll use later on to specify the type of border we want to put around our paragraph. We then create an instance of the Word.Application object and set the Visible property to True. We use the Add method to create a new document, and then use this line of code to create an instance of the Word Selection object:

Set objSelection = objWord.Selection

You’re right: that is way more work than we’re used to doing. And all that does is give us a blank Word document; we still have to create some paragraphs and put a border around them.

Let’s see what we can do about that. To begin with, we assign the value 1 to a counter variable named i. We’re going to use this variable to keep track of the paragraph number we’re working with. To put a border around a paragraph we need to first select that paragraph, and a very easy way to do that is to reference the paragraph number within the Paragraphs collection. As you might expect, the first paragraph in the document is paragraph number 1, the second paragraph is paragraph number 2, and so on. Each time we create a new paragraph we’ll increment i by 1; that will help us keep track of exactly which paragraph we’re working on.

To select a specific paragraph we use code like this:

objDoc.Paragraphs(i).Range.Select

At the moment, of course, i is equal to 1, so we’re selecting the first paragraph in the document.

Once we’ve selected the paragraph we can then start configuring border properties. Unless we specify otherwise (and we’ll discuss that later), our border will put a box around the entire paragraph. To make a single-line box we use this line of code, which tells the script to configure the OutsideLineStyle property:

objSelection.ParagraphFormat.Borders.OutsideLineStyle = wdLineStyleSingle

You’re right: there’s one of those constants we defined at the beginning of the script. The constant wdLineStyleSingle (with a value of 1) gives us a plain old straight line. What if we wanted to use a different line style? Then we could use one of the constants (and values) shown in the following table:

Constant

Value

wdLineStyleDashDot

5

wdLineStyleDashDotDot

6

wdLineStyleDashDotStroked

20

wdLineStyleDashLargeGap

4

wdLineStyleDashSmallGap

3

wdLineStyleDot

2

wdLineStyleDouble

7

wdLineStyleDoubleWavy

19

wdLineStyleEmboss3D

21

wdLineStyleEngrave3D

22

wdLineStyleInset

24

wdLineStyleNone

0

wdLineStyleOutset

23

wdLineStyleSingle

1

wdLineStyleSingleWavy

18

wdLineStyleThickThinLargeGap

16

wdLineStyleThickThinMedGap

13

wdLineStyleThickThinSmallGap

10

wdLineStyleThinThickLargeGap

15

wdLineStyleThinThickMedGap

12

wdLineStyleThinThickSmallGap

9

wdLineStyleThinThickThinLargeGap

17

wdLineStyleThinThickThinMedGap

14

wdLineStyleThinThickThinSmallGap

11

wdLineStyleTriple

8

And then, just for the heck of it, we set the Shadow property to True; as you might expect, that creates a fancy shadow box around our text.

Note. We should mention that each of the outside line border properties has an inside line counterpart; in addition to OutsideLineStyle there is a corresponding InsideLineStyle property.

After that we simply type in some text using the TypeText method, then simulate pressing the ENTER key (thus marking the end of paragraph 1) by using the TypeParagraph method. At that point in time we’ll have a document that looks something like this:

Microsoft Word

Next we increment our variable i by 1. (Why? Because now we’re working on paragraph 2.) By default Word carries paragraph formatting from one paragraph to the next. We don’t want a border around paragraph 2, so we select the paragraph and use this line of code to the set the line style to None; that means this paragraph will not have a border:

objSelection.ParagraphFormat.Borders.OutsideLineStyle = wdLineStyleNone

We then type some more text into the document and call it good. When we run the script, we’ll end up with a Word document that looks like this:

Microsoft Word

Hey, if you think it looks good here, wait till you see it in real life.

And, yes, there are other properties we can set when it comes to creating borders. For example, the OutsideLineWidth property lets us specify the thickness of a border line:

Constant

Value

wdLineWidth025pt

2

wdLineWidth050pt

4

wdLineWidth075pt

6

wdLineWidth100pt

8

wdLineWidth150pt

12

wdLineWidth225pt

18

wdLineWidth300pt

24

wdLineWidth450pt

36

wdLineWidth600pt

48

Likewise, the OutsideLineColorIndex property gives us the ability to put colored borders around a paragraph:

Constant

Value

wdAuto

0

wdBlack

1

wdBlue

2

wdBrightGreen

4

wdByAuthor

-1

wdDarkBlue

9

wdDarkRed

13

wdDarkYellow

14

wdGray25

16

wdGray50

15

wdGreen

11

wdNoHighlight

0

wdPink

5

wdRed

6

wdTeal

10

wdTurquoise

3

wdViolet

12

wdWhite

8

wdYellow

7

We know how anxious you are to see these additional properties in action, so here’s a modified script that puts a thick bright green border around the first paragraph:

Const wdLineStyleSingle = 1
Const wdLineStyleNone = 0
Const wdBrightGreen = 4
Const wdLineWidth300pt = 24

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

i = 1

objSelection.ParagraphFormat.Borders.OutsideLineStyle = wdLineStyleSingle
objSelection.ParagraphFormat.Borders.OutsideLineWidth = wdLineWidth300pt
objSelection.ParagraphFormat.Borders.OutsideColorIndex = wdBrightGreen
objSelection.TypeText "This is the first paragraph."
objSelection.TypeParagraph()
i = i + 1

objDoc.Paragraphs(i).Range.Select
objSelection.ParagraphFormat.Borders.OutsideLineStyle = wdLineStyleNone
objSelection.TypeText "This is the second paragraph."
objSelection.TypeParagraph()
i = i + 1

objSelection.TypeText "This is the third paragraph."
objSelection.TypeParagraph()
i = i + 1

And here’s what we get after running the script:

Microsoft Word

Good question: how did you guys ever do your jobs before the Office Space column came along?

One final thing we should mention (in light of the fact that we said we would mention it) is that you are not limited to putting a box around a paragraph; if you prefer, you can put a border above a paragraph, below a paragraph, or off to either side (or any combination thereof). To do that, you need to use one (or more) of the following constants in your script:

Constant

Value

wdBorderBottom

-3

wdBorderLeft

-2

wdBorderRight

-4

wdBorderTop

-1

But where do you actually use these constants? Well, what you need to do is define one of the constants and then use if to indicate which border you want to configure. For example, to put a line only at the top border of a paragraph use this code:

objSelection.ParagraphFormat.Borders(wdBorderTop).LineStyle = wdLineStyleSingle

Note. When working with an individual border you work with properties such as LineStyle and LineWidth; you don’t use OutsideLineStyle or InsideLineWidth.

For example, here’s a script that puts a thick bright green border on top (but only on top) of the first paragraph in a document:

Const wdLineStyleSingle = 1
Const wdLineStyleNone = 0
Const wdBrightGreen = 4
Const wdLineWidth300pt = 24
Const wdBorderTop = -1

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

i = 1

objSelection.ParagraphFormat.Borders(wdBorderTop).LineStyle = wdLineStyleSingle
objSelection.ParagraphFormat.Borders(wdBorderTop).LineWidth = wdLineWidth300pt
objSelection.ParagraphFormat.Borders(wdBorderTop).ColorIndex = wdBrightGreen
objSelection.TypeText "This is the first paragraph."
objSelection.TypeParagraph()
i = i + 1

objDoc.Paragraphs(i).Range.Select
objSelection.ParagraphFormat.Borders.OutsideLineStyle = wdLineStyleNone
objSelection.TypeText "This is the second paragraph."
objSelection.TypeParagraph()
i = i + 1

objSelection.TypeText "This is the third paragraph."
objSelection.TypeParagraph()
i = i + 1

And here’s what the finished product looks like:

Microsoft Word

Yes, very cool; we can hardly wait until our next column on putting borders around things. (We just have to figure out what else we can put a border around.)