C# Retrieve FormFields from Word Document and Inse

2019-08-11 02:59发布

问题:

I'm trying to figure out how to iterate through a document and pull all form fields and insert them into a new text file. I'm working through it trying to find examples of the functions I'm going to need and I'm not coming up with a lot of information. Maybe I'm not searching properly though. Here's what I've written so far.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Word;
using System.IO;

namespace purform
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {

            // create instance of Word 
            Microsoft.Office.Interop.Word.ApplicationClass oWordApp = new Microsoft.Office.Interop.Word.ApplicationClass();

            // create instance of Word document 
            Microsoft.Office.Interop.Word.Document oWordDoc = new Document();

            object missing = System.Reflection.Missing.Value;
            try
            {
                //declare objects
                object fileName = @"C:\\path\\to\\file.doc";
                object readOnly = false;
                object isVisible = true;

                //open word doc
                oWordDoc = oWordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref readOnly,
                ref missing, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing);

                oWordDoc.Activate();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unable to locate and activate document file");
            }

            object oFormFields = fieldArray[i, 0];
            oWordDoc.FormFields.get_Item(ref oFormFields).Range = 


           System.IO.File.WriteAllText(@"\\path\\to\\file.txt", fieldArray[]);



        }
    }
}

How can I get the mergefields?

回答1:

I have used code like this in the past.
Also note the FormFields are not mergefields

    foreach(Field wdField in workDoc.Fields)
    {
        if (wdField.Type == WdFieldType.wdFieldMergeField)
        {
            wdField.Select();
            string fieldText = wdField.Result.Text;
        }
    }


回答2:

You can use some thing like this. You can see that I've inserted comments on most of the code as I was only using the 'Get bookmark name' part. You may also get the format of the text form field.

foreach (Word.FormField oFormFields in oDoc.FormFields) {
     //To Get Bookmark Name
     if (oFormFields.Name.Contains("CompanyEntity")) {
                    //With Values
                    BMClick = BMClick + "," + oFormFields.Name;
                    BMType = oFormFields.Type.ToString();
                    BMClick = BMClick + "," + BMType + "," + oFormFields.Result + "\n";
     }

     //from here: get all BM names
     //if (oFormFields.Type == Microsoft.Office.Interop.Word.WdFieldType.wdFieldFormTextInput)
     //{

     //    ////To Get Bookmark Name
     //    //if ((oFormFields.Name.ToUpper().Contains("LR_M_IssueDate")) || (oFormFields.Name.ToUpper().Contains("LR_O_ExpiryDate")))
     //    //{
     //    //    BMClick = BMClick + "," + oFormFields.Name;
     //    //}

     //    //With Values
     //    //BMClick = BMClick + "," + oFormFields.Name;
     //    //BMType = oFormFields.Type.ToString();
     //    //BMClick = BMClick + "," + BMType + "," + oFormFields.Result + "\n"; 

     ////To check the text field format
      //    switch (((Microsoft.Office.Interop.Word.WdTextFormFieldType)oFormFields.TextInput.Type))
    //    {
    //        case Microsoft.Office.Interop.Word.WdTextFormFieldType.wdRegularText:
    //            {

    //                ////To Get Bookmark Name
    //                //if ((oFormFields.Name.ToUpper().Contains("LR_M_IssueDate")) || (oFormFields.Name.ToUpper().Contains("LR_O_ExpiryDate")))
    //                //{
    //                //    BMClick = BMClick + "," + oFormFields.Name;
    //                //}
    //                BMClick = BMClick + "," + oFormFields.Name;
    //                BMType = oFormFields.Type.ToString();
    //                BMClick = BMClick + "," + BMType;
    //                BMTextType = oFormFields.TextInput.Type.ToString();
    //                BMClick = BMClick + "," + BMTextType + "\n";
    //                break;
    //            }

    //        case Microsoft.Office.Interop.Word.WdTextFormFieldType.wdDateText:
    //            //if ((oFormFields.Name.ToUpper().Contains("LR_M_ISSUEDATE")) || (oFormFields.Name.ToUpper().Contains("LR_O_EXPIRYDATE")))
    //            //{
    //            //    BMClick = BMClick + "," + oFormFields.Name;
    //            //}
    //            BMClick = BMClick + "," + oFormFields.Name;
    //            BMType = oFormFields.Type.ToString();
    //            BMClick = BMClick + "," + BMType;
    //            BMTextType = oFormFields.TextInput.Type.ToString();
    //            BMClick = BMClick + "," + BMTextType + "\n";
    //            break;

    //        case Microsoft.Office.Interop.Word.WdTextFormFieldType.wdNumberText:
    //            BMClick = BMClick + "," + oFormFields.Name;
    //            BMType = oFormFields.Type.ToString();
    //            BMClick = BMClick + "," + BMType;
    //            BMTextType = oFormFields.TextInput.Type.ToString();
    //            BMClick = BMClick + "," + BMTextType + "\n";
    //            break;
    //        default:
    //            //MessageBox.Show("no");
    //            break;
    //    }
    //}



    //else if (oFormFields.Type == Microsoft.Office.Interop.Word.WdFieldType.wdFieldFormCheckBox)
    //{
    //    BMClick = BMClick + "," + oFormFields.Name;
    //    BMType = oFormFields.Type.ToString();
    //    BMClick = BMClick + "," + BMType + "," + oFormFields.Result + "\n";
    //}
    ////{
    ////    if (oFormFields.Name == "")
    ////    { 
    ////        Word.Range curPageRange = new Word.Range;
    ////        object AtPage = @"\page";
    ////        curPageRange = oDoc.Bookmarks.get_Item(ref AtPage).Range;
    ////        //curPageRange.Select(); 

    ////        BMClick=BMClick + "," + curPageRange;
    ////    }
    ////}

    //else if (oFormFields.Type == Microsoft.Office.Interop.Word.WdFieldType.wdFieldFormDropDown)
    //{
    //    BMClick = BMClick + "," + oFormFields.Name;
    //    BMType = oFormFields.Type.ToString();
    //    BMClick = BMClick + "," + BMType + "," + oFormFields.Result + "\n";
    //}
    ////{
    ////    if (oFormFields.Name == "")
    ////    { 
    ////        Word.Range curPageRange = new Word.Range;
    ////        object AtPage = @"\page";
    ////        curPageRange = oDoc.Bookmarks.get_Item(ref AtPage).Range;
    ////        //curPageRange.Select(); 

    ////        BMClick=BMClick + "," + curPageRange;
    ////    }
    ////}
    //until here: get all BM names

    ctr++;
    // }
}


标签: c# ms-word field