我使用 Word 文档作为模板,如下所示:
Hello my name is {MERGEFIELD Name /*MERGEFORMAT}
{IF {MERGEFIELD Gender /*MERGEFORMAT} = "M"
"Your gender is {MERGEFIELD Gender /*MERGEFORMAT}" ""}
我的 C# 程序如下所示:
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;
}
}
}
所以,我的问题是我的代码只检测到 2 个 Mergefield:Name MergeField 和字段 if。我想替换该字段中的 Mergefield Gender if。我怎样才能做到这一点?