如何为Office文件创建和修改自定义文档属性

问题描述 投票:-3回答:1

我正在关注this文章,但是Office.DocumentProperties引发错误,表明Office没有定义DocumentProperties。

private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
    {
        Microsoft.Office.Core.DocumentProperties properties;

        var activeDocument = Globals.ThisDocument.Application.ActiveDocument;

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

        MessageBox.Show(JsonConvert.SerializeObject(properties));
    }
c# ms-word vsto ms-office office-addins
1个回答
0
投票

How to: Read from and write to document properties文章介绍了如何读取和写入文档属性。以下代码在我的PC上就像一个超级按钮:

Word.Document doc = WordApp.ActiveDocument as Word.Document; 
 Office.DocumentProperties properties = doc.CustomDocumentProperties as Office.DocumentProperties; 
 for (int i = 0; i < properties.Count; i++) 
 { 
     Office.DocumentProperty property = properties[i]; 
     if(property!=null) 
     { 
         System.Windows.Forms.MessageBox.Show(property.Name); 
         Marshal.ReleaseComObject(property); 
     } 
 } 
 if (properties != null) Marshal.ReleaseComObject(properties); 
 if (doc != null) Marshal.ReleaseComObject(doc); 
© www.soinside.com 2019 - 2024. All rights reserved.