Problem with UpdateFields

Hi,

Ihave the following code which is railing for me

try
{
    document.Range.UpdateFields();
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

It is going intot he exception handler and the following error message is in the exception:

The argument cannot be null or empty string.
Parameter name: name

I have a number of custom fields in the document and I have no idea which one the exception is talking about.

Is there anyway to find out?

Cheers

Paul

Hi

Thanks for your inquiry. Could you please attach your input document here for testing? I will check the problem on my side and provide you more information.

Best regards,

Hi,

I found the problem, it is on our side. The code is mangling the custom property and removing the name of hte document property.

Is there anyway to delete a custom document property from a document and preferably one with no name!

Cheers

Paul

Hi Paul,

Thank you for additional information. Please try using the following line of code to remove CustomDocumentProperty:

doc.CustomDocumentProperties.Remove("PropertyName");

Also see the following link:
https://docs.aspose.com/words/net/work-with-document-properties/
Best regards,

The problem is that hte code has already corrupted the custom field as you can see in the attached document with the “reviewed by” and “authorised by” fields in the table in the attached document.

If you press alt and F9, you will see what I mean.

Is there anyway I can programmatically delete these corrupted fields?

I tried the code you sent bit it does not work.

Hi Paul,

Thank you for additional information. Each field in MS Word consists of FieldStart, FieldSeparator, FieldEnd nodes and Run nodes, which represent field code and field value (displayed text). So in MS Word document field looks like the following:
[FieldStart]here is field code[FieldSeparator]here is field value[FieldEnd]
Please press Alt-F9 to see the field code.
To remove empty fields from the document, you can use the following code:

// Open document.
Document doc = new Document("in.doc");
// Get all FieldStart from the document.
Node[] fieldStarts = doc.GetChildNodes(NodeType.FieldStart, true).ToArray();
// Loop through all FieldStart.
foreach(FieldStart fieldStart in fieldStarts)
{
    // Check if there is FieldSeparator after FieldStar and FieldEnd after FieldSeparator then remove.
    if (fieldStart.NextSibling.NodeType == NodeType.FieldSeparator &&
        fieldStart.NextSibling.NextSibling.NodeType == NodeType.FieldEnd)
    {
        fieldStart.NextSibling.NextSibling.Remove();
        fieldStart.NextSibling.Remove();
        fieldStart.Remove();
    }
}
doc.Save("out.doc");

Best regards,

Brilliant, is is possible to add docproperties to parts of the document?

Thanks for your help on this.

Hi Paul,

Thanks for your inquiry. Please see the following link to learn how to add DocumentProperties:
https://docs.aspose.com/words/net/work-with-document-properties/
Also, please follow the link to learn how to insert field into a document:
https://reference.aspose.com/words/net/aspose.words/documentbuilder/insertfield/
Best regards,

Hi,

Thanks for the replies.

I am nearly finished but I am getting a very strange bug with the attached document, I use the following method to delete the field:

public static void DeleteField(FieldStart fieldStart)
{
    fieldStart.NextSibling.NextSibling.Remove();
    fieldStart.NextSibling.Remove();
    fieldStart.Remove();
}

I am then inserting a new custom field with the following code:

public static void BuildField(Paragraph parentParagraph, bool isReviewedBy, bool isAuthorisedBy, IDocumentParent documentParent)
{
    var documentBuilder = new DocumentBuilder(parentParagraph.Document);
    var value = GetSignOffList(documentParent, isAuthorisedBy);
    var fieldText = string.Format(@"MERGEFIELD {0} \* MERGEFORMAT", (isReviewedBy) ? "ReviewList" : "AuthorisedBy");
    documentBuilder.MoveTo(parentParagraph);
    documentBuilder.InsertField(fieldText, value);
}

Everything is working fine apart from if you press ALT and F9 with the attached document, you can see that the background colour of the first field is yellow.

I am absolutely lost as to where this colour might be coming from. Any idea how I can get rid of this strange background coour?

Cheers

Paul

The code I previously posted left one of our documents like the attached.

No where else in the document is there white text or a yellow background.

I am completely baffled by this.

Is there anything I can do to change things to the correct colour?

Cheers

Paul

Hi Paul,

Thank you for additional information. Please try using the following code:

public static void BuildField(Paragraph parentParagraph, bool isReviewedBy, bool isAuthorisedBy, IDocumentParent documentParent)
{
    var documentBuilder = new DocumentBuilder(parentParagraph.Document);
    var value = GetSignOffList(documentParent, isAuthorisedBy);
    var fieldText = string.Format(@"MERGEFIELD {0} \* MERGEFORMAT", (isReviewedBy) ? "ReviewList" : "AuthorisedBy");
    documentBuilder.MoveTo(parentParagraph);
    documentBuilder.Font.HighlightColor = Color.Empty;
    documentBuilder.InsertField(fieldText, value);
}

Best regards,