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:

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:

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.

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:

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:

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:

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: