繁体   English   中英

如何使用c#.net读取MS字形字段

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM