Word prompts to update fields

Hi.

We are using Gembox to create a document with dynamic content. The document contains a table of content, and page numbers.

We are using this code to update everything before sending the document to our clients :

using var msInput = new MemoryStream(docBytes);
var doc = GemBox.Document.DocumentModel.Load(msInput);

foreach (GemBox.Document.TableOfEntries toc in doc.GetChildElements(true, GemBox.Document.ElementType.TableOfEntries))
{
    toc.Update();
}

doc.GetPaginator(new GemBox.Document.PaginatorOptions() { UpdateFields = true });

using var msOutput = new MemoryStream();
doc.Save(msOutput, new GemBox.Document.DocxSaveOptions());

return msOutput.ToArray();

Everything is updating correctly, but Word is always asking the user :

Is there a way to keep updating everyting (TOC and page numbers) without asking the user to update the fields in the document?

Thanks!

Hi Mathieu,

It seems that one of the fields in the document has an IsDirty flag.
So, try this:

toc.IsDirty = false;

And this:

foreach (var field in document.GetChildElements(true, ElementType.Field).Cast<Field>())
    field.IsDirty = false;

Or maybe this:

foreach (var field in document.GetChildElements(true, ElementType.Field).Cast<Field>())
{
    field.IsDirty = false;
    foreach (var nestedField in field.InstructionInlines.OfType<Field>())
        nestedField.IsDirty = false;
}

I hope this helps.

Regards,
Mario

Hi Mario,

You are right, everything seems to work fine with the code you gave me.

Thank you for your quick answer :slight_smile: