1/4/11 – Updated a couple of images and some of the code explanation
Adam Cogan asked me a question the other day that asked (among other things) "How do you know if a doc has multiple sections?"
In Word, of course, you can break a document up into sections by inserting a section break from the Breaks button in the Page Setup group on the Page Layout tab:
I created a new console application, added a reference to DocumentFormat.OpenXML and WindowsBase and used this code:
In it I take the filename passed as an argument and open it ReadOnly (line 22-23). I then find the number of sections using the typed enumerator (lines 26-27)
I also added the path to a file in the Command Line Arguments edit box in the Debug tab of the project Properties so there’s a file being passed in when I press F5:
Running the program gives the answer very quickly:
Sample file with 2 sections Download