Setting word document variables via OpenXML

Word2007Document variables in Microsoft Word can be valuable for storing any kind of additional information within a document. Document variables are (in contrast to document properties) not visible to the end user. But, serendipitously one can get their values via source code (e.g. from a Word AddIn). These characteristic properties might be very useful in multiple situations.

Setting and getting document variable values via Office automation is a simple and straightforward task. See

Unfortunately Office automation has several drawbacks. For example it isn’t advisable to use Office automation in a pure server process scenario(see Hence, I want to reveal a different approach.

With the present article I would like to provide you C# source code for adding document variables to an existing word document via OpenXML. In order to use the code you’ll need to install the OpenXML SDK 2.5 and add references to “WindowsBase” and “DocumentFormat.OpenXML” (see

    /// <summary>
    /// add a document variable
    /// </summary>
    /// <param name="document">word document where document variable should be added</param>
    /// <param name="name">variable name</param>
    /// <param name="value">variable value</param>
    /// <returns>'true' on success and 'false' otherwise</returns>
    public static bool AddDocumentVariable(WordprocessingDocument document, string name, string value)
        DocumentVariables documentVariables;
        documentVariables = document.MainDocumentPart.DocumentSettingsPart.Settings.Descendants<DocumentVariables>().FirstOrDefault();

        if (documentVariables == null)
          documentVariables = new DocumentVariables();
          Compatibility compatibility = document.MainDocumentPart.DocumentSettingsPart.Settings.Descendants<Compatibility>().FirstOrDefault();

        DocumentVariable documentVariable = new DocumentVariable() { Name = name, Val = value };

        return true;
        return false;

Feel free to use it or adapt it to your necessities. In case you have any kind of optimization, please let me know! Any comment will also be appreciated!

2 thoughts on “Setting word document variables via OpenXML

  1. Thanks, I tried something similar in my application trying to retreive existing DocumentVariables but it doesn’t work. Using MSWord (2013) I can see these variables but unfortunately   the instruction Document.MainDocumentPart.DocumentSettingsPart.Settings.Descendants().FirstOrDefault(); always return “null” …
    any idea ?

    Thank you

  2. Instruction returning “null” means your document doesn’t contrain any document variable. After using the method “AddDocumentVariable” you have to save your modified document. You can also use the OpenXML Productivity Tool to see what exactly happened!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s