Search code examples
vbams-wordline-breakslinefeed

VBA Word cannot insert Linefeed


I have a string with a number, looking like this:

"12345678-123"

I want to add a linefeed (the thing you get by pressing [SHIFT] + [ENTER]) before the dash "-".

It keeps adding a carriage return. This is my code:

sSomeString = Replace(sSomeString, "-", vbLf & Chr(30)) 'Chr(11) does not work ether - Chr(30) is the char for the dash, that does not break

ActiveDocument.Tables(iTableIndex).Cell(.Rows.Count, 4).Range.Text = sSomeString

I googled and so far it says everywhere to use ether vbLf or Chr(11). Any ideas?

// Problem solved:

My code didn't run correctly cause I was not using the Char(30), so Word created a Linebreak with the character "-"


Solution

  • Word uses the vertical tab (character code 11) for line breaks. So you'll want something like this:

    sSomeString = Replace(sSomeString, "-", Chr(11) & Chr(30))
    

    I don't know if Microsoft makes this readily available. However, you can always insert a line break in Word (Shift+Enter), then use VBA to examine the character code.