Tim's .NET Software Architecture Blog

Adventures in Architecting and Developing .NET Applications


News


CAG

Technorati Profile

www.flickr.com

My Stats

  • Posts - 178
  • Comments - 99
  • Trackbacks - 42

Tag Cloud


Recent Comments


Recent Posts


Archives


Post Categories


Image Galleries


Blogs


Link Blogs


Links


Podcasts



If you are using a template document and replacing text programmatically using the Office Open XML SDK 2 API you will need a way to identify the target to be replaced.  One option is to use a Content Control and setting the tag value the same for all of the controls that need to be substituted with a single value.  After some trial and error and a lot of digging through the DocumentReflector I came up with the following LINQ query to get a list of all blocks with the same tag name.

 

var blocks = from s in part.MainDocumentPart.Document.Descendants<SdtBlock>()
             where (
                     s.GetFirstChild<SdtProperties>().GetFirstChild<Tag>() != null
                     && s.GetFirstChild<SdtProperties>().GetFirstChild<Tag>().Val.Value.Equals(placeholder)
                     )
             select s;

 

The nice thing is that all Word document content controls have the tag property as an option so this works whether you are searching for text, rich text or quick parts.  If you are searching for images and some other content controls you may have to search for SdtRuns instead of SdtBlocks, but the query is essentially the same.

Technorati Tags: ,,,

posted @ Thursday, November 05, 2009 6:45 AM | Filed Under [ .NET Development Office Open XML ]

Comments

No comments posted yet.
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: