Sawan
Sawan

Reputation: 256

How to replace with multiline string?

The code for find and replace is working when replacement string is short (in a line) but causing problem when replacement string is multiline string (may be without new line char).

With WordDoc.Content.Find
   .Text = "<<audit_standard>>"
   .Replacement.Text = Range("B9")
   .Wrap = 1
   .Execute Replace:=1
End With

This works when Cell B9 content is short and can be fit in a single line in Word file.

Upvotes: 0

Views: 676

Answers (2)

Sawan
Sawan

Reputation: 256

After researching about it little more, I found an easy and elegant solution. I do not see any problem with my new approach for now. It's working perfectly.

In word document I created a bookmark named "audit_standard" for <<audit_standard>> text. and then wrote below code in VBA for replacement.

WordDoc.Bookmarks("audit_standard").Range.text = Range("B9").Value

Upvotes: 0

macropod
macropod

Reputation: 13490

Neither a Find expression nor a Replace expression can include a line break. Wrapped lines in the source are of no consequence however, unless they are more than 255 characters long (the F/R string-length limit). What might be able to use is:

Range("B9").Copy
With WordDoc.Content.Find
   .Text = "<<audit_standard>>"
   .Replacement.Text = "^c"
   .Wrap = 1
   .Execute Replace:=1
End With

Alternatively, to insert a line break (not a paragraph break) but otherwise discard the source formatting:

With WordDoc.Content.Find
   .Text = "<<audit_standard>>"
   .Replacement.Text = Replace(Range("B9").Text, Chr(10), "^l")
   .Wrap = 1
   .Execute Replace:=1
End With

If you want a paragraph break instead of a line break, replace ^l with ^p.

Upvotes: 1

Related Questions