C#从Word文档中检索FormFields并插入到文本文件中
本文关键字:插入 文本 文件 检索 Word 文档 FormFields | 更新日期: 2023-09-27 18:26:24
我正试图弄清楚如何遍历文档,提取所有表单字段并将它们插入到一个新的文本文件中。我正在努力寻找我需要的函数的例子,但我没有得到太多信息。也许我没有正确搜索。以下是我迄今为止所写的内容。
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不是合并字段
foreach(Field wdField in workDoc.Fields)
{
if (wdField.Type == WdFieldType.wdFieldMergeField)
{
wdField.Select();
string fieldText = wdField.Result.Text;
}
}
您可以使用这样的东西。你可以看到,我在大部分代码上都插入了注释,因为我只使用了"获取书签名称"部分。您还可以获得文本表单字段的格式。
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++;
// }
}