Friday, June 18, 2010

Not all Structured Document Tags are created equal! (Continued)

Every Sdt must contain SdtProperties.  So the solution was to get the OpenXmlElement of DocumentMainPart.Document then get the Descendants that are SdtProperties. For example:

Code Snippet
  1. Dim opnXMLElement As OpenXmlElement = newDocMainPart.Document
  2.             For Each sdtProperty As SdtProperties In opnXMLElement.Descendants(Of SdtProperties)()

Then the parent of sdtProperty (as shown above) MUST be one of the types we are looking for so I run this check:

Code Snippet
  1. Dim sdtContentCell As SdtContentCell = Nothing
  2.                 Dim sdtContentBlock As SdtContentBlock = Nothing
  3.                 Dim sdtContentRow As SdtContentRow = Nothing
  4.                 Dim sdtContentRun As SdtContentRun = Nothing
  5.                 Select Case True
  6.                     Case TypeOf sdtProperty.Parent Is SdtBlock
  7.                         sdtContentBlock = sdtProperty.Parent.GetFirstChild(Of SdtContentBlock)()
  8.                     Case TypeOf sdtProperty.Parent Is SdtCell
  9.                         sdtContentCell = sdtProperty.Parent.GetFirstChild(Of SdtContentCell)()
  10.                     Case TypeOf sdtProperty.Parent Is SdtRow
  11.                         sdtContentRow = sdtProperty.Parent.GetFirstChild(Of SdtContentRow)()
  12.                     Case TypeOf sdtProperty.Parent Is SdtRun
  13.                         sdtContentRun = sdtProperty.Parent.GetFirstChild(Of SdtContentRun)()
  14.                     Case Else
  15.                         'Nothing
  16.                 End Select

Now I have the actual Sdt Content I am looking for and I can do whatever replacement I need to do.

No comments:

Post a Comment

Calling .NET From COM Originally posted by Mikec276 on C Sharp Friends It might be hard to convince your IT manager to let you build y...