none
Word 2010 Documents collection corrupted if work with several documents RRS feed

  • Question

  • Hi

    I have an issue with document collections in Word 2010 using COleDispatchDriver.

    We uses Documents property off Application object and look for a document with specific file name.

    After a while it happens that after close of a document the collection is corrupted.

    Example:
    At start 3 documents open
    1. Doc1.doc
    2. Doc2.doc
    3. Doc3.doc

    Now the document 1 is closed and the result is:
    2 documents open
    1. Doc2.doc
    2. Doc2.doc

    At any time I try to get the collection of documents at later time. I always get the doc2 twice but the doc3 is not listed anymore even if still opened in Word.

    If such a situation appear I also tried to run a VB macro showing me all documents opened and get the same result First if I close the doc3 manually and reopen this again the right document collection is back.

    This does not happen in Word 2007. But very well reproducible with Word 2010.

    Kind regards
    Paul


    Paul

    Wednesday, July 25, 2012 2:26 PM

Answers

  • Finally we was able to implement a workaround suggested by Microsoft.

    "The workaround fetches the window document name from the Word windows collection.  Also the Word windows collection returns the correct count.  Since it is possible to have multiple views that hence show multiple windows that will return the same Window name, unique document names are stored in a CList."

    The issue was confirmed as a bug by Microsoft:

    "The bug is in Word 2010, so the workaround is not required in Word 2003 and Word 2007.  However, the workarounds will work even if used with Word 2003 or Word 2007 and I have tested it successfully.  The bug is set for triage during the next SP of Word 2010 (SP2). However, a timeline for it hasn’t been decided. I will let the product group know that we have another customer who is facing this issue so that they can consider it while reviewing the bug."


    Paul

    • Marked as answer by Paul73D Thursday, September 13, 2012 2:06 PM
    Thursday, September 13, 2012 2:06 PM

All replies

  • Could you possibly paste the code of the Macro, to make it extra easy for us to replicate the problem?

    I'm asking because it seems likely that it's specific to the way you're accessing it. If in all cases the documents collection was corrupted by the closing of a document, it probably would have been flagged and fixed already. Maybe it's to do with the type of loop you're using to iterate, or something similar.

    Wednesday, July 25, 2012 9:34 PM
  • Hi Joseph,

    The issue that Paul mentioned is easily reproducible using the following code:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim myDocument As Microsoft.Office.Interop.Word.Document
            List4.Items.Clear()

            For Each myDocument In WordGlobal_definst.Application.Documents
                List4.Items.Add(myDocument.Name)
            Next myDocument
        End Sub

    Wednesday, August 1, 2012 3:38 PM
  • I see. Yes, I am able to reproduce that error. Bad Microsoft programmers!

    The document count always seems reliable, however, so for any readers (and/or if the original poster is still hung up on it) this will work:

            Dim docCount As Integer
            Dim index As Integer
            docCount = oWord.Documents.Count
    
            For index = 1 To docCount
                ListBox1.Items.Add(oWord.Documents.Item(index).Name)
            Next index
    Of course, you ought to do validation to allow for the document count changing while in the loop.
    Wednesday, August 1, 2012 5:13 PM
  • Hi Joseph.

    We had nearly the same implementation in our automation integration using OleDispatcher in C++. There we cannot use the ForEach loop. We get the count and try to get the Item with the index. But also there we get sometime wrong item. I mean, we get teh document but this has the same name as the previous one.

    So the issue is not solved or answered.

    Kind regards,
    Paul.


    Paul

    Thursday, August 2, 2012 7:31 AM
  • I was able to reproduce the issue with a very simple macro.

    Create a new macro with following content:

    Sub DisplayOpenDocuments()
    
      Dim myDocument As Document
       
      For Each myDocument In Application.Documents
        MsgBox myDocument.FullName
      Next myDocument
    
    End Sub
    
    
    Now open 4 new documents and execute the macro.

    You will get 4 messages for Document4, Document3, Document2 and Document1 displayed.

    Now close the Document3 and create a new Document5 and run the macro again.You will get 5! messages for Document5, Document4, Document2, Document1 and Document1 displayed.

    I see only 4 documents. But the collections of Application.Documents give me 5 documents and the last 2 are doubled file names!


    Paul

    Friday, August 3, 2012 6:23 PM
  • Finally we was able to implement a workaround suggested by Microsoft.

    "The workaround fetches the window document name from the Word windows collection.  Also the Word windows collection returns the correct count.  Since it is possible to have multiple views that hence show multiple windows that will return the same Window name, unique document names are stored in a CList."

    The issue was confirmed as a bug by Microsoft:

    "The bug is in Word 2010, so the workaround is not required in Word 2003 and Word 2007.  However, the workarounds will work even if used with Word 2003 or Word 2007 and I have tested it successfully.  The bug is set for triage during the next SP of Word 2010 (SP2). However, a timeline for it hasn’t been decided. I will let the product group know that we have another customer who is facing this issue so that they can consider it while reviewing the bug."


    Paul

    • Marked as answer by Paul73D Thursday, September 13, 2012 2:06 PM
    Thursday, September 13, 2012 2:06 PM
  • Thanks for letting us know. Window names is a good idea.
    Thursday, September 13, 2012 3:21 PM
  • We had the same problem in production and we need a bugfix and can't wait until Office SP2!

    Unfortunatly 'Window names' is not a good idea, because you run into new kind of problems. For example: The user can create a new window for an already opened document using the Button on the 'View'-Ribbon.  So you have two windows for the same Document. If you print the opened documents using a 'For Each Windows'-Loop the document will be printed twice. That is definitifly not what we want. And this won't happen using the 'For each documents' - Loop (if the documents collection would work correctly).

    Tuesday, October 16, 2012 9:33 AM
  • I don't think users can rename windows. So, obviously, you can write code to check for duplicate window names.
    Tuesday, October 16, 2012 3:37 PM