Newly created merge fields do not show up

Hello,

I’m trying to dynamically create a document with merge fields. Here is the code that I have for it

private void AddMergeField(DocumentBuilder builder, string tableName, string fieldName)
{
    builder.InsertParagraph();

    string fieldFormat = @"MERGEFIELD {0} * MERGEFORMAT ";

    FieldStart field;
    if (String.IsNullOrEmpty(tableName))
    {
        string fieldCode = String.Format(fieldFormat, fieldName);
        field = builder.InsertField(fieldCode, "");

    }
    else
    {
        string fieldCode = String.Format(fieldFormat, "TableStart:" + tableName);
        field = builder.InsertField(fieldCode, "");
        builder.InsertParagraph();
        fieldCode = String.Format(fieldFormat, fieldName);
        field = builder.InsertField(fieldCode, "");
        builder.InsertParagraph();
        fieldCode = String.Format(fieldFormat, "TableEnd:" + tableName);
        field = builder.InsertField(fieldCode, "");
    }
}

Merge fields are created correctly but they do not show up in the result Word document as <>. If however I Alt-F9 in Word I can see all merge fields created correctly.

Is there anything I missed?
Thanks,
lubimkas

Hi
Thanks for your inquiry. You should specify field value to fix this. I modified your code.

private void AddMergeField(DocumentBuilder builder, string tableName, string fieldName)
{
    builder.InsertParagraph();
    string fieldFormat = @"MERGEFIELD {0} \* MERGEFORMAT ";
    string FieldNameFormat = "«{0}»";
    FieldStart field;
    if (String.IsNullOrEmpty(tableName))
    {
        string fieldCode = String.Format(fieldFormat, fieldName);
        string name = String.Format(FieldNameFormat, fieldName);
        field = builder.InsertField(fieldCode, name);
    }
    else
    {
        string fieldCode = String.Format(fieldFormat, "TableStart:" + tableName);
        string name = String.Format(FieldNameFormat, "TableStart:" + tableName);
        field = builder.InsertField(fieldCode, name);
        builder.InsertParagraph();
        fieldCode = String.Format(fieldFormat, fieldName);
        name = String.Format(FieldNameFormat, fieldName);
        field = builder.InsertField(fieldCode, name);
        builder.InsertParagraph();
        fieldCode = String.Format(fieldFormat, "TableEnd:" + tableName);
        name = String.Format(FieldNameFormat, "TableEnd:" + tableName);
        field = builder.InsertField(fieldCode, name);
    }
}

I believe that this will help you.
Best regards.

Great. That’s exactly what I need.
Thanks for your help.