Change cell format during mail merge

Hi,

I need to change the cell background depending upon the value inserted into word document. I use mail merge to insert the data from dataset into word table. I need a way to apply the formatting while a calling the mail merge function.

Code template:

void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
{
    if (args.FieldName.Equals("a"))
    {
        // Code to access cell and change its format.
    }
}

Please provide me suggestions to solve this problem. Thank You.

Regards,
Aniket

Hi Aniket,

Thanks for your inquiry. The following code finds the Cell your merge field is located in and sets it’s background shading color:

void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
{
    if (args.FieldName.Equals("a"))
    {
        // Code to access cell and change its format.
        Cell cell = (Cell) args.Field.Start.GetAncestor(NodeType.Cell);
        if (cell != null)
        {
            cell.CellFormat.Shading.BackgroundPatternColor = Color.Red;
        }
    }
}

I hope, this helps.

Best regards,

Thanks for the help. Approach worked.