Results 1 to 9 of 9
  1. #1
    Lounger
    Join Date
    Mar 2005
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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.

  2. #2
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    You could try
    s = "Line 1 " & vbCrLf & "Line 2"

  3. #3
    Lounger
    Join Date
    Mar 2005
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Ohhh I see - I knew it was simple - thanx'

  4. #4
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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.

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16> Klaus

  5. #5
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

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

  6. #6
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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 <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

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

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

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16> Klaus

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

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

    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)

  8. #8
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Yes, there appears to be a consistently individual approach in each case. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    Alan

  9. #9
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Thanks guys.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •