Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带有mergefield的Microsoft Word if语句不起作用_C#_.net_Ms Word_Office Interop_Mailmerge - Fatal编程技术网

C# 带有mergefield的Microsoft Word if语句不起作用

C# 带有mergefield的Microsoft Word if语句不起作用,c#,.net,ms-word,office-interop,mailmerge,C#,.net,Ms Word,Office Interop,Mailmerge,我正在尝试用microsoft word(microsoft.Office.Interop.word dll)和c#实现IF语句。我有一个模板如下- 我得到的结果是这样的- 我的代码是这样的 Dictionary<String, String> valueDic = new Dictionary<string, string>(); valueDic.Add("Gender", "Male"); Object oMissing = System.Reflecti

我正在尝试用microsoft word(microsoft.Office.Interop.word dll)和c#实现IF语句。我有一个模板如下-

我得到的结果是这样的-

我的代码是这样的

 Dictionary<String, String> valueDic = new Dictionary<string, string>();
 valueDic.Add("Gender", "Male");
 Object oMissing = System.Reflection.Missing.Value;
 Object oTemplatePath = @"E:\\ContactTemplate2.docx";
 Application wordApp = new Application();
 Document wordDoc = new Document();
 wordDoc = wordApp.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);
 foreach (Field myMergeField in wordDoc.Fields)
 {
   Range rngFieldCode = myMergeField.Code;
   String fieldText = rngFieldCode.Text;
   if (fieldText.StartsWith(" MERGEFIELD"))
   {
     Int32 endMerge = fieldText.IndexOf("\\");
     Int32 fieldNameLength = fieldText.Length - endMerge;
     String fieldName = fieldText.Substring(11, endMerge - 11);
     fieldName = fieldName.Trim();
     if (valueDic.ContainsKey(fieldName))
     {
       myMergeField.Select();
       wordDoc.Application.Selection.TypeText(valueDic[fieldName]);
     }
     else
     {
       myMergeField.Select();
       wordDoc.Application.Selection.TypeText(" ");
     }
   }
 }
 wordDoc.SaveAs(@"E:\\myfile.docx");
 wordApp.Application.Quit();
Dictionary valueDic=newdictionary();
添加(“性别”、“男性”);
对象omising=System.Reflection.Missing.Value;
对象oTemplatePath=@“E:\\ContactTemplate2.docx”;
Application wordApp=新应用程序();
Document wordDoc=新文档();
wordDoc=wordApp.Documents.Add(ref-oTemplatePath,ref-omising,ref-omising,ref-omising);
foreach(wordDoc.Fields中的字段myMergeField)
{
范围rngFieldCode=myMergeField.Code;
字符串fieldText=rngFieldCode.Text;
if(fieldText.StartsWith(“MERGEFIELD”))
{
Int32 endMerge=fieldText.IndexOf(“\\”);
Int32 fieldNameLength=fieldText.Length-endMerge;
字符串fieldName=fieldText.Substring(11,endMerge-11);
fieldName=fieldName.Trim();
if(valueDic.ContainsKey(字段名))
{
myMergeField.Select();
wordDoc.Application.Selection.TypeText(valueDic[fieldName]);
}
其他的
{
myMergeField.Select();
wordDoc.Application.Selection.TypeText(“”);
}
}
}
wordDoc.SaveAs(@“E:\\myfile.docx”);
wordApp.Application.Quit();
我想要的结果是-


有人能帮我解决吗?

因此,如果合并字段的名称在您的valueDic列表中,您想用它的名称替换合并字段吗

如果是这样的话,我建议您接下来需要这样做(您可能需要更正语法)

如果删除集合的成员,还需要验证字段迭代是否仍然有效。否则,您可能必须使用计数器向后迭代。您可能还需要注意正在使用的任何范围的TextRetrievalMode的IncludeFieldCodes和IncludeHiddenText属性

(注意:如果您这样做,Word会将{If Male=“Male”}解释为“Male”和“Male”之间的字符串比较,除非有一个名为“Male”的书签,在这种情况下,Word会将书签值与“Male”进行比较)

if (valueDic.ContainsKey(fieldname))
{
  Range rngFieldResult = myMergeField.Result;
  myMergeField.Unlink();
  Range.Text = valueDic[fieldName];
}