Microsoft Word (Microsoft.Office.Interop.Word dll) 및 C#을 사용하여 IF 문을 구현하려고합니다. 나는 다음과 같이 템플릿이 -
Microsoft Word에서 mergefield 문이 작동하지 않습니다.
을 나는 다음과 같은 결과를 얻고있다 -
내 코드는 다음과
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();
처럼 내가 같은 결과를 원하는 -
누군가 나를 도와 줄 수 있습니까?