微软Word办公自动化 - 填写文本表单域和复选框表单字段和邮件合并 [英] MS Word Office Automation - Filling Text Form Fields And Check Box Form Fields And Mail Merge

查看:435
本文介绍了微软Word办公自动化 - 填写文本表单域和复选框表单字段和邮件合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人对如何使用创建引擎C#什么好的建议或经验(VB.NET是好的,太)是通用的,足以应付微软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;

戴夫

推荐答案

我会派两个例子来解决你的自动化问题。 。第一个是使用邮件合并,二是采用书签

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

Word文件看起来是这样的:

The word file looks like this:

使用邮件合并(插入 - >快速配件 - >字段 - >邮件合并 - >合并场)
的名字:«的firstName»
姓:«的lastName»

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:

和代码如下:


  1. 使用书签

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




  • 使用邮件合并

  • 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.

    这篇关于微软Word办公自动化 - 填写文本表单域和复选框表单字段和邮件合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆