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
- Dim opnXMLElement As OpenXmlElement = newDocMainPart.Document
- 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
- Dim sdtContentCell As SdtContentCell = Nothing
- Dim sdtContentBlock As SdtContentBlock = Nothing
- Dim sdtContentRow As SdtContentRow = Nothing
- Dim sdtContentRun As SdtContentRun = Nothing
- Select Case True
- Case TypeOf sdtProperty.Parent Is SdtBlock
- sdtContentBlock = sdtProperty.Parent.GetFirstChild(Of SdtContentBlock)()
- Case TypeOf sdtProperty.Parent Is SdtCell
- sdtContentCell = sdtProperty.Parent.GetFirstChild(Of SdtContentCell)()
- Case TypeOf sdtProperty.Parent Is SdtRow
- sdtContentRow = sdtProperty.Parent.GetFirstChild(Of SdtContentRow)()
- Case TypeOf sdtProperty.Parent Is SdtRun
- sdtContentRun = sdtProperty.Parent.GetFirstChild(Of SdtContentRun)()
- Case Else
- 'Nothing
- 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