Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 373
Michael Haren Avatar asked Feb 24 '10 21:02

Michael Haren


People also ask

How do you add custom properties to a Word document?

Click the File tab. Click Info to view the document properties. To add or change properties, hover your pointer over the property you want to update and enter the information. Note that for some metadata, such as Author, you'll have to right-click on the property and choose Remove or Edit.

How can you add a custom property to a form in C#?

On the menu bar, choose View > Properties Window. The Property Editor dialog box appears. In the text box in the Name column, specify the name of the property. For the Type field of the custom property, choose the appropriate data type.

How do I add an author property control in Word?

Go to Insert > Header or Footer. Select Edit Header or Edit Footer. Select Quick Parts, and select Field. In the Field names list, choose the field you want (such as FileName, Date, Author, or Title), choose the format you want in the Field properties section.


3 Answers

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.

like image 120
Michael Haren Avatar answered Jan 01 '23 07:01

Michael Haren


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,
}
like image 39
Eddy Avatar answered Jan 01 '23 07:01

Eddy


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.

like image 39
Ravi Shankar Avatar answered Jan 01 '23 08:01

Ravi Shankar