Updating only parts of the Comments fields, after changing the values

Good day,
I change the value of the field in the document, update all fields of type Comments. However, the new value is written to me only in part of the fields. Could you take a look at why it doesn’t prescribe it for everyone but only for some…

my code:

Document doct = new Document("..................................");
doct.BuiltInDocumentProperties.Comments = "TestTest";

for (int idx = doct.Range.Fields.Count - 1; idx >= 0; idx--)
{
    Field field = doct.Range.Fields[idx];
    if (field.Type == FieldType.FieldComments)
    {
        field.Update();
    }
}

the result is that out of 126 fields, the value changes in 55…
Thanks for any advice.

@benestom Could you please attach your document here for testing? We will check the issue and provide you more information.

P099_3M.docx (613.9 KB)

the document is divided into sections, each header has a field

@benestom Thank you for addition information. In your document there are COMMETS fields that replaces the value set in the doc.BuiltInDocumentProperties.Comments:
{ COMMENTS 6.2.3.1 } and { COMMENTS 6.2.3.2 }

So upon updating these fields, the value in the doc.BuiltInDocumentProperties.Comments is overridden. Please see the following link for more information:
https://support.microsoft.com/en-us/office/field-codes-comments-field-da559a23-25ca-46f2-b46f-c8c7b2c432c5

You can reset new comment text in the COMMETS to avoid this:

Document doc = new Document(@"C:\Temp\in.docx");

doc.BuiltInDocumentProperties.Comments = "TestTest";

doc.Range.Fields.Where(f => f.Type == FieldType.FieldComments).Cast<FieldComments>().ToList()
    .ForEach(f => { f.Text = null; f.Update(); });

doc.Save(@"C:\Temp\out.docx");

Thank you in advance for your reply. This works fine…

1 Like