InsertDocument throws exception

May 2, 2012 at 9:19 AM

With the latest version (built from source) I get an exception from  Enum.Parse in the Table constructor.

        internal Table(DocX document, XElement xml)
            : base(document, xml)
            autofit = AutoFit.ColumnWidth;
            this.Xml = xml;

            XElement properties = xml.Element(XName.Get("tblPr", DocX.w.NamespaceName));

            XElement style = properties.Element(XName.Get("tblStyle", DocX.w.NamespaceName));
            if (style != null)
                XAttribute val = style.Attribute(XName.Get("val", DocX.w.NamespaceName));

                if (val != null)
                        design = (TableDesign)Enum.Parse(typeof(TableDesign), val.Value.Replace("-", string.Empty));

                    catch (Exception)
                        design = TableDesign.Custom;
                    design = TableDesign.None;

                design = TableDesign.None;

 If I ignore the exception I get a working file but the table styles are all messed up.

May 4, 2012 at 11:01 AM

Can you provide sample documents to reproduce?

May 4, 2012 at 8:16 PM
Edited May 4, 2012 at 8:20 PM

Sry should have followed up with a repro case, will try to get the time for it on monday.

This is basically what I do: (pseudo code)

DocX base = Load base document

for each item in items

   DocX i = Load item document template

   i.SearchAndReplace template strings from item data


end for



This works fine when the number of items is small, once I get to around 300+ items, things go bad.

In reality there are a few (less than ten) template documents used (selected depending on item data).

The template document contains a single table with text and I suspect that it's the table that is causing problems.

I further suspect that the problem is that I insert "the same" document several times, maybe there are some GUID issues?


I wish I had more time to investigate this, but this is for a very low priority help feature that my bosses can't see the value of (until it's completed of course :)

May 4, 2012 at 8:24 PM

Would you be willing to send the templates to Coffey so he can diagnose? He rewrote the InsertDocument so maybe something slipped in.


Btw is it much faster now with new sources? He spent a lot of time on optimizing things.

May 5, 2012 at 6:47 AM

I'll put together a repro case on monday.

Please note that I had similar problems with all versions I've tried (the latest DocX binary, the latest source, the source before the optimization).

The binary currently works best since I can merge up to around 200 documents in this fashion without any problems (saved document works flawlessly).
With more than 200 documents I get an exception that halts execution (i.e can't continue) so no document is produced.

The source before and after the optimization fails in a similar way, but the exception is a first chance and I can continue execution and produce a document,
Word complains about this document but it can still be loaded, but table styles are wrong (at the end of the document).
Before the optimization it took over an hour on my I7 to produce the document, now it's less than a minute so that's great!

Again my gut feeling is that there's a problem when merging table styles (maybe only when the same style is merged).
I've not dug deep enough into the xml to understand how table styles are handled, but I've seen some GUID's in there and since in my case the source document contains the same GUID's I think this might cause problems?

I could try to randomize the GUID's before every insert to see if it helps, but someone with a deeper understanding of the format would probably hunt this bug down a lot quicker.






May 10, 2012 at 10:16 AM

Dosh any repro case?

May 10, 2012 at 10:41 AM

Also could you check if you can merge 200 documents and then pick this 1 document and keep on merging it? Maybe this will work as a workaround until Coffey has time to fix :)