I use a Word document as a template as following:
Hello my name is {MERGEFIELD Name /*MERGEFORMAT}
{IF {MERGEFIELD Gender /*MERGEFORMAT} = "M"
"Your gender is {MERGEFIELD Gender /*MERGEFORMAT}" ""}
My C# program looks like :
Microsoft.Office.Interop.Word.Document documentWord = application.Documents.Open(myWordTemplate);
//Gender and Name are dynamic fields for the example
var myDynamicFields = dbContext.MyFields.All();
foreach (Microsoft.Office.Interop.Word.Field field in documentWord.Fields)
{
foreach (var item in myDynamicFields)
{
//Search for a "Name" or "Gender" field
if (field.Code.Text.Contains(item.Key))
{
field.Select();
text = item.Value + " ";
application.Selection.TypeText(text);
break;
}
}
}
So, my problem is that my code detects 2 Mergefield only : the Name MergeField and the field if. I'd like to replace the Mergefield Gender in the field if. How can I do this?