wordpress hit counter
Iterating through all Content Controls in an Open XML WordprocessingML Document - OpenXML Developer - Blog - OpenXML Developer
Goodbye and Hello

OpenXmlDeveloper.org is Shutting Down

There is a time for all good things to come to an end, and the time has come to shut down OpenXmlDeveloper.org.

Screen-casts and blog posts: Content on OpenXmlDeveloper.org will be moving to EricWhite.com.

Forums: We are moving the forums to EricWhite.com and StackOverflow.com. Please do not post in the forums on OpenXmlDeveloper.org. Instead, please post in the forums at EricWhite.com or at StackOverflow.com.

Please see this blog post for more information about my plans moving forward.  Cheers, Eric

Iterating through all Content Controls in an Open XML WordprocessingML Document

Iterating through all Content Controls in an Open XML WordprocessingML Document

Rate This
  • Comments 22

This page has been moved.  You can find this content at the following link: Iterating through all Content Controls in an Open XML WordprocessingML Document

OpenXmlDeveloper.org is shutting down as an active site.  See this blog post at EricWhite.com for more information.

Attachment: Test.docx
  • hello, I had to add an extra line to check whether the tag variable is null or not before the Console.WriteLine() otherwise I would be getting a null reference exception. thanks

  • If the contentcontrol sits inside a SdtCell, the above method might not be able to find it.

  • I added the SdtCell into this:

           public static IEnumerable<OpenXmlElement> ContentControls(

               this OpenXmlPart part)

           {

               return part.RootElement

                   .Descendants()

                   .Where(e => e is SdtBlock || e is SdtRun || e is SdtCell);

           }

  • Thanks a lot for your post. I tried to run this sample code, but i am getting the following error and not able to proceed.

    "'DocumentFormat.OpenXml.Packaging.OpenXmlPart.RootElement' is inaccessible due to its protection level". I just copied the code and try to run. I have added the reference to the corresponding DLL. Please guide me.

  • This is exactly what I need, however, I will need to go further and take values from a web form and edit the content control tags with the corresponding values. Thanks!

  • Hi Eric, you code works well for the attached docx file, but it doesn't Iterating through all Content Controls if one Content Control contains other Content Control.

    I mean lets say, i have Rich Text Content Control with table inside & inside this table i have lets say CheckBox  Content Control column for all the rows. These checkBox Content Control are not  Iterate through.

    It doesn't read the nested Content Controls.

    Please help in this.

    Thanks

    Gill

  • Hi I am able to find the solution as

    I added the SdtCell into this:

          public static IEnumerable<OpenXmlElement> ContentControls(this OpenXmlPart part)

          {

              return part.RootElement.Descendants().Where(e => e is SdtBlock || e is SdtRun || e is SdtCell);

          }

    Thanks

    Gill

  • In case Some One need the same functionality in VB.Net (as we don't have  yield return in VB.NET)

    <System.Runtime.CompilerServices.Extension()> _

       Public Function getDocumentContentControls(doc As WordprocessingDocument)

           Dim lstElements As New List(Of OpenXmlElement)

           For Each ContentControl As OpenXmlElement In GetPartContentControls(doc.MainDocumentPart)

               lstElements.Add(ContentControl)

           Next

           For Each header As OpenXmlPart In doc.MainDocumentPart.HeaderParts

               For Each ContentControl As OpenXmlElement In GetPartContentControls(header)

                   lstElements.Add(ContentControl)

               Next

           Next

           For Each footer As OpenXmlPart In doc.MainDocumentPart.FooterParts

               For Each ContentControl As OpenXmlElement In GetPartContentControls(footer)

                   lstElements.Add(ContentControl)

               Next

           Next

           If doc.MainDocumentPart.FootnotesPart IsNot Nothing Then

               Dim lobjFootNotes As OpenXmlPart = doc.MainDocumentPart.FootnotesPart

               For Each ContentControl As OpenXmlElement In GetPartContentControls(lobjFootNotes)

                   lstElements.Add(ContentControl)

               Next

           End If

           If doc.MainDocumentPart.EndnotesPart IsNot Nothing Then

               Dim lobjEndNotes As OpenXmlPart = doc.MainDocumentPart.EndnotesPart

               For Each ContentControl As OpenXmlElement In GetPartContentControls(lobjEndNotes)

                   lstElements.Add(ContentControl)

               Next

           End If

           Return lstElements

       End Function

    Thanks

    Gill

  • gsgill76 can you post the whole vb code for me please? You are referencing a method called GetPartContentControls and not too sure what that is doing in your code.

  • Pingback from  WordprocessingDocument handling | SharePoint Q&A

  • Pingback from  How to create an XML document that contains tags for all content control names in a Microsoft Word document, with Open XML and C#

  • Pingback from  How to dynamically bind data to Content Controls in a Microsoft Word document by using OpenXml and C#.

  • You should just use SdtElement instead of picking Cell/Run/Block/Row specifically - it will catch all instances of Sdt* items you care about. Reference: msdn.microsoft.com/.../documentformat.openxml.wordprocessing.sdtelement(v=office.14).aspx

    public static IEnumerable<OpenXmlElement> ContentControls(this OpenXmlPart part) {

              return part.RootElement.Descendants().Where(e => e is SdtElement);

    }

  • having trouble putting this into a winform app; runs ok in console app and finds every contentcontrol.  problem is, how to then WRITE into that content control??

    ?

    writing into the content control is something that is not easy to do, its not documented on the internet, there are examples but they use the word interop;  how are content control values written into directly from c# ???  it is EXTREMELY hard to do

  • Hi is there a way to identify the run attached with the tag in the Content Control and replace the text within the run, with another text?

    In other words I want to replace the text of each Content control with another text, is that something easy to do? Can’t find anything

Page 1 of 2 (22 items) 12