none
Adding and/or Changing document variables in Word document using Open XML RRS feed

  • Question

  • Hi

    I've looked all around for a solution to write and/or update document variables in a Word document using Open XML.

    I found out that these parts is contained in the Settings Class of the doxument XML, but I couln't found any samples or references of how to manage them

    How do I do this?

    Thanks in advance


    Best Regards Peter Karlström Midrange AB, Sweden
    Wednesday, November 23, 2011 9:40 AM

Answers

  • Hi Peter,

    Thank you for posting.

    If the document contains the varibles, you can refer to the following code to update them:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing;
     
    namespace OpenxmlWordDocumentVariables
    {
        class Program
        {
            static void Main(string[] args)
            {
                string fileName = @"D:\MyWorkingDocuments\OpenxmlCases\OpenxmlWordDocumentVariables\OpenxmlWordDocumentVariables\bin\Debug\documentvariables.docx";
                using (WordprocessingDocument doc = WordprocessingDocument.Open(fileName, true))
                {
     
                    List<DocumentVariables> docVariablesList = doc.MainDocumentPart.DocumentSettingsPart.Settings.Descendants<DocumentVariables>().ToList();
                    foreach (DocumentVariables docVars in docVariablesList)
                    {
                        foreach (DocumentVariable docVar in docVars)
                        {
                            docVar.Name = "Id";
                            docVar.Val.Value = "123";
                            Console.WriteLine(docVar.Name + " => " + docVar.Val.Value);
                        }
                    }
                } 
     
            }
        }
    }
    

    As for adding the document variables, you can create the DocumentVariable element and append it to the DocumentVariables element, below is the fragment code:
                DocumentVariable documentVariable = new DocumentVariable(){ Name = "Id", Val = "123" };

                documentVariables.Append(documentVariable);

    Hope this can help you and just feel free to follow up after you have tried.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us

    • Edited by Bruce Song Thursday, November 24, 2011 5:47 AM
    • Marked as answer by Peter Karlström Thursday, November 24, 2011 11:01 AM
    Thursday, November 24, 2011 5:46 AM

All replies

  • Hi Peter,

    Thank you for posting.

    If the document contains the varibles, you can refer to the following code to update them:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing;
     
    namespace OpenxmlWordDocumentVariables
    {
        class Program
        {
            static void Main(string[] args)
            {
                string fileName = @"D:\MyWorkingDocuments\OpenxmlCases\OpenxmlWordDocumentVariables\OpenxmlWordDocumentVariables\bin\Debug\documentvariables.docx";
                using (WordprocessingDocument doc = WordprocessingDocument.Open(fileName, true))
                {
     
                    List<DocumentVariables> docVariablesList = doc.MainDocumentPart.DocumentSettingsPart.Settings.Descendants<DocumentVariables>().ToList();
                    foreach (DocumentVariables docVars in docVariablesList)
                    {
                        foreach (DocumentVariable docVar in docVars)
                        {
                            docVar.Name = "Id";
                            docVar.Val.Value = "123";
                            Console.WriteLine(docVar.Name + " => " + docVar.Val.Value);
                        }
                    }
                } 
     
            }
        }
    }
    

    As for adding the document variables, you can create the DocumentVariable element and append it to the DocumentVariables element, below is the fragment code:
                DocumentVariable documentVariable = new DocumentVariable(){ Name = "Id", Val = "123" };

                documentVariables.Append(documentVariable);

    Hope this can help you and just feel free to follow up after you have tried.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us

    • Edited by Bruce Song Thursday, November 24, 2011 5:47 AM
    • Marked as answer by Peter Karlström Thursday, November 24, 2011 11:01 AM
    Thursday, November 24, 2011 5:46 AM
  • Hi Bruce

     

    Thanks a million! Works great.

    When I see it now, I should have found it myself, but I'm not yet as keen on Open XML (and XML as a whole) as I could wish for....

     


    Best Regards Peter Karlström Midrange AB, Sweden
    Thursday, November 24, 2011 11:01 AM