Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices to debug broken OpenXML word document in C# generation

I'm generating a MS Word document with OpenXml package in C#. Many times I've seen this erorr message:

The file .docx can not be opened because there are problems with the contents

This error message is pretty useless: all it says the problem is in footer. Most of the times it says the error is on line 1, char 1.

I usually find the problem, but it takes too much time by going back and forth.

Are there any tools/techniques that can help with debugging such errors? Show a better error, like you are trying to add a Text outwith of a Run, or something like that?

like image 829
trailmax Avatar asked Apr 01 '14 15:04

trailmax


1 Answers

After poking a bit more online, I found tools that eased the pain. Even made a blog post about it: http://tech.trailmax.info/2014/04/validating-of-openxml-generated-documents-or-the-file-cannot-be-opened-because-there-are-problems-with-contents/

The basics are here:

  1. There is OpenXmlValidator class. Use it like this: http://msdn.microsoft.com/en-us/library/office/bb497334%28v=office.15%29.aspx
  2. There is Open Xml Package Explorer for VSCode - use it for manual tweaking of your stuff and slightly better error messages.

If you know other(better) ways, please let me know!

like image 147
trailmax Avatar answered Oct 18 '22 23:10

trailmax