I am using word document template with predefined fields(bookmarks)! those bookmarks are programmatically updated by the actual value at run time and it works fine, but some of the word in document is not marked as a bookmark so I am using Find & Replace feature of Microsoft.Office.Interop.Word
to replace that word with actual value, but when it replace the word with text that contains the line breaks the line break does not appear in document the whole text comes in single line.
In below code I have replaced the text "Address" in document with following text
Park Royal House No: 3301 Wing - D
City: xxxx
State: YY
Zip: 100215
but it comes in document like this
Park Royal House No: 3301 Wing - D City: xxxx State: YY Zip: 100215
Following it the sample code to replace the text in word doc
Private Sub ReplaceWorkDocText()
Dim objApplication As Microsoft.Office.Interop.Word.ApplicationClass = Nothing
Dim objDocument As Microsoft.Office.Interop.Word.Document = Nothing
Dim findText As String = "Address"
Dim replaceText As String = "Park Royal House No: 3301 Wing - D" + _
vbCrLf + "City: xxxx" + _
vbCrLf + "State: YY" + _
vbCrLf + "Zip: 100215"
objApplication = New Microsoft.Office.Interop.Word.ApplicationClass()
objDocument = objApplication.Documents.Open("C:\TEST\FORM0001.docx")
With objDocument.Range.Find
.Text = findText
.Replacement.Text = replaceText
.Forward = True
.Execute(Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceOne)
End With
End Sub
Please help me how do I replace word in document with line break.