在C#中列出word文档的属性

时间:2011-11-08 08:34:14

标签: c# reflection properties ms-word

我想要一些帮助来检索word文档的属性。我已经获得了标题,主题和作者。但我似乎无法获得“Date Last Saved”属性,我不知道如何获取属性名称列表。

我的代码如下所示:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop;
using System.Reflection;
using System.IO;


namespace MetaDataSorter
{
    class Program
    {

    static void Main(string[] args)
    {
        String dirName = @"H:\projekt\test raw files";

        String fileNameString = @"H:\projekt\raw files\vgahm\1 NTFS\Raw Files\Microsoft Word Document\1-300\FILE006.DOC";
        object fileName = (object)fileNameString;

        object missing = System.Reflection.Missing.Value;

        Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();


        Microsoft.Office.Interop.Word.Document aDoc = null;

        if (File.Exists((string)fileName))
        {
            DateTime toDay = DateTime.Now;

            object readOnly = false;
            object isVisible = false;

            wordApp.Visible = false;

            aDoc = wordApp.Documents.Open(ref fileName, ref missing, 
                ref readOnly, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing);

            aDoc.Activate();

            //object property = getWordDocumentPropertyValue(aDoc, "Title");

            System.Console.WriteLine("property: " + getWordDocumentPropertyValue(aDoc, "Title"));
            System.Console.WriteLine("property: " + getWordDocumentPropertyValue(aDoc, "Subject"));
            System.Console.WriteLine("property: " + getWordDocumentPropertyValue(aDoc, "Author"));
            //System.Console.WriteLine("property: " + getWordDocumentPropertyValue(aDoc, "Date Last Saved"));

            aDoc.Close();
        }

    }

    private static String getWordDocumentPropertyValue(Microsoft.Office.Interop.Word.Document document, string propertyName)
    {
        object builtInProperties = document.BuiltInDocumentProperties;

        Type builtInPropertiesType = builtInProperties.GetType();

        object property = builtInPropertiesType.InvokeMember("Item", BindingFlags.GetProperty, null, builtInProperties, new object[] { propertyName });

        Type propertyType = property.GetType();
        object propertyValue = propertyType.InvokeMember("Value", BindingFlags.GetProperty, null, property, new object[] { });
        return propertyValue.ToString();
    }

}
}

我应该如何获取属性值列表?

2 个答案:

答案 0 :(得分:1)

您可以先看看 BuiltInDocumentProperties 类,或者使用此链接作为起点,http://support.microsoft.com/default.aspx?scid=kb;en-us;303294

请记住,某些属性特定于办公套件中的某些产品,有些属性很常见。你正在寻找的那个肯定是一个普通的。

关于word,请在此处找到列表http://msdn.microsoft.com/de-de/library/microsoft.office.interop.word.wdbuiltinproperty%28v=office.11%29.aspx

答案 1 :(得分:-1)

将此代码粘贴到VBA模块中的Word文档中,您将获得属性列表。

Sub ListeProprietes()
   Dim proDoc As DocumentProperty

   For Each proDoc In ActiveDocument.BuiltInDocumentProperties

        MsgBox (proDoc.Name)

   Next

End Sub