Example MergeField class mFieldSeparator.NextSibling not Run

Hi,

I’m using your example MergeField class to programmatically update merge field values in a document. This works ok for it seems like 90% of the fields, but in some fields the Code property throws an exception when I’m trying to set it because mFieldSeparator.NextSibling is not a Run Node, but is the FieldEnd Node. Is there a way to set the value in these fields? I was thinking I could just insert a run between the separator and the field end but why is it like that in the first place?

Thanks for your help,

Adam

Hi
Thanks for your request. This can occur because field value was removed manually. Or if you insert merge field programmatically but don’t specify field value as shown in the following code:

builder.InsertField(@"MERGEFIELD Image:myField \* MERGEFORMAT ", "");

Of course, you can insert Run between FieldSeparator and FieldEnd. Just check whether FieldSeparator.NextSibling is FieldEnd and insert Run after FieldSeparator in this case.
Could you please attach your document for testing?
Best regards.

Thanks, I have attached the document.

The field that causes this is MERGEFIELD RESWARE_SP_GetSalesPrice_1

Hi
Thanks you for additional information. Why don’t you use mail merge feature to fill your template with data? Please see the following link to learn more about mail merge:
https://docs.aspose.com/words/net/types-of-mail-merge-operations/
I am sure that mail merge will give you proper result.
Best regards.

I am not using the mail merge feature because we do not want to replace the fields. They need to be updated every time the document is opened.

Ok. I think in this case it is better to bookmarks or formfields instead mergefields. See the following links for more information:
https://docs.aspose.com/words/net/working-with-bookmarks/
https://docs.aspose.com/words/net/working-with-form-fields/
Best regards.

Bookmarks will not work for us because we want the field to be easily visible in the document. Form fields may have worked, but when we first implemented the system 5 years ago we used merge fields for whatever reason so we are stuck with those.

Hi
Thank you for additional information. You can try modifying MergeField.Name property as shown below:

/// 
/// Gets or sets the name of the merge field.
/// 
internal string Name
{
    get
    {
        return GetTextSameParent(mFieldSeparator.NextSibling, mFieldEnd).Trim('«', '»');
    }
    set
    {
        // Merge field name is stored in the field result which is a Run 
        // node between field separator and field end.
        Run fieldResult = null;
        if (mFieldSeparator.NextSibling.NodeType == NodeType.Run)
        {
            fieldResult = (Run)mFieldSeparator.NextSibling;
        }
        else
        {
            //Create new Run
            fieldResult = new Run(mFieldSeparator.Document);
            //insert after field separator
            mFieldSeparator.ParentNode.InsertAfter(fieldResult, mFieldSeparator);
        }
        fieldResult.Text = string.Format("«{0}»", value);
        // But sometimes the field result can consist of more than one run, delete these runs.
        RemoveSameParent(fieldResult.NextSibling, mFieldEnd);
        UpdateFieldCode(value);
    }
}

Hope this helps.
Best regards.