How To: Insert New Line / Line Break in Microsoft Word using VBA

Wed, 10/05/2017 - 10:15 -- James Oakley
New paragraph / new line?

In Microsoft Word, there's all the difference in the world between a new paragraph and a new line.

To insert a new paragraph, press the Enter key. If you have "show all characters" turned on, you'll see each paragraph break with its "backwards P" icon. Each paragraph in Word has its own properties. It can have extra space above or below, it could be indented from the left / right margins, with the option of different indentation for the first line.

To insert a new line, press Shift + Enter. If you have "show all characters" turned on, you'll see each manual line break with the icon of a solid arrow that goes first down, then left. Forcing a new line does not start a new paragraph, so the text will be laid out exactly as if it were a continuation of the paragraph you were in, simply on a new line. Because that's what it is. If your paragraph has extra space above or below, this new line will simply be spaced in the same way as the line-spacing (you don't get the extra spacing). If your paragraph has different indentation for the first line, this new line won't have that different indentation, because it's not the first line in a new paragraph.

That's how you enter a line break in Word itself. But what if you want to do so in Visual Basic (i.e., in a macro)?

I needed to do this recently, and the answer was hard to find online, so I'll write it up here — as usual, to help me find this again, and to help anyone else looking for the same thing.

New paragraphs in VBA are easy. There's an application wide constant defined, vbCrLf, which represents ASCII character 10 (carriage return) followed by ASCII character 13 (line feed). A code snippet like this:

ActiveDocument.Paragraphs(6).Range.Text = "Foo" & vbCrLf & "Bar"

will replace the text of that paragraph with "Foo", followed by a paragraph break, followed by "Bar". Thus:

Foo and Bar in different paragraphs

But how do you get "Foo", followed by a line break, followed by "Bar", using VBA?

Foo and Bar separated by a line break

You might try some other VBA supplied constants. Such as vbCr (carriage return, ASCII 13) and vbNewLine (new line, also ASCII 13), or vbLf (line feed, ASCII 10). But all of those insert a new paragraph.

ActiveDocument.Paragraphs(6).Range.Text = "Foo" & vbCr & "Bar"
ActiveDocument.Paragraphs(6).Range.Text = "Foo" & vbLf & "Bar"
ActiveDocument.Paragraphs(6).Range.Text = "Foo" & vbNewLine & "Bar"

It turns out that what you need is ASCII 11, for which there is no pre-defined constant. The following gives you what you're after:

ActiveDocument.Paragraphs(6).Range.Text = "Foo" & Chr(11) & "Bar"

I didn't find that in any Microsoft documentation anywhere, but it works. Thanks for reading, and happy macro writing!

Blog Category: 

Comments

manish's picture
Submitted by manish on

hi i need a code to go to a new line in macro after time for example

03:00:00 AM pingu show

04:00:00 AM RAMSE SHOW

TO

03:00:00 AM

pingu show

04:00:00 AM

RAMSE SHOW

Ellen's picture
Submitted by Ellen on

Thanks!!  This was really helpful!  :-)  Worked perfectly.

cool browser's picture

This was really helpful!  :-)  Worked perfectly.

James Oakley's picture
Submitted by James Oakley on

Do they document these named constants anywhere?

?asc(vbVerticalTab)
 11 

James Oakley's picture
Submitted by James Oakley on

I was just using a sample document and trying to change the text of paragraph 6 so that it includes a line-break.

Evan's picture
Submitted by Evan on

Hi James,

I know this is an old article but I just wanted to register my thanks. Was pulling my hair out over this for a solid 20 minutes. Utterly bizarre that it's not documented anywhere officially.

flyingtogether's picture
Submitted by flyingtogether on

cool

Add new comment

Additional Terms