none
Newline or Carriage Return in VBA for Word2010? A total quickie.

    Question

  • I tried "\r\n" with no look. It simply inserts "\r\n" verbatim into the document. How do I send a newline character in VBA?

    Thanks!

    Sep

    Wednesday, October 27, 2010 10:00 PM

Answers

  • Sub ScratchMacro()
    'A quick macro scratch pad created by Greg Maxey
    ActiveDocument.Range.InsertAfter "Test" & Chr(11) & "Test"  'New line
    ActiveDocument.Range.InsertAfter "Test" & vbCr & "Test" 'New paragraph
    End Sub

    "Sepoto" wrote in message news:9bff0bf2-f53d-4c04-97fa-f5b48a48e120@communitybridge.codeplex.com...

    I tried "\r\n" with no look. It simply inserts "\r\n" verbatim into the document. How do I send a newline character in VBA?

    Thanks!

    Sep


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm
    • Marked as answer by S.e.p.y Wednesday, October 27, 2010 10:57 PM
    Wednesday, October 27, 2010 10:42 PM

All replies

  • Sub ScratchMacro()
    'A quick macro scratch pad created by Greg Maxey
    ActiveDocument.Range.InsertAfter "Test" & Chr(11) & "Test"  'New line
    ActiveDocument.Range.InsertAfter "Test" & vbCr & "Test" 'New paragraph
    End Sub

    "Sepoto" wrote in message news:9bff0bf2-f53d-4c04-97fa-f5b48a48e120@communitybridge.codeplex.com...

    I tried "\r\n" with no look. It simply inserts "\r\n" verbatim into the document. How do I send a newline character in VBA?

    Thanks!

    Sep


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm
    • Marked as answer by S.e.p.y Wednesday, October 27, 2010 10:57 PM
    Wednesday, October 27, 2010 10:42 PM
  • Thanks! I've been to your web page. It's way cool. Maybe we will get a chance some time to collaborate on some projects. I'm hoping for more work as we all do!

    Sep!

    Wednesday, October 27, 2010 10:58 PM
  • A paragraph mark, a new line if you will, in a Word document is represented by a carriage return character (ascii 13). According to Microsoft, there should be no other occurrences of this character in a document but they can be created, and I believe you have a page about this on your site, Greg.
     
    The correct way to insert a paragraph mark is to use Range.InsertParagraph (or InsertParagraphBefore or InsertParagraphAfter), or Selection.TypeParagraph. However, VBA is kind (if that's the right word) and will insert one if you use any of the various predefined constants or other methods of inserting either a line feed or a carriage return character (or even both, although in the 'wrong' order, two will be inserted).
     

    Enjoy,
    Tony
    www.WordArticles.com
    Thursday, October 28, 2010 8:22 AM