This project has moved and is read-only. For the latest updates, please go here.

Word Template

Feb 10, 2014 at 12:27 PM

I'm trying to open a Word template (.dotx) with the DocX library, but it's giving me an InvalidOperationException with the text "Sequence contains no elements".

Can I open and edit Word templates with the DocX library or should I go back to the Interop libraries?
Feb 10, 2014 at 2:04 PM
Edited Feb 10, 2014 at 2:06 PM
I've had exactly this same problem today and I'v tracked it back to the postLoad function in the DocX.cs file.

When setting the main part it uses this string to find the main content part:
Unfortunately for dotx files it should be:
At this point I don't know if the library works with templates but including this as an option may alleviate it but also may cause something else to fail.

Perhaps someone close to the project may be able to comment on it aqs I'm just starting to learn it's capabilities...


Feb 10, 2014 at 2:24 PM
Well updating the code to include the additional content type only works so much.

I updated the Where exptression in the DocX.cs file at line 2149 to
 document.mainPart = package.GetParts().Where
                p => p.ContentType.Equals( "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml", StringComparison.CurrentCultureIgnoreCase) ||
                     p.ContentType.Equals("application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml", StringComparison.CurrentCultureIgnoreCase) 

And this loads and processes the file as you expected however there is a big drawback - you must still save the file as a template. i.e. a dotx. If you save it as a docX Word can not load it.

I'm not sure at this point how to update the library so it saves it with the correct content type....
Feb 10, 2014 at 3:46 PM
Thanks for the swift reply. This helps loads!
Feb 11, 2014 at 7:11 AM
Make sure to commit patches when you figure this one out :-) DocX awaits for your contribution!
Feb 11, 2014 at 12:01 PM
Hi Madboy,

Thanks for the invite but I must admit I have no idea how to commit changes as I've never contributed to an open source project before and at this point I'm not sure my changes are robust enough - At this point I'm considering them hacks but if you're close enough to the project and design I'd welcome any kind of "moderation" regarding the changes I'm suggesting!

I've now been able to load a template document from Word and save it as a standard document making a couple of minor changes. Again a warning - I don't yet know the implications of these changes and if they're likely to break any other functionality...

My method of loading a dotx template and creating the docx version of the file are a little unconventional but initial testing implies it works. This is what i do:
  1. Create a new DocX object for the target document
  2. Insert the ".dotx" file into the target document.
  3. Save file
DocX _template = DocX.Load(templateName);
DocX _document = DocX.Create(fileName);
This seems to create a docx file that can be loaded by MS Word
Feb 11, 2014 at 12:06 PM
Well that has to be verified if none of the functionalities are broken after using dotx. I guess I don't understand the need for DotX in comparision to simply having a docx file and treating it as a start document? Can you explain what would be diffrent if you use .docx at Load ?
Feb 11, 2014 at 12:10 PM
TBH - a docx starting file was the reserve option and we could have used it if needed - it's just a number of our starting documents are already created as ",dotx" so I started looking at this first...
Feb 11, 2014 at 12:22 PM
Edited Feb 11, 2014 at 12:23 PM
Also why wouldn't you skip DocX.Create process?
DocX _template = DocX.Load(templateName);
<some stuff>
Since you're using SaveAs you're not overwritting said file?

It would be nice to know if application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml is the only difference between DocX and DotX.
Feb 11, 2014 at 12:29 PM
If I use the code above and save the file with a .docx extension my Word installation (I'm using 2013) reports that the file is corrupt when I load it...

I found that inserting the template into a correctly created docx object allowed me to save a file that could be loaded by MS Word?

I'm not sure exactly why it works or if it does so 100%, as I don't have any real knowledge of the docx internal format. This is why I stated I regarded the code as a hack at this stage but as you say it would be good to know if this was the only difference in internal formats...
Feb 11, 2014 at 2:56 PM
This is a topic in which I'm very interested. Being able to read and write template files is on the critical path for one of my projects. I've been meaning to look more at this for some time. I'm happy to help with submitting patches or anything else you need
Feb 11, 2014 at 2:58 PM
Then we wait for your support stimms ;-) Thanks
Feb 14, 2014 at 4:56 AM
I had a few minutes tonight to look at this problem in more detail. It seems that the differences between docx and dotx are pretty minimal.
  1. docProps\App.xml - the dotx has a template of equal to its own name while the docx has template equal to Normal.dotm (at least in a default document, this is likely different for documents based on another template). There is also a HeadingPairs section the to dotx which does not appear in the docx. I'm not sure what these do so I'll attempt to hard code them in my naive implementation.
  2. docProps\core.xml - looks to just be revision number and created date differences. Not important
  3. word\settings.xml - very minor differences. A proof state was added and another rsid(whatever that is) was added. I wonder if maybe this was just an artifact of whether the spelling/grammar checker had run before I saved off my example documents. docx
  4. word\document.xml - The differences here look to be artifacts of the spelling/grammar checker. I don't think we need to take them into account.
  5. [Content_Types].xml - As previously mentioned there is a change in the content type in this file
So really it does look like we might be able to get away with simply changing the document type(#5) and the template name(#1) without other changes. My test documents were trivial so there may well be nuances which show up further into the process. Release early, release buggy - that's my motto.
Feb 14, 2014 at 5:26 AM
I had some success with changing just the document type. I still have a bit of work to do around loading templates but I have creating new dotx files working.
Feb 23, 2015 at 11:50 AM
Armaron wrote:

I'm trying to open a Word template (.dotx) with the DocX library, but it's giving me an InvalidOperationException with the text "Sequence contains no elements".

Can I open and edit Word templates with the DocX library or should I go back to the Interop libraries?
With the current version of DocX (tested with opening a dotx file is possible.
            using (DocX wordTemplate = DocX.Load(@"C:\Template.dotx"))

Be careful. You cannot open a template (dotx) and save it as document (docx)

Creating a docx from a template is done the following way:
            using (DocX wordDoc = DocX.Create(@"C:\test.docx", DocumentTypes.Document))