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

Merge Tool - Problem

Jan 19, 2010 at 1:24 PM

Hi,

I´m trying to make a merge tool and i receive this exception: "An item with the same key has already been added."

Code:

DocX docx1 = DocX.Load("c:\\1.docx");
DocX docx2 = DocX.Load("c:\\2.docx"); //copy of the "1.docx"
DocX docx3 = DocX.Create("c:\\3.docx");
docx3.InsertDocument(docx1);
docx3.InsertDocument(docx2); //->exception
docx3.Save();

 

Any idea to resolve this?

Tks

Jan 26, 2010 at 6:30 PM

Hi Droque,

 

sorry it has taken me so long to reply, I have just finished two weeks of exams. I will need the two documents in order to debug this issue, if the documents are sensitive please create simpler documents which cause the same error. I am out of the country this week but I will take a look at this when I return on the first of February.

 

Kind regards and happy coding,

Cathal

Feb 5, 2010 at 10:35 AM

Hi Coffeycathal,

 

No problem, thanks for your help.  To simulate use this docx  -> http://hotfile.com/dl/27255190/d200c60/1.docx.html (use the same docx to "2.docx" file)

 

Best regards

Mar 26, 2010 at 5:34 PM
Edited Mar 26, 2010 at 6:02 PM

Hi,

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  ;)

G

Mar 27, 2010 at 7:34 PM
Hi gcondit,

thank you for taking the time to write to me about DocX. I am very
glad that you have found it useful. With respect to the "An item with
the same key has already been added." bug, I think the problem is as
follows.

When a docx document contains content other than text such as images,
hyperlinks, etc they are stored separately and linked into the
document using a key. The first key is id_01 and each successive key
has a higher number. I think the problem is document_a and document_b
both contain a piece of content with the same key id_n. When the two
documents are merged DocX.dll tries to add a piece of content from
document_b into document_a using a key that has already been allocated
by document_a, this causes the error message "An item with the same
key has already been added.". This bug is on top of my bug list and it
will be fixed in May when my exams are completed.

Thank you again for contacting me about this issue. I hope that you
will still continue to find DocX to be a useful tool.
Cathal
Mar 27, 2010 at 8:39 PM
Edited Mar 27, 2010 at 9:13 PM

Thank you again for this tool. It has saved me a bunch of time and made things much easier!

You are very nice to provide this tool and to update it, it is quite admirable.

If it helps, I was able to reproduce the error with just tables and text. Wanted to offer it to help in your fix.

In this example I add the same document twice, but the error also arises if the file is copied (BadOrig2.docx) loaded as a Novacode.DocX and inserted after BadOrig.docx. Not sure if this will help out or not, but I figure that it may make diagnosis easier :)

File:  (removed, see update)

 

Code used to reproduce ASP.net/VB:

        Dim SourceDocument As Novacode.DocX
        Dim OutputDocument As Novacode.DocX
        SourceDocument = Novacode.DocX.Load("c:\BadOrig.docx")
        OutputDocument = Novacode.DocX.Create("c:\OutputDoc.docx")
        OutputDocument.InsertDocument(SourceDocument)
        OutputDocument.InsertDocument(SourceDocument) ' <----------Error Line
        OutputDocument.Save()

Mar 27, 2010 at 9:01 PM

Update:

I just changed the file link because the document still had corporate info in the document properties that I forgot to remove. But oddly enough, when I removed this data from the Word doc (via properties inside Word) and saved it the error went away. I have saved a new link with the data changed but still there so you can reproduce.  If you open the document, go to properties and remove all Summary and Custom properity entries the file will work with DocX insert.

 

new file: http://hotfile.com/dl/34937580/5964850/BadOrig.docx.html

 

Mar 27, 2010 at 10:12 PM
Edited Mar 27, 2010 at 10:28 PM

Sorry for so many posts here, but I have narrowed it down to one thing (Custom Properties within Word) and it is very easy to reproduce on your end (no downloading at all!).

This error ("An item with the same key has already been added.") happens when you add a custom property within Word then use docX to InsertDocument more than once.

To reproduce open a new Word 2007 Document and do the following:

(add a custom property to the document, how to from Microsoft)

Click the Microsoft Office Button , point to Prepare, and then click Properties.
In the Document Information Panel, click the arrow next to Document Properties, and then click Advanced Properties.
In the Document Properties dialog box, click the Custom tab.
In the Name box, type a name for the custom property, or select a name from the list.
In the Type list, select the data type for the property that you want to add.
In the Value box, type a value for the property. The value that you type must match the selection in the Type list. For example, if you select Number in the Type list, you must type a number in the Value box. Values that don't match the property type are stored as text.
Click Add.

 

Save the blank document.

If you now try to insert this document via docX.InsertDocument and do it more than once (or with any other document that has custom properties) it will raise the error.

 

Unfortunately, doing a "SourceDocument.CustomProperties.Clear()" in docX will not help. But if you manually remove the custom properties in Word your document can be inserted multiple times via docX.

 

Thank you to Cathal for replying and I hope this helps in the fix.

 

Good Luck with your exams (I am sure you will do well!)

 

G

 

 

Dec 5, 2012 at 12:59 PM

Hello Sir,

Is this problem fixed in the latest release?

 

thank you,

Vittorio Sozzi