We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Need to get the parent cell/row from a merge field

Hi,


I am using the mail merge functionality of Aspose words to populate data in a word template. One of the things I am trying to do during this merge is to change text colors and colors of surrounding table cells based on the data source. Specifically, I have merge fields inside of a table and when they get passed a value “PASS” I want to change the background color of the table row they are in to green. And conversely, if it is “FAIL” I want to color the row red.

Currently, I have a class that implements IFieldMergingCallback so I can do this on a field by field basis. However, I am unable to get the Row object of the row that the field resides in.

Using
var row = (Row)args.Field.Start.GetAncestor(NodeType.Row);
just returns null.

Does anyone know how or if this can be done?

Hi there,


Thanks for your inquiry. Please refer to the following article:
How to Apply Custom Formatting during Mail Merge

You are correctly getting the table’s row in IFieldMergingCallback.FieldMerging. Please attach the following resources here for testing:

  • Your input Word document.
  • Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.

I’ve attached a working console app. The .docx is included in the project. The only thing that needs to be changed is the path to the .docx.


Basically, I’m trying to get the row object for the third row in the table (the one containing “MF3” merge field). With that object I want to change the background color of the cells in that row but as it works currently the row object comes back null.
Hi there,

Thanks for sharing the detail. The DocumentBuilder.MoveToMergeField method (String) moves the cursor to a position just beyond the specified merge field and removes the merge field. This is the reason you are getting null value for Row object.

Please use DocumentBuilder.MoveToMergeField method (String, Boolean, Boolean) as shown in highlighted code snippet below to achieve your requirements.

public void FieldMerging(FieldMergingArgs args)

{

var builder = new DocumentBuilder(args.Document);

builder.MoveToMergeField(args.FieldName, false, false);

if (args.FieldValue != null && args.FieldValue.ToString() == "NO")

builder.Font.Color = Color.Red;

builder.Write(args.FieldValue == null ? "" : args.FieldValue.ToString());

if (args.FieldValue.ToString() == "FAIL")

{

var row = (Row)args.Field.Start.GetAncestor(NodeType.Row);

if (row == null)

Console.WriteLine("Row object is null");

// Do stuff with row (change colors and whatnot)

// But row object is null

}

}