How do I add custom properties to a Word doc with .NET 4?

☆樱花仙子☆ 提交于 2019-12-01 03:34:11

问题


Using .NET 4, how do I add custom properties to a document?

I'm assuming it goes something like this:

WordApp // an instance of Microsoft.Office.Interop.Word.Application
  .ActiveDocument
  .CustomDocumentProperties
  .Add...?

I cannot seem to find documentation for this that applies to .NET4/interops v14.


回答1:


It took a lot of guessing (much more than 12 minutes worth, I'm embarrassed to say!) to figure this out:

WordApp // an instance of Microsoft.Office.Interop.Word.Application
  .ActiveDocument
  .CustomDocumentProperties
  .Add(Name: "PropertyName", 
       LinkToContent: false, 
       Type: 4, 
       Value: "PropertyValue");

I couldn't find a decent enum for the types, so I dug the magic number "4" out of a forum post for string and it works...

For casual browsers, this was tricky because CustomDocumentProperties is dynamic, so I get no Intellisense. And for some reason, I cannot find docs on this.




回答2:


The magic number 4 isn't that magic. Here is the enum you can use: (part of microsoft.office.core)

public enum MsoDocProperties
{
    msoPropertyTypeNumber = 1,
    msoPropertyTypeBoolean = 2,
    msoPropertyTypeDate = 3,
    msoPropertyTypeString = 4,
    msoPropertyTypeFloat = 5,
}



回答3:


Use DSO File to Read/Write custom properties of Office documents. In fact DSO file works on any file format. DSO is Microsoft Developer Support OLE File Property Reader to read and write document properties of Microsoft Word, Microsoft Excel, Microsoft PowerPoint, and Microsoft Visio files, independent of the application that created the file. A sample application is also included with this download. Please try this :) DSO File Download.



来源:https://stackoverflow.com/questions/2329837/how-do-i-add-custom-properties-to-a-word-doc-with-net-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!