Programmatically remove parts from an Office file

Kevin Boske has another post on using the System.IO.Packaging assembly to work with the new Office Open XML formats. He shows how you can quickly open the package and navigate the relationships to find a specific part, then remove that part.

This is great if you want to clean up a file. Let's say you want to always remove comments from a document before it's posted on the web. Or maybe you want to remove the VBA from a macro-enabled document before users have access to it. These new open formats give you a lot more control over document entering and leaving your environment.

Kevin also stresses a point I've brought up before, and that's related to part names. It's important to remember that in Office we rely on the relationships to navigate the packages. We never reference a part directly (always via relationships), and as a result you can't rely on part names when you are inspecting a file. You need to use the content types part and the relationship files.