列出c#中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:projekttest raw files";
        String fileNameString = @"H:projektraw filesvgahm1 NTFSRaw FilesMicrosoft Word Document1-300FILE006.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();
    }
}
}

我应该如何去获得属性值的列表?

您可以从查看类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

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

Sub ListeProprietes()
   Dim proDoc As DocumentProperty
   For Each proDoc In ActiveDocument.BuiltInDocumentProperties
        MsgBox (proDoc.Name)
   Next
End Sub

相关内容

  • 没有找到相关文章

最新更新