2011-11-08 11 views
2

我想獲得一些幫助來檢索word文檔的屬性。我已經獲得了標題,主題和作者。但我似乎無法獲得「Date Last Saved」屬性,我不知道如何獲取屬性名稱列表。C#中的word文檔的列表屬性

我的代碼如下所示:

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(); 
    } 

} 
} 

我應該如何去獲取屬性值的列表?

回答

1

您可以通過在看看類BuiltInDocumentProperties或使用該鏈接爲出發點可能會開始,http://support.microsoft.com/default.aspx?scid=kb;en-us;303294

記住,有些屬性是特定於某些Office產品套件中,有些是常見的。你正在尋找的那個肯定是常見的。

關於單詞,在列表中找到這裏http://msdn.microsoft.com/de-de/library/microsoft.office.interop.word.wdbuiltinproperty%28v=office.11%29.aspx

+1

謝謝!但這並不奏效。我在添加使用辦公室內容所需的引用時遇到了問題。我解決了它,但是通過絆倒http://www.codeproject.com/KB/office/MsWordAutoDocProp.aspx?fid=260729&df=90&mpp=25&noise=3&prof=False&sort=Position&view=Quick&fr=18這表明我可以使用「Last節省時間「,這是屬性名稱:) – user1028037

-1

一個VBA模塊,在這個代碼粘貼到Word文檔中,你會得到的屬性列表。

Sub ListeProprietes() 
    Dim proDoc As DocumentProperty 

    For Each proDoc In ActiveDocument.BuiltInDocumentProperties 

     MsgBox (proDoc.Name) 

    Next 

End Sub