C#从Word文档中检索FormFields并将其插入到文本文件中 [英] C# Retrieve FormFields from Word Document and Insert into Text file

查看:349
本文介绍了C#从Word文档中检索FormFields并将其插入到文本文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何遍历文档并提取所有表单字段并将其插入新文本文件的方法.我正在尝试寻找所需功能的示例,但并没有提供很多信息.也许我搜索不正确.这是我到目前为止写的.

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[]);



        }
    }
}

如何获取合并字段?

推荐答案

我过去曾经使用过这样的代码.
还要注意,FormFields不是mergefields

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

这篇关于C#从Word文档中检索FormFields并将其插入到文本文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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