How to set the result of a text formfield in a Word 2000 table, using VBA, if the string contains carriage returns

Article contributed by Dave Rado

The following works in Word 97:

ActiveDocument.FormFields("Text2").Result = "Line1" & vbCr + "Line2"

Unfortunately in Word 2000, if the formfield is in a table, the carriage returns are converted to invalid ascii characters: 

There are two workarounds:


One is to use line breaks instead of paragraph marks. The following works in Word 97 and above:

ActiveDocument.FormFields("Text1").Result = "Line1" & Chr$(11) & "Line2"

Or you can use the strangely named vbVerticalTab constant instead of Chr$(11)

ActiveDocument.FormFields("Text1").Result = "Line1" & vbVerticalTab & "Line2"


The other workaround is to use the following:

ActiveDocument.Bookmarks("Text1").Range.Fields(1).Result.Text = _
        "Line1" & vbCr + "Line2"
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True

This also works in all versions of Word VBA. As well as allowing you to use real paragraph marks, this method has the advantage that it works with strings longer than 256 characters; but the disadvantage that it you have to unprotect and reprotect. See also: How to set the result of a text formfield using VBA, if the string is longer then 256 characters

Click to view Terms of Use page

Click to view Disclaimer page