none
Word 2003/2007 add-ins not working on Word 2010 due to corrupted document collection object RRS feed

  • Question

  • Dear Madam/Sir,

    Please be informed that due to a bug in Word 2010 I cannot use my Word 2003/2007 add-ins. A workaround using the Windows object might work but I will have to go through all my code, change and test it. If a fix will be released soon a workaround is also a waste of time.

    Please advise if this is a known problem and when a fix can be expected.

    The bug can be reproduced using the code below.

    Kind regards,

    Paul Kevenaar

    Function test01()
    '02-10-2012 1 paul kevenaar
    'reproduceer Word 2010 bug, corrupted document collection object
    'workaround door gebruik van windows object
    Dim oDoc, oDoc1, oDoc2, oDoc3, oDoc4 As Document
    Dim oWin As Window
    Dim n As Integer

    Debug.Print Format(Now(), "dd-MM-yyyy"), "MS Office " & Application.Version
       
    Debug.Print "testdoc", ThisDocument.Name

    Set oDoc1 = Documents.Add: Debug.Print "add", oDoc1.Name
    Set oDoc2 = Documents.Add: Debug.Print "add", oDoc2.Name
    Set oDoc3 = Documents.Add: Debug.Print "add", oDoc3.Name
    Set oDoc4 = Documents.Add: Debug.Print "add", oDoc4.Name

    For n = 1 To Documents.Count
        Debug.Print "count", n, Documents(n).Name
    Next n

    Debug.Print "close", oDoc4.Name: oDoc4.Close: Set oDoc4 = Nothing
    Debug.Print "close", oDoc3.Name: oDoc3.Close: Set oDoc3 = Nothing

    For n = 1 To Documents.Count
        Debug.Print "count", n, Documents(n).Name
    Next n
    n = 0
    For Each oDoc In Documents
        n = n + 1
        Debug.Print "doc", n, oDoc.Name
    Next oDoc
    n = 0
    For Each oWin In Windows
        n = n + 1
        Debug.Print "win", n, oWin.Document.Name
    Next oWin

    Debug.Print "close", oDoc2.Name: oDoc2.Close: Set oDoc2 = Nothing
    Debug.Print "close", oDoc1.Name: oDoc1.Close: Set oDoc1 = Nothing

    End Function


    Output:

    02-10-2012    MS Office 14.0
    testdoc       test_documentobject_01.doc
    add           Document2
    add           Document3
    add           Document4
    add           Document5
    count          1            Document5
    count          2            Document4
    count          3            Document3
    count          4            Document2
    count          5            test_documentobject_01.doc
    close         Document5
    close         Document4
    count          1            Document3
    count          2            Document2
    count          3            Document3
    doc            1            Document3
    doc            2            Document2
    doc            3            Document3
    doc            4            Document2
    doc            5            test_documentobject_01.doc
    win            1            Document2
    win            2            Document3
    win            3            test_documentobject_01.doc
    close         Document3
    close         Document2

     

    • Moved by Max Meng Wednesday, October 24, 2012 3:54 AM Moving to a more appropriate forum (From:Word IT Pro Discussions)
    Tuesday, October 23, 2012 6:03 PM

All replies

  • Hi bas258,

    Thanks for posting in the MSDN Forum.

    Would you please tell me whether your code is a VBA project. I never see such formatted code after VB6.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, October 24, 2012 7:26 AM
    Moderator
  • Please clarify what the expected output would be and specify what you think is a "bug".

    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, October 24, 2012 8:17 AM
    Moderator
  • Thanks for moving this topic to a more appropriate forum.
    Thursday, October 25, 2012 7:33 AM
  • Hi Tom

    Yes it is VBA in Word (not VB6). To get the output you need to open a new Word document, launch the Visual Basic editor, copy/paste the Function test01 in a new module and run it.
    The output is then shown in the immediate window.
    For VB6 (or .NET) you would have to change the code somewhat. I havn't tested it but one might expect similar behavior.
    Thursday, October 25, 2012 7:35 AM
  • Hi Cindy,

    Please allow me to go through the code to try to answer your question about the expected output.

    The code opens 4 new documents.
    In the first loop the Document.Count property is 5 as expected
    Also all 5 document names are printed as expected in the immediate window.

    Then 2 documents are closed.

    In the second loop the Document.Count property is now 3 as expected.
    To print the document names the method Documents.Item(#).Name is used  (or in short Documents(#).Name  were # = a Long indicating the ordinal position of the individual object)
    One would expect that the 3 names of the open documents would be printed.
    The second loop though prints the same document name for item 1 and 3 (the name for the' testdoc' is left out).
    I think this is a bug.

    In the third loop prints the names of all document objects in the document collection.
    One would expect the names of the 3 open document would be printed.
    But instead 5 names are printed including the 2 closed documents. These names still exist in the document collection.
    I think this is a bug.

    In the last loop the windows collection is used to print the document names.
    Here for the expected 3 windows objects the expected document names are printed.
    Because each document can have multiple windows I am not sure this will work properly as workaround.

    Hope this will help but please don't hesitate to ask for clarification.

    Paul
    Thursday, October 25, 2012 7:36 AM
  • Hi Cindy,

    Any update?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Friday, October 26, 2012 6:12 AM
    Moderator
  • Hi Tom,

    Did you manage to reproduce the issue?

    Best regards,

    Paul

    Wednesday, October 31, 2012 7:04 AM
  • Hi Paul,

    Sorry, I hasn't reproduced it due to I have no environment like your description. OK, I will try to consult other senior engineers for it. I will let you know what I found as possible as I can.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us


    Thursday, November 1, 2012 6:43 AM
    Moderator
  • Hi Tom

    I am sorry, I assumed you also have Office 2010 installed, that is the only thing needed.

    Thanks for consulting other senior engineers.

    Best regards,

    Paul

    Thursday, November 1, 2012 10:44 AM
  • Hi Paul

    Sorry about the delay in answering, I had to give a seminar last weekend and things have been busy...

    I reproduce the issue at my end. I can even go you one better: in the Documents(n) loop, even though .Count returns three I can query ?Documents(4).Name in the Immediate Window (when I use a breakpoint before the For loop to stop the code) and I don't get an error. I then get both document names twice (rather than one twice and one once).

    So, yes, I'd classify this as a bug - and a nasty one. No idea where it's coming from. I tested a couple of changes in your code, just to see if any of the shortcuts you take have an effect, but the results remain the same.

    I also tested whether the behavior is different if I close the document window manually, rather than letting the code close it - no difference.

    Given that Office 2013 is already on the market, I wouldn't expect the behavior in 2010 to change any time soon (if at all). So you should probably look at a work-around. You could consider maintaining your own Documents collection by monitoring the DocumentBeforeClose, DocumentOpen and NewDocument events. (Just as an alternative to using the Windows collection.)


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, November 1, 2012 4:42 PM
    Moderator
  • It appears that the bug is only in Word 2010 (and like Cindy, I can reproduce it there). Here's the output in Word 2013:

    01-11-2012    MS Office 15.0
    testdoc       test_document.docm
    add           Document2
    add           Document3
    add           Document4
    add           Document5
    count          1            Document5
    count          2            Document4
    count          3            Document3
    count          4            Document2
    count          5            test_document.docm
    close         Document5
    close         Document4
    count          1            Document3
    count          2            Document2
    count          3            test_document.docm
    doc            1            Document3
    doc            2            Document2
    doc            3            test_document.docm
    win            1            Document2
    win            2            Document3
    win            3            test_document.docm
    close         Document3
    close         Document2


    Jay Freedman
    MS Word MVP  FAQ: http://word.mvps.org

    Thursday, November 1, 2012 5:39 PM
  • Hi Cindy,

    Please do not worry about delay. Thanks for your effort and that you could confirm the issue.

    I also think this is a nasty one were the indexing is all wrong. I would not be surprised if many code uses the count property without any check and error handling, as there should be no need to.

    This is creepy because this bug will not always give wrong output and when it does will not always trigger an error in your code. And so your code may well do something wrong, just sometimes and without noticing.

    I want to think that Microsoft will respond to this bug. At least publish it as a known problem. I am unable to find such a list on the Microsoft site.

    Thank for your suggestion about the workaround. Perhaps if I build in a check for duplicate document names in the windows collection then using the windows collection as workaround is more robust than adding my own collection(more dependencies). But it’s just first thoughts about tackling the multiple windows problem that for sure will occur. I really have to think it over. For now I just build in a check for the Office version and inform the user that the add-in will unload itself due to this issue.

    Best regards,

    Paul

    Friday, November 2, 2012 9:30 AM
  • Hi Jay,

    Thanks for your reply.

    The output is in line with the output I get on Office 2003 and Office 2007 systems.

    Best regards,

    Paul

    Friday, November 2, 2012 9:39 AM
  • Hi Tom,

    For your information I changed the code fit to run in Visual Studio. Please find below the code and output. In line with the confirmation from Cindy one may expect that any application/code that depend on the Word 2010 document collection object is affected.

    Best regards,

    Paul

        Function test01() As Boolean
            '03-11-2012 1.0 Visual Studio 2008 VB code
            Dim WDapp As Microsoft.Office.Interop.Word.Application 
            Dim oDoc, oDoc0, oDoc1, oDoc2, oDoc3, oDoc4 As Microsoft.Office.Interop.Word.Document 
            Dim oWin As Microsoft.Office.Interop.Word.Window
            Dim n As Integer
    
            WDapp = CType(CreateObject("Word.Application"), Microsoft.Office.Interop.Word.Application)
            With WDapp
                Debug.Print(Format(Now(), "dd-MM-yyyy") & "  MS Office " & .Application.Version)
    
                oDoc0 = .Documents.Add : Debug.Print("add " & oDoc0.Name)
                oDoc1 = .Documents.Add : Debug.Print("add " & oDoc1.Name)
                oDoc2 = .Documents.Add : Debug.Print("add " & oDoc2.Name)
                oDoc3 = .Documents.Add : Debug.Print("add " & oDoc3.Name)
                oDoc4 = .Documents.Add : Debug.Print("add " & oDoc4.Name)
    
                For n = 1 To .Documents.Count
                    Debug.Print("count " & n & "  " & .Documents(n).Name)
                Next n
    
                Debug.Print("close " & oDoc4.Name) : oDoc4.Close() : oDoc4 = Nothing
                Debug.Print("close " & oDoc3.Name) : oDoc3.Close() : oDoc3 = Nothing
    
                For n = 1 To .Documents.Count
                    Debug.Print("count " & n & "  " & .Documents(n).Name)
                Next n
                n = 0
                For Each oDoc In .Documents
                    n = n + 1
                    Debug.Print("doc " & n & "  " & oDoc.Name)
                Next oDoc
                n = 0
                For Each oWin In .Windows
                    n = n + 1
                    Debug.Print("win " & n & " " & oWin.Document.Name)
                Next oWin
    
                Debug.Print("close " & oDoc2.Name) : oDoc2.Close() : oDoc2 = Nothing
                Debug.Print("close " & oDoc1.Name) : oDoc1.Close() : oDoc1 = Nothing
                Debug.Print("close " & oDoc0.Name) : oDoc0.Close() : oDoc0 = Nothing
    
            End With
    
            WDapp.Quit(False)
            WDapp = Nothing
        End Function
    

    Output:
    03-11-2012  MS Office 14.0
    add Document1
    add Document2
    add Document3
    add Document4
    add Document5
    count 1  Document5
    count 2  Document4
    count 3  Document3
    count 4  Document2
    count 5  Document1
    close Document5
    close Document4
    count 1  Document3
    count 2  Document2
    count 3  Document3
    doc 1  Document3
    doc 2  Document2
    doc 3  Document3
    doc 4  Document2
    doc 5  Document1
    win 1 Document1
    win 2 Document2
    win 3 Document3
    close Document3
    close Document2
    close Document1


    • Edited by bas258 Sunday, November 4, 2012 9:06 AM
    Saturday, November 3, 2012 12:55 PM
  • HI base258,

    I can reproduce your issue now!

    And I think what Jay's side is make sense, I will feedback it on my side and involve some experts into your thread to see whether there have some work round to solve it.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Monday, November 5, 2012 7:14 AM
    Moderator
  • Hi Bas258

    Like Cindy, Jay and Tom, I can see the problem in Word 2010 but not in Word 2003, 2007 or 2013.

    I have verified that the issue you’re experiencing is due to a known bug but unfortunately I do not have a timeframe on when this may be addressed. I am sorry for the inconvenience this is causing you.

    Best Regards,

    Donald M.
    Microsoft Online Community Support
    --------------------------------------------------------------------------------
    Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    Monday, November 5, 2012 9:30 PM
  • Hi Donald,

    Thanks for your time in replying.

    Good for letting know that this is a known bug. I feel inexpert not able to find this information before. Would it be possible to send me a link to a site were Microsoft publishes its Word 2007 known issues.

    Sorry to hear that there is no timeframe for adressing this issue. Can you please advise when a timeframe will be available.

    Best regards,

    Paul

    Tuesday, November 6, 2012 4:06 PM
  • Hi Paul,

    Any information that is available about issue would be found on Support.Micorosoft.Com but we do not create an article for every issue we find especially if we don't have a solution or if it has already been resolved in the latest product (as this was).

    Unfortunately not only is there no timeframe on when the issue may be address there is no timeframe on when there may be a timeframe.

    Best Regards,

    Donald M.
    Microsoft Online Community Support
    --------------------------------------------------------------------------------
    Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    Tuesday, November 6, 2012 4:56 PM
  • Hi all

    I started fiddling with a workaround for this and ran into something very interesting:

    If I have an Add-in (in my case a VSTO add-in) that loads with Word 2010, and this add-in "monitors" the NewDocument, DocumentOpen and DocumentClose events, the problem does not reproduce - at least, not on my system.

    I set up the add-in to maintain a System.Collections.Generic.List of type Word.Document with the intention of saving each document to the list when one is created/ opened and removing it from the list when one is closed - my code does all that. I also added a COMVisible interface and class to expose a DocumentsItem function that will return the Document from the List according to its Index in the list.

    I copied bas's VBA code that repros the problem into the VBA editor, extended it to show the results of the Add-in's function next to the repro information. I was very surprised to notice that the repro information was correct (although in the reverse order). I then commented out what I had added and ran bas's code as it stands here - it still worked correctly. My next step was to unload my add-in, and the repro code still gave the correct reults.

    I quit Word, started it again (the add-in was not loaded) and the repro code reproduced the "bad" results. Loaded the add-in, quit Word, started Word and ran the code (with the add-in loaded) and the problem with the Documents collection did not appear.

    So it seems that just the presence of an add-in monitoring these (or at least one - haven't tested that) events corrects the issue with the Documents collection...


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, November 9, 2012 10:45 AM
    Moderator
  • Further testing shows that loading a Normal.dotm that monitors these same events also results in "good" results. Note that after creating the events Word must be restarted.

    I have the following code in the ThisDocument project of Normal.dotm:

    Private Sub Document_Close()
        Debug.Print "Closing"
    End Sub
    
    Private Sub Document_New()
        Debug.Print "new"
    End Sub
    
    Private Sub Document_Open()
        Debug.Print "Opening"
    End Sub


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, November 9, 2012 10:51 AM
    Moderator
  • Could you tell me where to place this code in a VBA add-in (a global template)? I placed it in the ThisDocument object of the add-in, but I've found that nothing happens; the document collection is still incorrect and I don't see anything in the Immediate Window. I do have an Event handler that monitors App_DocumentOpen and App_NewDocument; I tried adding App_DocumentBeforeClose but that had no effect (I didn't really expect it to, as I don't have to close any documents to get the bad doc collection bug).

    Am I doing something wrong? I also tried commenting out my call to Register_Event_Handler so that the only events would appear in ThisDocument, but still no joy.

    Thanks in advance!

    Friday, January 25, 2013 5:47 PM
  • HI cchimi

    I didn't test this with a global VBA Add-in template because, in my experience, not all events in "ThisDocument" trigger in an add-in template. Not even all "Auto" macros trigger in a global VBA template. I think you have to use Normal.dotm

    The kind of add-in I was talking about is a VSTO (or any other type of COM) add-in.


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, January 28, 2013 4:20 PM
    Moderator
  • Ah, bummer. Well, thanks for the response!
    Tuesday, January 29, 2013 6:11 PM