填写单词模板时,WINWORD.EXE仍在运行

时间:2017-03-31 11:28:56

标签: c# ms-word

我创建了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();
    }

1 个答案:

答案 0 :(得分:2)

  1. 添加对Microsoft Word 12.0对象库的引用。
  2. 创建Word模板。
  3. 选择单词中的插入标签。
  4. 选择快速部件。然后是Field。
  5. 选择字段类别为MergeField。将MergeField名称指定为“lbl_name”。
  6. enter image description here

    enter image description here

    enter image description here

    并使用以下代码:

       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();
        }