Changing TOC fieldcode text

Hi there,

I have a document with TOC fields and I would like to change the text in these fields so that the displaying text in the Table of Contents is dynamic. I got some code from another thread, but I am stuck as to how to get the field object and change the field code text. Here is the code I am working with...

NodeCollection fields = section.GetChildNodes(NodeType.FieldStart, true);

foreach (FieldStart field in fields)

{

if (field.FieldType == FieldType.FieldTOCEntry)

{

Aspose.Words.Field tocField = new Field(field); //<== this doesnt work...

}

}

Hi,

The Field class provides quitу limited functionality at the moment and it is not supposed for changing the field code. So meanwhile you should implement it yourself. Field code consists of a text run or runs enclosed between field start and either field separator or field end; so basically you should delete the run(s) first and then insert your own run, or alter the text of the first run and delete the rest, something like this:

if (field.FieldType == FieldType.FieldTOCEntry)

{

Run fieldCode = (Run)field.NextSibling;

fieldCode.Text = "Insert your text here";

Node node = fieldCode.NextSibling;

Node nextNode;

while ((node.NodeType != NodeType.FieldSeparator) && (node.NodeType != NodeType.FieldEnd))

{

nextNode = node.NextSibling;

node.Remove();

node = nextNode;

}

}