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

Free Support Forum - aspose.com

Number of Merge Field in a Table Cell

During processing a document, I get a reference to a cell object:

Dim cell As Aspose.Words.Tables.Cell = nodeContainer

How can I work out the number of Merge Fields that are inside the cell?
I’m trying to use:

Dim nFields As Integer = cell.GetChildNodes(Aspose.Words.NodeType.FieldStart, True).Count

But because I’m in the MailMerge.MergeField event, the fields that were previously merged are lost. I assume this is because GetChildNodes uses the live document.

Thanks for your request. Could you please provide me more information what you try to achieve? Why do you need number of merge fields in cell? As workaround you can get number of merge fields before mail merge. For example you can use the following code.

// Create hash table
Hashtable cellsHash = new Hashtable();
// Open document
Document doc = new Document(@"Test109\in.doc");
// Get collection of cells
NodeCollection cells = doc.GetChildNodes(NodeType.Cell, true);
foreach (Cell cell in cells)
    int mergefieldsCount = 0;
    // calculate count of Mergefield in the current cell
    NodeCollection fieldStarts = cell.GetChildNodes(NodeType.FieldStart, true);
    foreach (FieldStart start in fieldStarts)
        if (start.FieldType == FieldType.FieldMergeField)
    // Add entry into hashtable
    cellsHash.Add(cell, mergefieldsCount);

Then you can get count of mergefield from created hashtable. For example.

int count = Convert.ToInt32(cellsHash[cells[0]]);

Hope this helps.
Best regards.