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

时间:2019-10-10 13:48:59

标签: c# ms-word vsto ms-office office-addins

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

1 个答案:

答案 0 :(得分:1)

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

"build": {
    "mac": {
      "icon": "build/icon2.ico",
      "target": [
        "dmg"
      ]
    },
    "files": [
      "**/*"
      "*.js",
      "build",
      "node_modules",
      "package.json"
    ],
  },
  "scripts": {
    "pack": "electron-builder --dir",
    "dist": "electron-builder"
  }

有时候,您需要使用后期绑定技术来避免出现异常情况:

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