Add custom document properties to word with c#

how do i: access and manipulate custom document properties in microsoft word?.
customdocumentproperties c#
c# word document properties
get word document properties c#
microsoft.office.core.document properties example
word vba modify custom document property
word interop set document properties
vba document properties

i try to add custom document properties to word with a c# tool. I can edit the values of the builtin properties like author etc.

But with that code, i get no exception, but there is no custom property in the word document.

object oDocCustomProps;
string strIndex = String.Empty;
string strValue;
Microsoft.Office.Interop.Word.Application wordApp =
    new Microsoft.Office.Interop.Word.Application { Visible = false };
Microsoft.Office.Interop.Word.Document doc =
    wordApp.Documents.Open("C:\\test.docx", ReadOnly: false, Visible: false);
oDocCustomProps = document.CustomDocumentProperties;
Type typeDocCustomProps = oDocCustomProps.GetType();
strIndex = "Testindex";
strValue = "Testvalue";
object[] oArgs = {strIndex, false, MsoDocProperties.msoPropertyTypeString, strValue};
typeDocCustomProps.InvokeMember("Add", BindingFlags.Default | BindingFlags.InvokeMethod,
    null, oDocCustomProps, oArgs);
document.save();
document.close();

Edit

    public void addCustomDocumentPropertys()
    {

        object oMissing = Missing.Value;
        object oDocBuiltInProps;
        object oDocCustomProps;


        oDocBuiltInProps = oDoc.BuiltInDocumentProperties;
        Type typeDocBuiltInProps = oDocBuiltInProps.GetType();

        //Get the Author property and display it.
        string strIndex = String.Empty;
        string strValue;

        oDocCustomProps = oDoc.CustomDocumentProperties;
        Type typeDocCustomProps = oDocCustomProps.GetType();

        foreach (DataRow row in dt_customAttributes.Rows)
        {
            strIndex = row[0].ToString();
            strValue = row[1].ToString();
            object[] oArgs = {strIndex,false,
                 MsoDocProperties.msoPropertyTypeString,
                 strValue};

            typeDocCustomProps.InvokeMember("Add", BindingFlags.Default |
                                       BindingFlags.InvokeMethod, null,
                                       oDocCustomProps, oArgs);
        }
    }

By now you can do this:

  dynamic properties = null;
  try
  {
    properties = Globals.ThisAddIn.Application.ActiveDocument.CustomDocumentProperties;

    // Testing purposes:
    properties.Add("SomeCustomProp", false, MsoDocProperties.msoPropertyTypeString, "SomeCustomValue");
    properties.Add("SomeOtherCustomProp", false, MsoDocProperties.msoPropertyTypeString, "SomeOtherValue");
    properties.Add("SomeNumericCustomProp", false, MsoDocProperties.msoPropertyTypeNumber, 1982331);
  }
  finally
  {
    Marshal.ReleaseComObject(properties);
  }

Releasing the COM object used to be recommended, but I'm not sure if it's still required.

Also, instead of

properties = Globals.ThisAddIn.Application.ActiveDocument.CustomDocumentProperties;

just use your own reference to the application, then .ActiveDocument.CustomDocumentProperties;.


Note: this does the same as the edit in the question - just using dynamic instead of manual reflection.

How to: Set a custom property in a word processing document , Set document properties in Excel; Set document properties in Word; Robust C# Copy. Microsoft.Office.Core.DocumentProperties properties; properties = (​Microsoft. How to: Create and modify custom document properties  If you add a custom document property to the DocumentProperties collection that's linked to a given value in an Office document, you must save the document to see the change to the DocumentProperty object. Example. This example, which is designed to run in Word, adds three custom document properties to the DocumentProperties collection.

I have had the same strange thing and it turns out that word does not save if you only change CustomDocumentProperties! You need to tell word that it must save by setting the doc.saved to false.

Hope this helps. Best Regards Frank

How to: Read from and write to document properties, Gets a collection that represents all the custom document properties for the document. Word. Assemblies: Microsoft.Office.Tools.Word.dll, Microsoft.Office.​Tools. that represents all the custom document properties for the document. C# Copy Use the BuiltInDocumentProperties property to return the collection of built-in  Create a new windows application project and add a button to it. On click of that button, we will open a document and add values for a built in and custom document properties. Than add reference to (Word 10.0 or 11.0 object library) and Microsoft.Office.Core.dll within COM tab of Add reference dialog box. After adding…

You can do something like this

Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application { Visible = false };
Microsoft.Office.Interop.Word.Document Doc = wordApp.Documents.Open("C:\\test.docx", ReadOnly: false, Visible: false);

Doc.BuiltInDocumentProperties["Title"].Value = this.Title.Text;
Doc.BuiltInDocumentProperties["Subject"].Value = this.Subject.Text;
Doc.BuiltInDocumentProperties["Category"].Value = this.Category.Text;
Doc.BuiltInDocumentProperties["Keywords"].Value = this.Keywords.Text;
Doc.BuiltInDocumentProperties["Author"].Value = this.Author.Text;
Doc.BuiltInDocumentProperties["Comments"].Value = this.Comments.Text;

Document.CustomDocumentProperties Property (Microsoft.Office , By now you can do this: dynamic properties = null; try { properties = Globals.​ThisAddIn.Application.ActiveDocument. Use the CustomDocumentProperties property of a document to work with custom properties. For example, in a document-level project for Microsoft Office Excel, use the CustomDocumentProperties property of the ThisWorkbook class. In a VSTO Add-in project for Excel, use the CustomDocumentProperties property of a Workbook object. These properties return a DocumentProperties object, which is a collection of DocumentProperty objects.

Add custom document properties to word with c#, I have had the same strange thing and it turns out that word does not save if you only change CustomDocumentProperties! You need to tell  Select the custom property you want to insert into your document from the list and click “OK”. The value is inserted where the cursor is located. If you change the value of the custom property, you can update the fields in your document to reflect the change by selecting all the fields, which can be easily done by selecting all the content in the document, and pressing F9.

Add custom document properties to word with c, What you may not know is how to insert values assigned to custom properties—​the properties you create—into your document. In order to do this,  In a VSTO Add-in project, use the BuiltInDocumentProperties property of a Document object. These properties return a DocumentProperties object, which is a collection of DocumentProperty objects. You can use the Item property of the collection to retrieve a particular property, either by name or by index within the collection.

Inserting Custom Properties with Fields (Microsoft Word), Creates a new custom document property of the PropertyType.String data Assembly: Aspose.Words (in Aspose.Words.dll) Version: 20.4. Syntax. C#. VB. C​++. Click “Add”. Your custom property with the “Value” and “Type” is added to the “Properties” list. Click “OK” to close the dialog box. You won’t see the added custom property on the “Info” backstage screen, but you can insert it into your document using a field. We will discuss how to do this in a future article.

Comments
  • My edit works for me... but i dont now why :D
  • Much love Frank!
  • I can edit the values of the builtin properties like author etc. but my problem is to add customDocumentProperties