I have a Word template with various merge fields. For certain merge fields I would like to display a checkbox instead of the text I have saved in the database. This text is NOT a boolean value, so I cannot simply check for true/false value. In order to distinguish these types of fields I add a prefix using the field options in Word. Word calls the field ‘Text to be inserted before:’. My question is how can I read the value of this prefix? args.fieldname only returns the merge field without the text before. If I can’t read the value of this prefix, what other method would I have to indicate I want to display this field differently? Is there some sort of tag property?
Hi
Jacob,
Thanks for your inquiry. Could you please attach a simplified Word document here for testing? I will investigate the scenario and provide you more information.
Best Regards,
Thanks for looking into it. In the attached document the merge fields for “soil type” have the prefixes I am referring to.
I am looking at alternative ways of doing what I need. One possibility for me could be the mapped data fields. By using those I think I can name my merge fields whatever I like as long as I have them mapped to the database field I want. Perhaps, then I can add a special character to the merge field name and check for that when executing the mail merge.
Hi Jacob,
Thank you for the additional information and sorry for the delayed response. I think, to be able to track merge fields based on prefixes and insert check boxes, the following code snippet would be help full.
private class HandleMergeFieldInsertCheckBox
: IFieldMergingCallback
: IFieldMergingCallback
{
void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
{
NodeCollection
fields = e.Document.GetChildNodes(NodeType.FieldStart,
true);
fields = e.Document.GetChildNodes(NodeType.FieldStart,
true);
foreach
(FieldStart f in
fields)
(FieldStart f in
fields)
{
if
(f.FieldType.Equals(FieldType.FieldMergeField))
(f.FieldType.Equals(FieldType.FieldMergeField))
{
FieldSeparator
sep = (FieldSeparator)FindNextSibling(f, NodeType.FieldSeparator);
sep = (FieldSeparator)FindNextSibling(f, NodeType.FieldSeparator);
if
(sep.NextSibling.Range.Text.StartsWith("#")
&&
(sep.NextSibling.Range.Text.StartsWith("#")
&&
sep.NextSibling.Range.Text.Contains(e.FieldName))
{
DocumentBuilder
builder = new DocumentBuilder(e.Document);
builder = new DocumentBuilder(e.Document);
builder.MoveToMergeField(e.FieldName);
string
checkBoxName = string.Format("{0}{1}", e.FieldName, e.FieldValue);
checkBoxName = string.Format("{0}{1}", e.FieldName, e.FieldValue);
builder.InsertCheckBox(checkBoxName, true,
0);
0);
return;
}
}
}
}
void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
{
// Do
nothing.
nothing.
}
}
public static Node
FindNextSibling(Node startNode, NodeType nodeType)
FindNextSibling(Node startNode, NodeType nodeType)
{
for (Node node = startNode; node != null; node = node.NextSibling)
{
if
(node.NodeType.Equals(nodeType))
(node.NodeType.Equals(nodeType))
return
node;
node;
}
return null;
}
Please let me know if I can be of any further assistance.
Best Regards,