Hi Joseph,
Thanks for attaching your template for me to look at. I have created some sample code for you below, it is the implementation of my idea above. I have also attached the edited template. This template includes an extra field which defines the filter in the field result (e.g = 5). The code will handle this field while merging and will not merge a particular row if the value does not match this filter. You can also set if you want the filter field merged result to be removed from the document during merging.
If there are any other requirements that you need to define n your template then we can take a further look into them. I also think you are correct when you suggest there should be some sort of way to easily skip a record in template when merging. I have linked a new feature request to include a member in the FieldMergingArgs which should give you the option to skip the merging of a current record. This would make implementing this sort of filtering much easier. We will keep you informed of any developments.
// Pass an boolean option which defines whether to remove the field containing the filter or not<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
doc.MailMerge.FieldMergingCallback = new HandleMergeField(true);
// Set up the handler any node changing during merging.
doc.NodeChangingCallback = new HandleNodeChangingMerge();
doc.MailMerge.ExecuteWithRegions(dataset);
public class HandleMergeField : IFieldMergingCallback
{
bool mDeleteField;
public HandleMergeField(bool deleteFieldWithFilter)
{
mDeleteField = deleteFieldWithFilter;
}
void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
{
// Check if this field contains a filter defined by the equals sign in the field code.
// If it does have a filter then evaluate it and if the value is differen to the filter, mark
// the row it appears in to be removed
if (args.Field.Result.Contains("="))
{
// Get the filter value between the "=" symbol and the end of the field result symbol
string result = args.Field.Result;
int index = result.LastIndexOf("=");
string filterText = result.Substring(index + 1, result.IndexOf("»", index + 1) - index - 1).Trim();
// If the value is different mark it for removal
if (!args.FieldValue.Equals(filterText))
args.Text = "%RowRemove%";
// The value is the same, if the filter field is set to be removed the set the value to merge to empty
else if(mDeleteField)
args.Text = "";
}
}
void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
{
// Do Nothing
}
}
public class HandleNodeChangingMerge : INodeChangingCallback
{
void INodeChangingCallback.NodeInserted(NodeChangingArgs args)
{
Node node = args.Node;
if (node.NodeType == NodeType.Run)
{
Run run = (Run)node;
// Remove the row marked with the %RowRemove% tag
if (run.Range.Text.Contains("%RowRemove%"))
{
Row row = (Row)run.GetAncestor(NodeType.Row);
if(row != null)
row.Remove();
}
}
}
void INodeChangingCallback.NodeInserting(NodeChangingArgs args)
{
// Do Nothing
}
void INodeChangingCallback.NodeRemoved(NodeChangingArgs args)
{
// Do Nothing
}
void INodeChangingCallback.NodeRemoving(NodeChangingArgs args)
{
// Do Nothing
}
}
If you have any further queries, please feel free to ask.
Thanks,