2009-07-16 84 views
12

我試圖將自定義屬性添加到我以編程方式創建的工作簿。我有一個方法來獲取和設置屬性,但問題是工作簿正在爲CustomDocumentProperties屬性返回null。我無法弄清楚如何初始化此屬性,以便我可以從工作簿中添加和檢索屬性。 Microsoft.Office.Core.DocumentProperties是一個接口,所以我不能去,然後執行以下以編程方式訪問Excel自定義文檔屬性

if(workbook.CustomDocumentProperties == null) 
    workbook.CustomDocumentProperties = new DocumentProperties; 

這裏是我得和設置屬性代碼:

 private object GetDocumentProperty(string propertyName, MsoDocProperties type) 
    { 
     object returnVal = null; 

     Microsoft.Office.Core.DocumentProperties properties; 
     properties = (Microsoft.Office.Core.DocumentProperties)workBk.CustomDocumentProperties; 

     foreach (Microsoft.Office.Core.DocumentProperty property in properties) 
     { 
      if (property.Name == propertyName && property.Type == type) 
      { 
       returnVal = property.Value; 
      } 
      DisposeComObject(property); 
     } 

     DisposeComObject(properties); 

     return returnVal; 
    } 

    protected void SetDocumentProperty(string propertyName, string propertyValue) 
    { 
     DocumentProperties properties; 
     properties = workBk.CustomDocumentProperties as DocumentProperties; 

     bool propertyExists = false; 
     foreach (DocumentProperty prop in properties) 
     { 
      if (prop.Name == propertyName) 
      { 
       prop.Value = propertyValue; 
       propertyExists = true; 
      } 
      DisposeComObject(prop); 

      if(propertyExists) break; 
     } 

     if (!propertyExists) 
     { 
      properties.Add(propertyName, false, MsoDocProperties.msoPropertyTypeString, propertyValue, Type.Missing); 
     } 

     DisposeComObject(propertyExists); 

    } 

線 性質= workBk.CustomDocumentProperties作爲DocumentProperties; 總是將屬性設置爲null。

這是使用Microsoft.Office.Core v12.0.0.0和Microsoft.Office.Interop.Excell v12.0.0.0(Office 2007中)

回答

10

我看了看自己的代碼,並可以看到我的訪問使用後期綁定的屬性。我不記得爲什麼,但我會發布一些代碼以防萬一。

object properties = workBk.GetType().InvokeMember("CustomDocumentProperties", BindingFlags.Default | BindingFlags.GetProperty, null, workBk, null); 

object property = properties.GetType().InvokeMember("Item", BindingFlags.Default | BindingFlags.GetProperty, null, properties, new object[] { propertyIndex }); 

object propertyValue = property.GetType().InvokeMember("Value", BindingFlags.Default | BindingFlags.GetProperty, null, propertyWrapper.Object, null); 

編輯:啊,我現在還記得why。 :-)

編輯2:Jimbojones'的答案 - 使用動態關鍵字 - 是一個更好的解決方案(如果你看重易用性的使用超過使用dynamic的性能開銷)。

+0

準確地說,我找到了這個鏈接,並且當您發佈此內容時我發佈了我的代碼。 +1並接受爲您:-) – 2009-07-16 14:18:46

+0

更新以鏈接「爲什麼」:https://support.microsoft.com/zh-cn/kb/303296 – 2016-02-09 18:18:37

7

我找到了解決方案here

這裏是我結束了代碼:

public void SetDocumentProperty(string propertyName, string propertyValue) 
    { 
     object oDocCustomProps = workBk.CustomDocumentProperties; 
     Type typeDocCustomProps = oDocCustomProps.GetType(); 

     object[] oArgs = {propertyName,false, 
       MsoDocProperties.msoPropertyTypeString, 
       propertyValue}; 

     typeDocCustomProps.InvokeMember("Add", BindingFlags.Default | 
            BindingFlags.InvokeMethod, null, 
            oDocCustomProps, oArgs); 

    } 

    private object GetDocumentProperty(string propertyName, MsoDocProperties type) 
    { 
     object returnVal = null; 

     object oDocCustomProps = workBk.CustomDocumentProperties; 
     Type typeDocCustomProps = oDocCustomProps.GetType(); 


     object returned = typeDocCustomProps.InvokeMember("Item", 
            BindingFlags.Default | 
            BindingFlags.GetProperty, null, 
            oDocCustomProps, new object[] { propertyName }); 

     Type typeDocAuthorProp = returned.GetType(); 
     returnVal = typeDocAuthorProp.InvokeMember("Value", 
            BindingFlags.Default | 
            BindingFlags.GetProperty, 
            null, returned, 
            new object[] { }).ToString(); 

     return returnVal; 
    } 

有些異常處理是必要的手,如果物業犯規時存在檢索

3

晚回答這個問題,但我制定了一個簡單的方法來添加將來可能對某人有用的自定義DocumentProperties。

我的問題是用System.String.GetType()提供的系統類型調用Add()方法觸發了COMException:類型不匹配。參照在以前的答案的鏈接很明顯,這種方法需要一個Office特定類型,從而使最終爲我工作的代碼是:

var custProps = (Office.DocumentProperties)this.CustomDocumentProperties; 
custProps.Add("AProperty", false, MsoDocProperties.msoPropertyTypeString, "AStringProperty"); 

因爲它是一個CustomDocumentProperty辦公室將添加自定義屬性沒有困難,但如果您需要檢查存在或驗證CustomDocumentProperty可能不存在時的值,您將不得不捕獲一個System.ArgumentException。

編輯

正如奧利弗·博克的評論指出,這是一個Office 2007和僅佔解決方案,據我所知。

12

如果你的目標是.NET 4。0,您可以使用dynamic關鍵字進行後期綁定

Document doc = GetActiveDocument(); 
if (doc != null) 
{ 
    dynamic properties = doc.CustomDocumentProperties; 
    foreach (dynamic p in properties) 
    { 
     Console.WriteLine(p.Name + " " + p.Value); 
    } 
} 
相關問題