我正在尝试弄清楚如何遍历文档并提取所有表单域并将它们插入到新的文本文件中。我正在研究它,试图找到我将需要的功能的示例,但我没有提供很多信息。也许我没有正确搜索。这是我到目前为止所写的内容。
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[]);
}
}
}
如何获取合并域?
最佳答案
我以前用过这样的代码。
foreach(Field wdField in workDoc.Fields)
{
if (wdField.Type == WdFieldType.wdFieldMergeField)
{
wdField.Select();
string fieldText = wdField.Result.Text;
}
}
关于C# 从 Word 文档中检索 FormFields 并插入到文本文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9518275/