• How do I add CR/LF in string? (VB)

    • This topic has 8 replies, 5 voices, and was last updated 20 years ago.
    Author
    Topic
    #418863

    Hi folks,

    How do I add a newline to a string in VB? I tried:

    s = “Line 1 ” + Chr(10) + Chr(13) + “Line 2”

    But although this works on a c++ stream it does not here…? In ASCII, CR = 0x0D and LF = 0x0A.

    Viewing 0 reply threads
    Author
    Replies
    • #944031

      You could try
      s = “Line 1 ” & vbCrLf & “Line 2”

      • #944033

        Ohhh I see – I knew it was simple – thanx’

        • #944187

          Word just uses vbCr (Ascii 13) in a document. If you insert vbCrLf, the line feed (Ascii 10) will be stripped automatically.
          If you mean a manual line break, that would be ChrW(11) or vbVerticalTab.

          cheers Klaus

          • #944197

            Thanks for pointing that out. That raises the question: in which case(s) should one use vbCrLf instead of vbCR in Word VBA?

            • #944201

              If you build strings to insert into a document, you can use either (vbCr makes more sense I think).

              But in many other cases, you’d use neither grin

              In Find/Replace, you should use ^p (and definitely not ^13!).

              If you insert some text, [Selection].InsertParagraph (or .InsertParagraphBefore, .InsertParagraphAfter) is probably safest.

              cheers Klaus

            • #944202

              As far as I know, you don’t need to use vbCrLf in Word VBA. As the prefix indicates, it’s a general VB constant, available in all dialects of VB/VBA.

              (Long live consistency: Word uses vbCr or Chr(11) to start a new paragraph/line, Excel uses vbLf to start a new line within a cell, and Access uses vbCrLf to start a new line within a field value)

            • #944261

              Yes, there appears to be a consistently individual approach in each case. grin

              Alan

            • #944676

              Thanks guys.

    Viewing 0 reply threads
    Reply To: How do I add CR/LF in string? (VB)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: