[英]How to read MS word form fields using c#.net
我試圖閱讀包含文本和表單字段的單詞文檔。 我需要閱讀文檔中的所有文本和字段。 但是下面的代碼總是返回空值。 它永遠不會進入foreach循環。 我不知道這是什么問題,因為構建時沒有錯誤。 但是我沒有得到輸出。 我用c#.net 4.6.2編寫它,它將用作庫文件。 代碼有什么問題嗎?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Word;
using System.Activities;
using System.ComponentModel;
namespace WordExer
{
public class WordExer : CodeActivity
{
[Category("Input")]
public InArgument<string> AVal { get; set; }
[Category("Output")]
public OutArgument<string> CVal { get; set; }
protected override void Execute(CodeActivityContext context)
{
var a = AVal.Get(context);
string text = "";
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = word.Documents.Add(a);
doc.Activate();
foreach (FormField field in doc.FormFields)
{
Console.WriteLine(field.Range.Text);
text += field.Range.Text;
}
CVal.Set(context, text);
word.Quit();
}
}
}
您可以嘗試以內聯形狀訪問它們,如下面的代碼片段所示
foreach (InlineShape shape in doc.InlineShapes)
{
if (shape.OLEFormat != null && shape.OLEFormat.ClassType == "CONTROL Forms.TextBox.1")
{
Console.WriteLine("Data :" + shape.OLEFormat.Object.Value);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.