我正在关注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));
}
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);