How Can I Insert a Manual Line Break into a Microsoft Word Document?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I insert a manual line break into a Microsoft Word document?

— YR

SpacerHey, Scripting Guy! AnswerScript Center

Hey, YR. Greetings (saluti) from Rome, the Eternal City! You know, according to legend, Rome was founded thousands of years ago by the twins Romulus and Remus, along with their distant cousin (and oldest living Scripting Guy) Peter Costantini. Peter, of course, denies this, inisting that he wasn’t even alive when Romulus and Remus founded the city. That might be true. If so, however, then how does he explain the shirt he always wears, the one that says “I co-founded Rome with Romulus and Remus and all I got was this stupid T-shirt”?

Interesting note. According to the Babelfish online translation service, the word Costantini, when translated from Italian to English, means “Costantini.” Strange but true!

We’ll try to figure out who really founded Rome as soon as we get the chance. Right now, however, it’s time for breakfast. So far, the Scripting Guy who writes this column has found Italian food to be quite good. After just a couple of days he’s already had, among other things:

Bigne, a pastry filled with chocolate or vanilla cream.

Napoleons, a pastry filled with both chocolate and vanilla cream.

Sfogliatelle, a pastry filled with a mandarin-flavored ricotta filling.

Pasticiotti, another pastry filled with chocolate.

Fruit tarts, éclairs, zeppole, pastiera, cassataSicilianazuppette, and profitteroli. All pastries, and all good.

Note. Is there anything to eat in Italy besides pastries? Interesting question; we’ll have to get back to you on that one, too.

In the meantime, and while we wait for our cappuccino, here’s a script that inserts a manual line break into a Microsoft Word document:

Set objWord = CreateObject(“Word.Application”)
objWord.Visible = True

Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection

objSelection.TypeText “This paragraph is followed by a paragraph return.”

objSelection.TypeText “This paragraph is followed by a line break.” & Chr(11)

objSelection.TypeText “This paragraph is also followed by a line break.” & Chr(11)

objSelection.TypeText “This paragraph is followed by a paragraph return.”

Let’s explain how this script works and, along the way, show you a few pictures that should help convince you that it really does work. As you can see, we start out the way most Microsoft Word scripts start out: we create an instance of the Word.Application object and then set the Visible property to True; that gives us a running instance of Microsoft Word that we can see onscreen. Next we call the Add method in order to add a new, blank document to our instance of Word, then use the following line of code to create an instance of Word’s Selection object, something that positions the cursor at the beginning of the document and enables us to start adding text:

Set objSelection = objWord.Selection

The first bit of text we add should look relatively familiar; all we’re doing is using the TypeText method to type a sentence, then using the TypeParagraph() method to add a hard paragraph return (equivalent to pressing the ENTER key on the keyboard) at the end of that sentence:

objSelection.TypeText “This paragraph is followed by a paragraph return.”

Now, take a look at the next piece of text we type into our document:

objSelection.TypeText “This paragraph is followed by a line break.” & Chr(11)

Once again we’re using the TypeText method to add a sentence to the document. However, we aren’t just adding a sentence; we’re also adding Chr(11) to the end of that sentence. What’s Chr(11)? As it turns out, that’s the ASCII code for a line break. Want to add a line break to the end of a sentence in a Microsoft Word document? Then use Chr(11) rather than the TypeParagraph() method.

And then, just for heck of it, we add a couple more paragraphs, one with a line break, one with a paragraph return.

When the script finishes running our Word document will look like this:

Microsoft Word

Granted, that looks like any other Word document. But notice what happens when we click the button that shows us the formatting marks:

Microsoft Word

As you can see, lines 1 and 4 have a hard paragraph return after them; by contrast, lines 2 and 3 conclude with a soft paragraph return (a line break). If we center-align just line 2 look what happens:

Microsoft Word

Why did lines 3 and 4 also get centered? That’s easy: because they are connected using line break characters rather than paragraph returns. Proof positive that the script works!

Breaking news flash. From what we’ve just been told, Romulus and Remus did not co-found the city of Rome. According to the official story (or at least one of many official stories), the two brothers started to found the city together; however, Remus felt that Romulus was making the city walls too low. To prove his point, he proceeded to jump over the walls. Romulus, in turn, did what any self-respecting brother would do: he killed Remus, finished building the city, and then named it after himself.

Needless to say, ever since then none of his fellow Scripting Guys have been brave enough to tell Peter that he’s been building his walls too low. We’ll just have to make do with the walls the way he built them.

Comments (2)

  1. Casualbob says:

    Great, thank you, you’ve just saved my bacon. Oink oink.

Skip to main content