I thought it would be really cool if I could post code that would modify Word 2007 and Word 2010 documents to fix the numbering format problem described in my last post without needing to use Word 2013. I was stymied for three days by the error message:
“The specified package is invalid. The main part is missing.”
It came up part of the time, but not all the time, for Word documents of all types when I tried to open them like this:
WordprocessingDocument doc = WordprocessingDocument.Open(openXMLFileName, true);
The error message is mentioned often in questions in various forums, but I could almost never find a resolution for it and the questions that were answered were not for my scenario. One piece of information did help me, though: the actual cause comes from the underlying
System.IO.Packaging namespace. The message indicates that
System.IO.Packaging doesn’t find valid parts and relationships.
Sure enough, when I checked for the number of Parts in a document I got 0 (zero). This was extremely puzzling and I lost a few hours sleep. The next morning a moderator in the Open XML SDK forum confirmed that he had no problems accessing the files. So the issue was certainly due to my machine’s configuration. What next?
I finally decided to test files from a different Windows folder as I was beginning to suspect a permissions problem. Sure enough, documents in “My documents” worked without a hitch. Using the Windows Share wizard, I granted “share” permissions to “everybody” for the folder with my test files – et voilà, problem solved!