[MSWordUtils] Text Replacement issues with Newline()

Forge Component
Published on 5 Jul (4 weeks ago) by Stuart Harris
3 votes
Published on 5 Jul (4 weeks ago) by Stuart Harris


We encountered issues with adding newline for text replacement features. 

Here is how we do it:

Create an empty word document with following content:

(a)  <TextA>< TextB><TextC><Text4><Text5><Text6>

(b)  <TextA>< TextC><Text5>

Add 6xMSWordUtils_ReplaceText with:

- "<TextA>" Replace with I am 

- "<TextB>" Replace with NewLine()+NewLine()+"going"+NewLine()+" to own"

- "<TextC>" Replace with " S$1,000,000.00 "

- "<Text4>" Replace with NewLine()+NewLine()+"S$Dollars."+NewLine()+"Cool"

- "<Text5>" Replace with "$$Money;"

- "<Text6>" Replace with NewLine()+NewLine()+"Thank "+NewLine()+"You"

Here is the result:


(a)I am < TextB> S$1,000,000.00


(b)I am < TextC>< Text5>


Anyone have advice for this?




Hi Ferry,

Thank you for posting on the support channel. As we discussed earlier, it appears the DocX library has trouble with text replacements on the same line when using NewLine(), and potentially also when they are next to each other.  I have investigated this with bookmarks, and bookmark replacement appears to handle NewLine() better than text replacement.

Also, it looks like < TextC> and < Text5> have an extra space in the identifier, which could explain why they are not being replaced. Although maybe that is due to how the text was pasted into the comment.

I hope this helps.

Kind regards,


Hi Stuart,

Sorry for the typo mistake in the previous post. The < TextC> and < Text5> was my typo mistake in the post.

I have uploaded the .oml file. You can refer to it for the detailed integration.




I felt very happy while reading this site. This was a really very informative site for me. I really liked it. This was really a cordial post. Thanks a lot!

t rex game

Great, thank you Ferry.  I will have a look over the next few days and get back to you.

Hi Ferry,

I have had a look and there does not appear to be anything wrong with your code or your document.

It looks like the problem is when using NewLine() on a text replacement that is not the end of the line, causes problems with the underlying library.

Replacing bookmarks however does not seem to have this problem.

Maybe placing a single text replacement on a line would work better and producing the line in code might work better.

I hope this helps!

Kind regards,


Hello again Ferry,

I had a look at the source document you were using for replacing text.  Actually is seems there are som problems with the document.  Many of the text tags do have a space in them.

I have fixed the document (see attached), but I do still get some strange behaviour.  So text replacement does not appear to work well when inserting new lines.

So keeping it simple, and potentially changing to use bookmarks seems like the best approach.

I hope this helps.

Kind regards,