Wednesday, October 21, 2009

OpenXml – Delete Header and Footer Parts

Dependencies:

DocumentFormat.OpenXml.dll (version 2.0.3930.0)

Imports:

DocumentFormat.OpenXml.WordProcessing

DocumentFormat.OpenXml.Packaging

Code Snippet
  1. Private Sub DeleteHeaderAndFooterParts(ByRef myDoc As WordprocessingDocument)
  2. Dim headRefs As List(Of HeaderReference) = myDoc.MainDocumentPart.Document.Descendants(Of HeaderReference)().ToList()
  3. For Each headRef As HeaderReference In headRefs
  4. Dim relId As String = headRef.GetAttribute("id", wns_r).Value
  5. myDoc.MainDocumentPart.DeletePart(relId)
  6. headRef.Remove()
  7. Next
  8. Dim footRefs As List(Of FooterReference) = myDoc.MainDocumentPart.Document.Descendants(Of FooterReference)().ToList()
  9. For Each footRef As FooterReference In footRefs
  10. Dim relId As String = footRef.GetAttribute("id", wns_r).Value
  11. myDoc.MainDocumentPart.DeletePart(relId)
  12. footRef.Remove()
  13. Next
  14. myDoc.MainDocumentPart.Document.Save(myDoc.MainDocumentPart.GetStream(FileMode.Create))
  15. End Sub

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...