读取BuiltInDocumentProperties/CustomDocumentProperties在Word 2010中是否始终为null? [英] Read BuiltInDocumentProperties/CustomDocumentProperties alway null with Word 2010?

查看:237
本文介绍了读取BuiltInDocumentProperties/CustomDocumentProperties在Word 2010中是否始终为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读出Word文档的BuiltInDocumentProperties/CustomDocumentProperties.以下Source始终返回null:-(

I want to read out the BuiltInDocumentProperties/CustomDocumentProperties of an Word document. The following Source always return null :-(

using Microsoft.Office.Core;
using Word = Microsoft.Office.Interop.Word;
.....
    private void toolStripMenuItemTmp_Click(object sender, EventArgs e)
    {
        Word.Application word = new Word.Application();
        Word.Document document = word.Documents.Open(@"C:\Users\fillibuster\Desktop\docproperty.docx");
        DocumentProperties properties = (DocumentProperties)document.CustomDocumentProperties;
        if (properties != null)
        {
            foreach (Microsoft.Office.Core.DocumentProperty item in properties)
            {
                MessageBox.Show(item.Name.ToString() + item.Value.ToString());
            }
        }
        else
        {
            MessageBox.Show("null");
        }

    }

来源有什么问题?可以使用CustomDocumentProperties和BuiltInDocumentProperties并将其填写在文档中!

What's wrong with the source? CustomDocumentProperties and BuiltInDocumentProperties are available and filled in the document!

推荐答案

.docx文档存在相同的问题.一种解决方法是忘记类型转换,而将dynamicobject保留为类型,然后代码起作用.我怀疑.docx文件的COM属性不是MSDN中描述的类型...

I had the same issue with .docx document. One way to get through was to forget about type casting and instead keep dynamic and object as types and then the code worked. I suspect that the COM property of a .docx file is not the type described in the MSDN...

因此,此代码捕获原始文档的属性并将其设置在Dictionary中.

So this code captures the raw document's properties and set them in a Dictionary.

  try
  {
       BuiltInDocumentProperties = new Dictionary<string, string>();
       var builtinProps = Doc.BuiltInDocumentProperties; // don't strong cast this or you will get null
       SetBuiltInProperty(builtinProps, "Title");
       SetBuiltInProperty(builtinProps, "Keywords");
  }
  catch (Exception e)
  {
       // Ignorer l'erreur
       Log.Warn("Erreur inattendue à la lecture des propriétés internes du document", e);
  }

  IDictionary<string, string> BuiltInDocumentProperties { get; set; }

  internal void SetBuiltInProperty(dynamic builtInProps, string property)
  {
      if (builtInProps != null)
      {
          try
          {
              var prop = builtInProps[property];
              if (prop != null)
              {
                  string str = prop.Value.ToString();
                  BuiltInDocumentProperties[property] = str;
              }
          }
          catch (RuntimeBinderException)
          {
              // Property is missing
          }
          catch (COMException)
          {

          }
      }
 }

这篇关于读取BuiltInDocumentProperties/CustomDocumentProperties在Word 2010中是否始终为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆