我创建了Microsoft Word模板,我需要将数据传递给word模板。
我使用了以下代码,但是在这段代码之后,word应用程序任务没有从任务管理器中删除,如何解决?
Word关闭时,WINWORD.EXE仍在运行
private void button1_Click(object sender, EventArgs e)
{
Object oMissing = System.Reflection.Missing.Value;
Object oTemplatePath = "D:\\template.dotx";
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.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 (fieldName == "lbl_name")
{
myMergeField.Select();
wordApp.Selection.TypeText("my data");
}
}
}
wordDoc.SaveAs("D:\\myfile.doc");
wordDoc.SaveAs2("D:\\myfile.pdf", WdSaveFormat.wdFormatPDF);
wordDoc.Close();
wordApp.Application.Quit();
}
答案 0 :(得分:2)
并使用以下代码:
private void button1_Click(object sender, EventArgs e)
{
Object oMissing = System.Reflection.Missing.Value;
Object oTemplatePath = "D:\\template.dotx";
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.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 (fieldName == "lbl_name")
{
myMergeField.Select();
wordApp.Selection.TypeText("my data");
}
}
}
wordDoc.SaveAs("D:\\myfile.doc");
wordDoc.SaveAs2("D:\\myfile.pdf", WdSaveFormat.wdFormatPDF);
wordDoc.Close();
wordApp.Application.Quit();
}