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

发布于 2024-08-04 06:33:58 字数 177 浏览 2 评论 0原文

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

提前欢呼并感谢您的任何建议;

戴夫

Does anyone have any good advice or experience on how to create an engine using C# (VB.NET is okay too) that is generic enough to handle most cases of MS Word text fields I need to fill with data I'm getting from a database? In short, I'm about to embark on this little Office automation excursion and I'm hoping a little bit of feedback here may help me to avoid some time consuming errors.

Cheers and thanks in advance for any advice;

Dave

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿挽旧人 2024-08-11 06:33:58

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

Word 文件如下所示:

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

=======

使用书签(插入 -> 书签)
名字:(<- 书签在此处,但不可见)
姓氏:

代码如下:

  1. 使用书签

     打开("D:/Doc1.doc");
        if (oDoc.Bookmarks.Exists("bkmFirstName"))
        {
            对象 oBookMark = "bkmFirstName";
            oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = textBox1.Text;
        }
    
        if (oDoc.Bookmarks.Exists("bkmLastName"))
        {
            对象 oBookMark = "bkmLastName";
            oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = textBox2.Text;
        }
    
        SaveAs("D:/Test/Doc2.doc");辞职();
        MessageBox.Show("文件保存成功!");
    
  2. 使用 MailMerge

     打开("D:/Doc1.doc");
        foreach(oDoc.Fields 中的字段 myMergeField)
        {
            //iTotalFields++;
            范围 rngFieldCode = myMergeField.Code;
            字符串 fieldText = rngFieldCode.Text;
    
            // 仅获取 MAILMERGE 字段
            if (fieldText.StartsWith(" MERGEFIELD"))
            {
                Int32 endMerge = fieldText.IndexOf("\\");
                Int32 fieldNameLength = fieldText.Length - endMerge;
                字符串 fieldName = fieldText.Substring(11, endMerge - 11);
    
                字段名称 = 字段名称.Trim();
                if (字段名 == "名字")
                {
                    myMergeField.Select();
                    oWordApplic.Selection.TypeText("此文本替换模板中的字段");
                }
            }
        }
        SaveAs("D:/Test/Doc2.doc");辞职();
        MessageBox.Show("文件保存成功!");
    

我还使用了一些辅助方法。

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

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

I will sent two examples for solving your automation problem. The first one is using MailMerge and the second is using bookmarks.

The word file looks like this:

Using MailMerge (Insert - > Quick Parts -> Field -> Mail merge -> Merge field)
First name: «firstName»
Last name: «lastName»

=======

Using Bookmarks( Insert -> BookMark)
First name: (<- the bookmark is here, it’s not visible)
Last name:

And the code is following:

  1. Using bookmarks

        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. Using 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!");
    

I've also used some helper methods.

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

I hope that this implementation will give some ideas for solving your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文