vbams-wordword-2010

Wrong Application.Documents enumeration


My question is short. Unfortunately my examples need a lot of text.

Short version

Usually Application.Documents works fine if I just have opened some files. But after a while (open/close other files and so on) the returned enumeration is quite often but not always wrong! (Even the following simple code example fails then.)

Sub test_SaveAllUnsavedDocuments()

    Dim Doc As Object

    For Each Doc In Word.Application.Documents
        Debug.Print Doc.Name
    Next
    Debug.Print "Word.Application.Documents.Count: " & Word.Application.Documents.Count

End Sub

As far as I can tell:

(I have reproduced this on another PC without any Add-Ons too. OS version is: Windows 7, 64 bit.)

Long version

I open some files. Example file names:

"6-1-spare parts (RSM)-2.22-de-DE.docx"
"6-1-spare parts (RSM)-2.22-en-GB.docx"
"6-1-spare parts (RSM)-2.22-en-US.docx"

My procedure returns:

6-1-spare parts (RSM)-2.22-en-US.docx
6-1-spare parts (RSM)-2.22-en-GB.docx
6-1-spare parts (RSM)-2.22-de-DE.docx
Word.Application.Documents.Count: 3

I open another file ("Test.docx") or create a new document via CTRL+N. My procedure returns:

Test.docx
6-1-spare parts (RSM)-2.22-en-US.docx
6-1-spare parts (RSM)-2.22-en-GB.docx
6-1-spare parts (RSM)-2.22-de-DE.docx
Word.Application.Documents.Count: 4

Now the errors start

I close "Test.docx". My procedure returns:

6-1-spare parts (RSM)-2.22-en-US.docx
6-1-spare parts (RSM)-2.22-en-US.docx
6-1-spare parts (RSM)-2.22-en-GB.docx
6-1-spare parts (RSM)-2.22-de-DE.docx
Word.Application.Documents.Count: 3

There is one duplicate!

I repeat this and my procedure returns:

6-1-spare parts (RSM)-2.22-en-US.docx
6-1-spare parts (RSM)-2.22-en-GB.docx
6-1-spare parts (RSM)-2.22-en-US.docx
6-1-spare parts (RSM)-2.22-en-GB.docx
6-1-spare parts (RSM)-2.22-de-DE.docx
Word.Application.Documents.Count: 3

There are two duplicates!

I repeat this and my procedure returns:

6-1-spare parts (RSM)-2.22-en-US.docx
6-1-spare parts (RSM)-2.22-en-GB.docx
6-1-spare parts (RSM)-2.22-de-DE.docx
6-1-spare parts (RSM)-2.22-en-US.docx
6-1-spare parts (RSM)-2.22-en-GB.docx
6-1-spare parts (RSM)-2.22-de-DE.docx
Word.Application.Documents.Count: 3

There are three duplicates! And: now the number of listed file names equals two times the actual number of open files!

I repeat this one more time and my procedure returns:

6-1-spare parts (RSM)-2.22-en-US.docx
6-1-spare parts (RSM)-2.22-en-GB.docx
6-1-spare parts (RSM)-2.22-de-DE.docx
Word.Application.Documents.Count: 3

No matter what I do now, the returned list is (and remains) correct until I close and re-open the documents.


Solution

  • I have heard about the For ..Each loop not being reliable, but this is the first time I came across an actual sample. The following code doesn't produce that error.

    Sub test_SaveAllUnsavedDocuments()

    Dim Doc As Object
    Dim i As Integer
    
    For i = 1 To Word.Application.Documents.Count 
        Debug.Print Documents(i).Name
    Next
    Debug.Print "Word.Application.Documents.Count: " & Word.Application.Documents.Count
    

    End Sub

    At first I tried declaring Doc As Document, but that didn't make a difference.