Free Support Forum -

Inserting a field after a specific location in the text of a paragraph

I need to create a TOA in my Word document. I know I can use the { TOA } and { TA } field codes to do this, and DocumentBuilder has InsertField() to put in any field code one needs. Now, putting in the TOA code doesn't seem to be too hard as that'll be at the top of the document, but...I need (or at least I think I need) precise control over where the TA code is going to go within a paragraph. However, I can't figure out how to move to a specific location within a paragraph to place the field. Can this be done, and if so, how is it done?


Jason Bock

Hey Jason,

The answer is "surely"! DocumentBuilder provides a set of the MoveXXX methods to move the insertion point to any place in the document. Actually, you can move to any document node using the MoveTo method. A neat way of moving to a specific location within a paragraph is placing a bookmark at that location and calling MoveToBookmark.

I looked at the MoveXXX methods and the only one that comes close to what I want is MoveToParagraph(), but the documentation says that the 2nd argument doesn't really work - only 0 and -1 are supported for characterIndex. MoveToBookmark() won't work - I don't have a bookmark to move to (the documents in the real system will be coming from thousands of clients and we don't control how they are made). MoveTo(Node) won't work either as the Run objects within the paragraph fragment the text that I need to add a cite (TA) field for.

What I'd really like to do is say, OK, my text is "Smith vs. Jones" and I know it's in a paragraph. I can use GetText() on the Paragraph, use IndexOf() to find out where that text is, call MoveToParagraph() to get to the end of that specific string within the paragraph text, and then InsertField(). But since MoveToParagraph() as documented won't move to a precise character, I don't know of another way to pull this off.

I've attached a sample document that has three cites within them. If you could look at the document and demonstrate how I could programatically add TA field codes after the cites I'd appreciate it.



Using replace evaluators might be a nice solution then. Here's a sample code for your document:

public void TestToa()


Document doc = new Document(@"D:\Work\TOA.doc");

InsertToa(doc, @"Prescott v\. Leaf River Forest Prods\., Inc\., 740 So\. 2d 301, 311");

InsertToa(doc, @"Harris v\. Owens-Corning Fiberglass Corp\., 102 F\.3d 1429 \(7th Cir\. 1996\)");

InsertToa(doc, @"Munford, Inc\. v\. Fleming\, 597 So\. 2d 1282, 1284 \(Miss\. 1992\)");

doc.Save(@"D:\Work\TOA Out.doc");


private void InsertToa(Document doc, string pattern)


doc.Range.Replace(new Regex(pattern), new ReplaceEvaluator(InsertToaEvaluator), true);


private ReplaceAction InsertToaEvaluator(object sender, ReplaceEvaluatorArgs e)


DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document);


builder.InsertField(@" TOA \* MERGEFORMAT ", "");

return ReplaceAction.Skip;


In practice, the task might require some additional actions as the text to find is not necessarily contained in one Run. So if you want to insert the field right after a text fragment, you might have a need to move to one of the next runs using NextSibling or maybe split the last run that is covered by the text into two runs. However, the base techinque should be now clear. If it isn't, don't hesitate to post your further questions here.

With a little modification that code works - I now see the { TA ... } fields in my resulting document.

The final problem is getting the { TOA ... } field in. I tried this (this code happens after the TA fields are inserted):

DocumentBuilder builder = new DocumentBuilder(toaDocument);
builder.MoveToParagraph(0, 0);
builder.InsertField(@" TOA \h \c ""1""", null);

string newToaDocument = Path.Combine(
Path.GetFileNameWithoutExtension(targetDocument) + "Aspose.doc");


but that doesn't work. The TOA field doesn't show up; in fact the "garbage" Aspose text (I'm using an evaluation version) is doubled. I've tried other MoveToXXX() methods but nothing seems to work - any ideas?



I tried your code and noticed the TOA field at the beginning of the document as expected. It was visible even if I used the evaluation version. Did you press Alt-F9 to see field codes or did you update the field when in field results mode by pressing F9?

I do - I missed that the first time I opened up the doc. But...when I press F9 to update the TOA, it doesn't work. I've uploaded a doc called TOAAspose.doc, which is the file generated by my code. If I make the TA and TOA fields manually, it works (I'd like to attach that document as well, but I can only do one per forum entry - I'll add another entry with that document).

In Word, the field codes look the same, but the TOAAspose file doesn't show the TOA. What am I missing?



Here’s the manual file

It appears that the TA fields have more complicated structure. So to insert a proper TA field, use the following code line:

builder.InsertField(@" TA \l """ + e.Match.Value + @""" \s """ + e.Match.Value + @""" \c 1" , "");

Please note that the first switch is "l" (lowercase L) and the last character of the field code is "1" (the number one). It took a while to discover what was wrong during insertion Smile [:)]

Ah, yes, totally missed that :)

It works now. I still have to manually right-click on the field when I open the document and say "Update Field". Then hitting F9 works. It would be nice if that could "automatically" be there when the user opened the document but I understand why it's not.