MS Word Office Automation - Filling Text Form Fields And Check Box Form Fields And Mail Merge

陌路散爱 提交于 2019-11-30 07:13:31

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!