Update field insert bed value

good day,

I found a difference in updating the fields, for one document update the fields gradually
if i use the code:

doc.UpdateFields();

the result of fields is 1 that’s ok
if i execute the code:

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

the result is 6.2.3…1 in the header, that’s wrong

you don’t know why?
You could look at it T.

1P099m03.docx (602.6 KB)

@benestom The first on the screenshot is COMMENTS field, which shows the value of Comments document property. In your document the value of this property is 6.2.3.1

Also, in your document there is fields like this { COMMENTS 6.2.3.1 }. This field code resets value of Comments document property to the specified text. When you update field one by one part of comments are updated before resetting the value.

I don’t understand, if I press the RIGHT button on the document I sent and give the properties, the value IS 1 will be displayed, as you see the 6…

if i update all links then i have 1,
if I update only some see the example above I have 6,…

@benestom I checked the value of Comments property in MS Word. Most likely, MS Word updated the value upon loading the document to the value set by COMMENTS field in your document.

Here is a simple test:

Document doc = new Document(@"C:\Temp\in.docx");
Console.WriteLine(doc.BuiltInDocumentProperties.Comments); // returns 1
doc.UpdateFields();
Console.WriteLine(doc.BuiltInDocumentProperties.Comments); // returns 6.2.3.2

As you can see after loading document BuiltInDocumentProperties.Comments returns 1. After updating fields it returns 6.2.3.2. The value is changed after updating the following field in the document { COMMENTS 6.2.3.2 }

You can lock COMMENTS field, which change the value of the appropriate property:

Document doc = new Document(@"C:\Temp\in.docx");
foreach (Field f in doc.Range.Fields)
{
    if (f.Type == FieldType.FieldComments)
    {
        FieldComments c = (FieldComments)f;
        if(!string.IsNullOrEmpty(c.Text))
            c.IsLocked = true;
    }
}
doc.UpdateFields();
doc.Save(@"C:\Temp\out.docx");

In this case COMMENTS fields will show 1 in the output document.

I’ll be more specific
The document I sent came out like this:

  1. It contained the value in comment 6.3.2.1
  2. I set it to 1
    doc.BuiltInDocumentProperties.Comments = 1
  3. I saved it and sent it as 1P099m03.docx

if then I do field.Update(); I will still have the value there, which I have already overwritten…
You understand what my problem is

@benestom yes, I understand your problem, but as I have mentioned in your document there are the following field { COMMENTS 6.2.3.2 }. Upon updating this field it overrides the value of BuiltInDocumentProperties.Comments.
https://support.microsoft.com/en-gb/office/field-codes-comments-field-da559a23-25ca-46f2-b46f-c8c7b2c432c5

So I have suggested to lock the COMMENTS fields in your document, which change value of BuiltInDocumentProperties.Comments upon updating fields.

Problem solved, I found a comment field in the dock that changed it. After editing this field it stopped changing. Thank you very much

1 Like