0
votes

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.

1
I tried your code and when i tested it my word document came out with line breaks. Though i did have to set "Embed Interop Types" to "False" on the interop reference. Not sure why it's not not working for youCal-cium
@Cal-cium Thanks, In my case the "Address" is under word table. I got the other solution which seems working fine. same going to postNeeraj Kumar Gupta
I just tried it using a table and I see what you mean. But when you copy and paste that text outside the table it has line breaks, how strange. It must be to do with table formatting.Cal-cium

1 Answers

1
votes

I use this work around to do the same, its working fine.

Private Sub ReplaceWorkDocText()
    Dim objApplication As Microsoft.Office.Interop.Word.ApplicationClass = Nothing
    Dim range As Microsoft.Office.Interop.Word.Range 
    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")

    range= objDocument.Range
    With range.Find
        .Text = findText
        .Forward = True
    End With

    While range.Find.Execute(findText, MatchWholeWord:=True)
        range.Text = replaceText
        Exit While
    End While
End Sub