none
How to assign CustomProperties to Word from "outside" via app or powershell ? RRS feed

  • Question

  • Hi,

    I would like to add/assign custom properties and their values to Wordtemplates. I would like to accomplish that without having the pain of opening word each time and do it manually.

    Is there a way to script that ?

    Thanks

    Sven 

    Monday, July 11, 2011 10:28 AM

Answers

  • Hi Sven,

     

    I think the sample code is exactly what you want:

    Manipulate Docx with C# without Microsoft Word installed with OpenXML SDK

    In this particular situation, I'm editing the custom properties of a docx file, which are commonly used to store some application's info for further use, or even for some add-in that we developed as well.

     

    Here is the code snippet:

     

    //  Work with the custom properties part.

            PackagePart customPropsPart = null;

     

            //  Get the custom part (custom.xml). It may not exist.

            foreach (PackageRelationship relationship in

              wdPackage.GetRelationshipsByType(

              customPropertiesRelationshipType))

            {

                Uri documentUri = PackUriHelper.ResolvePartUri(

                    new Uri("/", UriKind.Relative), relationship.TargetUri);

                customPropsPart = wdPackage.GetPart(documentUri);

                //  There is only one custom properties part,

                // if it exists at all.

                break;

            }

     

     

    If you have further concern about this, please feel free to let me know.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Sven W Wednesday, July 20, 2011 9:13 PM
    Wednesday, July 20, 2011 6:15 AM
    Moderator
  • Hello Sven,

    here is a PowerShell example:

    How Can I Add Custom Properties to a Microsoft Word Document?


    Uros Calakovic
    • Marked as answer by Sven W Wednesday, July 20, 2011 9:12 PM
    Wednesday, July 20, 2011 3:13 PM

All replies

  • Hi Sven

    Which version of Word and/or which Word file formats are we talking about?


    Cindy Meister, VSTO/Word MVP
    Monday, July 11, 2011 11:08 AM
    Moderator
  • Well, actually I am looking for a solution for Word 2007 using docx fileformat.
    Monday, July 11, 2011 11:12 AM
  • Hi Sven

    That's good :-) At least, it's good if you use a programming language that can "unpack" the docx ZIP file then look in the appropriate xml file from the package that carries the CustomDocumentProperties. I simply don't know PowerShell, so I have no idea if it can do that.

    I do know that C# and VB.NET can do that. You'll find more information on how to use them to do this kind of thing at OpenXMLDeveloper.org and in the Open XML SDK forum here in the MSDN forums.


    Cindy Meister, VSTO/Word MVP
    Monday, July 11, 2011 12:04 PM
    Moderator
  • Hi Sven,

     

    Thanks for your post

     

    Cindy has already guided you in a right redirection: using OpenXML. Here are two examples for your reference:

    Setting Custom Properties in Word 2010 Documents by Using the Open XML SDK 2.0

    Manipulate Docx with C# without Microsoft Word installed with OpenXML SDK

     

    Additionally, you can download the OpenXML SDK 2.0 from:

    Open XML SDK 2.0 for Microsoft Office

     

    If you have any concern about this, please feel free to let me know.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, July 12, 2011 6:59 AM
    Moderator
  • Hi Sven,

    Any update on this question?


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, July 19, 2011 9:32 AM
    Moderator
  • No, actually no update. I hoped that there was already a solution ready for this..
    Tuesday, July 19, 2011 10:30 AM
  • Hi Sven,

     

    I think the sample code is exactly what you want:

    Manipulate Docx with C# without Microsoft Word installed with OpenXML SDK

    In this particular situation, I'm editing the custom properties of a docx file, which are commonly used to store some application's info for further use, or even for some add-in that we developed as well.

     

    Here is the code snippet:

     

    //  Work with the custom properties part.

            PackagePart customPropsPart = null;

     

            //  Get the custom part (custom.xml). It may not exist.

            foreach (PackageRelationship relationship in

              wdPackage.GetRelationshipsByType(

              customPropertiesRelationshipType))

            {

                Uri documentUri = PackUriHelper.ResolvePartUri(

                    new Uri("/", UriKind.Relative), relationship.TargetUri);

                customPropsPart = wdPackage.GetPart(documentUri);

                //  There is only one custom properties part,

                // if it exists at all.

                break;

            }

     

     

    If you have further concern about this, please feel free to let me know.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Sven W Wednesday, July 20, 2011 9:13 PM
    Wednesday, July 20, 2011 6:15 AM
    Moderator
  • Hello Sven,

    here is a PowerShell example:

    How Can I Add Custom Properties to a Microsoft Word Document?


    Uros Calakovic
    • Marked as answer by Sven W Wednesday, July 20, 2011 9:12 PM
    Wednesday, July 20, 2011 3:13 PM