MS Word Office Automation - 填写文本表单字段和复选框表单字段和邮件合并

时间:2009-09-03 02:04:10

标签: c# vb.net ms-word ms-office

有没有人对如何使用C#创建引擎有任何好的建议或经验(VB.NET也可以),它足够通用,可以处理大多数MS Word文本字段的情况,我需要填写我正在获取的数据从数据库?简而言之,我即将开始这个小小的Office自动化游览,我希望这里的一些反馈可以帮助我避免一些耗时的错误。

欢迎并提前感谢任何建议;

戴夫

1 个答案:

答案 0 :(得分:15)

我将发送两个示例来解决您的自动化问题。第一个是使用MailMerge,第二个是使用书签。

word文件如下所示:

使用MailMerge(插入 - >快速部分 - >字段 - >邮件合并 - >合并字段) 名字:«firstName» 姓氏:«lastName»

=======

使用书签(插入 - > BookMark) 名字:(< - 书签在这里,它不可见) 姓氏:

代码如下:

  1. 使用书签

        Open("D:/Doc1.doc");
        if (oDoc.Bookmarks.Exists("bkmFirstName"))
        {
            object oBookMark = "bkmFirstName";
            oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = textBox1.Text;
        }
    
        if (oDoc.Bookmarks.Exists("bkmLastName"))
        {
            object oBookMark = "bkmLastName";
            oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = textBox2.Text;
        }
    
        SaveAs("D:/Test/Doc2.doc"); Quit();
        MessageBox.Show("The file is successfully saved!");
    
  2. 使用MailMerge

        Open("D:/Doc1.doc");
        foreach (Field myMergeField in oDoc.Fields)
        {
            //iTotalFields++;
            Range rngFieldCode = myMergeField.Code;
            String fieldText = rngFieldCode.Text;
    
            // GET only MAILMERGE fields
            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 == "firstName")
                {
                    myMergeField.Select();
                    oWordApplic.Selection.TypeText("This Text Replaces the Field in the Template");
                }
            }
        }
        SaveAs("D:/Test/Doc2.doc"); Quit();
        MessageBox.Show("The file is successfully saved!");
    
  3. 我也使用了一些辅助方法。

        ApplicationClass oWordApplic = new Microsoft.Office.Interop.Word.ApplicationClass();
        private Microsoft.Office.Interop.Word.Document oDoc = new Document();
    
        public void Open(string strFileName)
        {
            object fileName = strFileName;
            object readOnly = false;
            object isVisible = true;
            object missing = System.Reflection.Missing.Value;
    
            oDoc = oWordApplic.Documents.Open(ref fileName, ref missing, ref readOnly,
            ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
    
            oDoc.Activate();
        }
    
        public void SaveAs(string strFileName)
        {
            object missing = System.Reflection.Missing.Value;
            object fileName = strFileName;
    
            oDoc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
        }
    
        public void Quit()
        {
            object missing = System.Reflection.Missing.Value;
            oWordApplic.Application.Quit(ref missing, ref missing, ref missing);
        }
    

    我希望这个实现会为解决你的问题提供一些想法。