【问题标题】:How to get a type of a word field如何获取单词字段的类型
【发布时间】:2020-01-28 10:25:28
【问题描述】:

我有一个包含不同字段的 Word 文件。我想知道每个字段的类型。我知道有一个名为Field.GetType() 的方法,但它返回的是这样的:System.__ComObject

但我想知道WdFieldType 每个字段都有什么。 https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.word.wdfieldtype?view=word-pia#Microsoft_Office_Interop_Word_WdFieldType_wdFieldIncludeText

问题是,我不知道人们使用什么归档类型。当我尝试获取 Result 时,某些字段类型会导致错误。 这就是为什么我想排除这些类型,但我首先必须知道有哪些类型。

我的代码:

foreach (string LeitudNimi in Nimed)
{
    foreach (Field f in doc.Fields)
    {
        if (f.Type != WdFieldType.some field that is causing error)
        {
            if (f.Result.Text.ToLower() == LeitudNimi)
            { 
                f.Result.Text = $"{Nimi}"; 
                f.Unlink(); 
            }
        }
    }
}

并使用该代码。我首先打算做这样的事情:

foreach (Field f in doc.Fields)
                        {var Type1 = f.GetType()
File.AppendAllText(@"C:\tulemus.txt", $"Field type is: {Type1}" + Environment.NewLine);}

【问题讨论】:

  • String Type1 == f.GetType() 你知道 C# 吗? ... GetType()object.GetType() ... 你需要获取类型属性 var fieldType = f.Type.ToString()
  • 谢谢。对不起,我只是在学习 C#。 == 是错误的。

标签: c# ms-word office-interop word-field


【解决方案1】:

我认为您应该检查 [f.Type] (reference) 的值,就像您在第一个代码示例中所做的那样。 f.GetType() 将返回 f 持有的实例的 C# 对象类型

foreach (Field f in doc.Fields)
{
  File.AppendAllText(@"C:\tulemus.txt", $"Field type is: {f.Type}" + Environment.NewLine);
}

【讨论】:

    猜你喜欢
    • 2015-05-21
    • 2011-05-27
    • 2016-10-04
    • 2018-05-16
    • 2018-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多