Replacing text with fields


#1

I have several documents which have text in the format “[M99]” that I want to replace with a fieldcode with the same name. I’ve tried using the DocumentBuilder but it seems that the margin notes in the documents (which are in frames) throw off the insertion point because I end up with all of the fieldcodes and surrounding text in the first margin note rather than interspersed within the body. A test document without margin notes works.

If I instead want to parse the document nodes, can I build essentially a FieldCode node and insert it or can you only insert a field by calling DocumentBuilder.InsertField()? Or better yet, am I simply missing something when using the DocumentBuilder?

Attached is my code and documents.

Thanks,
Joe


#2

You are doing everything right. Just one detail to add - you need to position builder before insertion. Add the following line in your code:

while (m.Success) {

builder.MoveTo(run); // add this line

builder.Write(runText.Substring(0, m.Index));

// ...

and it will work fine.

BTW, good job with the FixVisitor class. Do you mind if I'll put it in one of our howto articles?


#3


Perfect, that did it. Please feel free to use the code in any way you wish. As always, thanks for the blazingly quick response.

Joe