我正在试图弄清楚如何遍历文档并拉出所有表单字段并将它们插入到新的文本文件中。我正在努力寻找我将需要的功能的例子,我没有提出很多信息。也许我不是在搜索。这是我到目前为止所写的内容。
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[]);
}
}
}
如何获得合并域?
答案 0 :(得分:6)
我过去曾使用过这样的代码。
foreach(Field wdField in workDoc.Fields)
{
if (wdField.Type == WdFieldType.wdFieldMergeField)
{
wdField.Select();
string fieldText = wdField.Result.Text;
}
}
答案 1 :(得分:-1)
你可以使用这样的东西。您可以看到我在大多数代码上插入了注释,因为我只使用了“获取书签名称”部分。您还可以获得文本表单字段的格式。
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++;
// }
}