Mar 26, 2010 at 4:34 PM
Edited Mar 26, 2010 at 5:02 PM
First, I wanted to say docX is AWESOME and thank you to Cathal for sharing it!!! You rock.
With regards to the merging problem, I too am having this issue when inserting documents into other documents. After a lot of testing I have only been able to reproduce this error ("An item with the same key has already been added.") with
some word files when using docX .insertDocument().
Note, that all documents I am about to discuss are docx not doc files. Please also note that this is not a coding problem because my code works, then I change the Word file being used and it fails. Thus, I am not posting code because the code is not the
issue (it is basically the same as the code above by the OP). It has more to do with the source Word document being used than my individual code.
Just for reference, my whole goal is to grab a document, replace some text with sql field data and then insert the doc into a master document and keep going (a merge).
It was weird because all new documents I make (to be used as source documents for the merge) will not produce this error when inserted into eachother using docx.dll. But if I took
some Word documents my coworkers had saved (all docx files, not doc files!) and try to use them for the merge I would often get this error, but not always. Very weird. Depending on which document (again, these are all Word 2007 docx files) the
code will either work or produce this error.
It has something to do with some property of the Word file itself conflicting withing docX.dll insertDocument (I suspect).
Here is the workaround I've found (hope it works for you until this can be fixed in the docX.dll):
If I take one of the merge source documents (test.docx, for example) that produces this error, and then ,within Word (manually), copy the whole document and paste it into a new document (test2.docx) and save this document and rerun my code using test2.docx
it works fine. Same words, paragraphs tables etc. The only differences can be the properties of the file itself.
My best guess is that some of the properties of the Word Doc (author, version etc) are conflicting when used with docX.dll.
If you get this error ("An item with the same key has already been added.") when inserting documents into each other, try (from within Word, manually yourself, not via docX.dll) copying the whole document and pasting it into a
new clean document. Save this new document and give it a try again in your code. I bet you don't get the error.
As to what is happening, I have no clue but copying and pasting to a clean document works around this error ;)