none
how do i insert data into a content control in a word document using open xml RRS feed

  • Question

  • Hi
    I am trying to build a vb.net application using open xml.
    i have a dotx file with content controls.
    i open the template using wordprocessingdocument.open(...)
    i see in the document.xml file of the alias/ tag of the content control and the sdt:content tag.
    i want to retrive data from a database and place the data in the place of the content control.
    HOW DO I FIND THE CONTROL AT RUN TIME AND THEN AFTER THE CONTROL IS FOUND CHANGE THE VALUE/CONTENT IN THE CONTROL?
    ( i tried using body.descendants... but no luck). i am new to the open xml coding.
    i would appreciate the code example.
    thank you
    karen

    Wednesday, November 25, 2009 6:27 AM

All replies

  • Hi, karenbarlev

    Thanks for your question.

    I think Linq could be helpful to find these controls. And after find these elements, change their value or attribute as needed.

    The code snippet is as following. I hope it would work for you.

    Thanks,
    Raymond

    using System;
    using System.Linq;
    using DocumentFormat.OpenXml.Packaging;
    
    namespace SampleCode
    {
        class Sample
        {
            public static void Main(String[] args)
            {
                using (WordprocessingDocument package = WordprocessingDocument.Open("source.docx", true))
                {
                    string tagName = "content";
                    var elements = package.MainDocumentPart.RootElement.Descendants().Where(ele => ele.LocalName != null && ele.LocalName.Equals(tagName));
                    foreach (var ele in elements)
                    {
                        //update the value or content here                  
    
                    }
                }
    
            }
        }
    }
    
    Wednesday, November 25, 2009 8:25 AM
  • Raymond Hi

    thank you so much for your reply...and so fast
    is there any way to code ot in vb.
    i tried but am stuck in the "where(ele => ele.localname.....)

    how is ele defined?
    how do i write the line in vb?
    i defined elements as DIM ELEMENTS AS  SDKELEMENT
    "var elements = package.MainDocumentPart.RootElement.Descendants().Where(ele => ele.LocalName != null && ele.LocalName.Equals(tagName)); "

    THANK YOU AGAIN
    Karen

    Wednesday, November 25, 2009 9:26 AM
  • HI, Karen

    ele is an OpenXmlElement.

    The way to find all elements is using lamda expression. you may learn more from this site:http://msdn.microsoft.com/en-us/magazine/cc163362.aspx

    However, I have tried to convert the C# code to VB code as following. Since I am not familiar with VB.NET, the snippet are just for referrence. Hope it would be of help.

    Thanks,
    Raymond

    Imports System.IO
    Imports System.Linq
    Imports DocumentFormat.OpenXml
    Imports DocumentFormat.OpenXml.Packaging
    Imports DocumentFormat.OpenXml.Wordprocessing
    
    Public Class clsExcel
    
        Shared Sub Main()
            Dim package As WordprocessingDocument = WordprocessingDocument.Open("source.docx", True)
            Dim elements = package.MainDocumentPart.RootElement.Descendants().Where(Function(ele) Not String.IsNullOrEmpty(ele.LocalName) And String.Equals(ele.LocalName, "content"))
            For Each ele In elements
    
            Next
    
    
        End Sub
    
        
    End Class
    Wednesday, November 25, 2009 10:11 AM
  • Raymond, Hi


    Thank you very much for your help
    The code you provided helped me very much
    As i mentioned before i am new to this progamming - open xml
    Is there any way you could write how i change the value in a content control
    I tried to change the innertext but it is readonly.
    Do i have to remove it and add a new one?
    Is there a way to replace the value after i found the <w:t> I want?

    Thank you for your help
    Karen

    Wednesday, November 25, 2009 1:27 PM
  • You can use the ".Text" property to set the text data.

                textElement.Text = "foo";

    BTW, you can try the DocumentReflector and ClassExplorer tool to see what codes to use for a specific element.

    Monday, November 30, 2009 10:09 AM