Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I add a line between the existing paragraphs in a Word document?

-- GB

SpacerHey, Scripting Guy! AnswerScript Center

Hey, GB. You’ll have to forgive us if we seem a little distracted this morning; shocking as this might seem, the Scripting Guy who writes this column may have made a monumental mistake. In yesterday’s column he joked about an email he received that told him that he had won the “Spainsh” lottery. That sounded like a classic Internet scam, and he treated it as such.

But maybe he shouldn’t have done that. After all, the same day that column was written the same Scripting Guy who made fun of the Spanish lottery received a letter in the mail inviting him to join “upscale people such as yourself” in a private jet club, a club where membership entitles you to a certain number of hours per year flitting about the world in a private jet. “Frequent flyers such as you can surely appreciate the benefits of a private jet over commercial air travel,” noted the letter. As if they even had to ask a question like that!

Note. In case you’re wondering, the most airplane trips the Scripting Guy who writes this column has ever taken in a single year is three: one to Pasadena to watch the Huskies beat Purdue in the Rose Bowl; one to Hawaii for a family vacation; and one to Orlando to speak at a conference. This year he hasn’t flown in a plane at all. But still, he probably does qualify as a frequent flyer, at least compared to, say, Charlemagne or King Henry the VIII (or the Scripting Editor).

At first the Scripting Guy who writes this column figured this was just junk mail; high-class junk mail, perhaps, but junk mail nonetheless. But then he got to thinking: how did this private jet company get his name and address? Why did they keep referring to his “lavish lifestyle” and appreciation for the finer things in life? How did they know that his time is far more valuable than mere money?

As you might expect, he then began to worry. After all, what if the Spanish lottery routinely forwards the names of all their winners to private jet clubs and butler placement agencies and the like? What if the Scripting Guy who writes this column really did win one million euros in the Spanish lottery only to throw it all away by making fun of the lottery folks?

That should be a fun thing to go home and try to explain to the family.

But hey: easy come, easy go. Besides, at his current salary, and depending on the exchange rate, it should only take him 300 years or so to make a million euros anyway.

Well, assuming, of course, that his next few performance reviews go a bit better than his last one did.

The bottom line is this: having thrown away a million euros, it looks as though the Scripting Guy who writes this column will have to continue writing this column for a little longer. (But probably just for a few months; as he noted yesterday, he wins the Spanish lottery on a fairly regular basis.) That’s bad news for the Scripting Guy who writes this column, but it’s good news for you, GB; after all, you get a script that can add a line between existing paragraphs in a Word document:

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

Set objDoc = objWord.Documents.Open("C:\Scripts\Test.doc")

Set colParagraphs = objDoc.Paragraphs
colParagraphs.LineUnitAfter = 1

To begin with, we should note that there are probably a couple different ways we could have approached this problem. For example, we could have done a search-and-replace and simply replaced all paragraph returns with two paragraph returns. (For more information on writing search-and-replace scripts, see this previous Hey, Scripting Guy! column.) In the end we decided to modify the paragraph format settings so that an additional line would be automatically tacked on to the end of each paragraph in the document. That’s an approach we haven’t used before and, as you just saw, it doesn’t require us to write very much code. (Needless to say, we don’t get paid by the word.)

We should also point out, GB, that we’re assuming you have a Word document that looks something like this:

Microsoft Word


As you can see, the paragraphs are all jammed together. We want to space things out a bit by adding a blank line between each of these paragraphs.

To do that we start out by creating an instance of the Word.Application object and then setting the Visible property to True; that gives us a running instance of Microsoft Word that we can see onscreen. We then use this line of code to open the document C:\Scripts\Test.doc:

Set objDoc = objWord.Documents.Open("C:\Scripts\Test.doc")

Believe it or not, that’s the hard part. As soon as the document is open we can use this line of code to retrieve a collection of all the paragraphs in Test.doc:

Set colParagraphs = objDoc.Paragraphs

From there we simply set the value of the LineUnitAfter property to 1, which causes 1 blank line to appear after each paragraph in the document:

colParagraphs.LineUnitAfter = 1

Just that easy, just that quick.

But, you interject, what if we wanted 2 blank lines to appear between each paragraph in the document? That’s no problem; one way to do that would be to set the LineUnitAfter property to 2:

colParagraphs.LineUnitAfter = 2

Etc., etc. Alternatively, we could also assign a value to the LineUnitBefore property, which would cause a blank line to automatically appear before each paragraph. Whatever floats your boat, right?

Well, at least for those of you who have a boat.

In case you’re wondering, here’s what Test.doc looks like after we run our script:

Microsoft Word


Meanwhile, we’re still debating whether we should go ahead and join the private jet club anyway, Spanish lottery or no Spanish lottery. The letter we received didn’t mention the actual price of a membership, which is usually a bad sign. On the other hand, though, the offer did include a coupon for $250 off the in-flight catering for our first flight. As you can imagine, it’s awfully hard to pass up an offer like that. (To tell you the truth, we’re not convinced we could even eat $250 worth of food during a single flight. But we’re willing to give it a try.)