Documents collection corrupt

B

Brian Knittel

I'm having problems with a VBA application I've written using macros and
Forms,
where the Documents collection appears to be corrupt -- a given document
will
appear in it more than once, and another not at all. For example, with the
following documents open:

UCT0816A.DOC
UCT0816B.DOC
UCL0816A.DOC
textentry.doc

after the app has been running a while, the following VBA snippet

for each doc in Application.Documents
Selection.TypeText doc.name & VbCR
next

might print

UCT0816A.DOC
UCT0816B.DOC
textentry.doc
textentry.doc

or some variation on this theme. The count is always correct, but
the document objects are not always unique. I can view the doc object
in the debugger inside the "for each" loop and see that it's definitely an
exact
duplicate of one of the documents. Content is the same, etc,
so it's not the case that I've simply changed the Name property of one
of the open documents. Word's "Window" menu does show the four
distinct filenames. The collection really is wrong.

All of these documents are attached to the same template (.dot file)
although
they mght have been created at different times, saved, and later reopened.

This is happening on Word 97 SR-2, Word 2003 SP2 and possibly other
versions.

Does this ring any bells for anyone? Is it possible that something I'm doing
is explicitly corrupting the Documents collection?

Thanks
Brian
 
R

Russ

When that happens, try
for each doc in Application.Documents
MsgBox doc.name
next

Actually typing into one of the documents with:
Selection.TypeText doc.name & VbCR
Might be confusing the code by activating the document being typed into?
 
R

Russ

In the off chance that you wanted to type into each document, you would need
to activate each one.

for each doc in Application.Documents
doc.activate
Selection.TypeText doc.name & VbCR
next
 
R

Russ

As an aside:
I've also noticed that when I minimize a document window that the next open
document becomes the active document automatically. That caught me off guard
the first time I did it because I was no longer sending commands with the
activedocument object to the document I intended. That's one reason to give
each document a doc object variable name to reference, instead of relying on
the activedocument object.
 
B

Brian Knittel

The active window is not the issue. The code I cited is just to
demonstrate the corruption problem. The problem is shown by what
is being displayed: Iterating through the Documents collection shows
that one document is missing and one is listed twice:


What I've found is that with several documents open,
the corruption occurs immedately after one of the documents has
been closed, either by the user closing it, or by a macro
programmatically closing it. It ALWAYS happens on Word 97,
when you close the first-opened of several documents. It happens
sometimes but not always on later versions of Word (with all SPs applied).

Has anyone run into this?

Thanks
Brian
 
H

Hanoch Abelman

Brian, I have run into this. I have spent some days trying to track it down, but with no luck. Did you ever solve it?
 
B

Brian Knittel

Yes, I worked around the bug by enumerating open windows and building a list of documents. You just have to recognized that a given document can have multiple windows

ndocs = 0
dim doclist(50,2)
For Each wnd In Application.Windows

Set doc = wnd.Document
If UCase(doc.name) = UCase(filename) Then
If doc.Saved Then
doc.Close
Set doc = Nothing
Else
warn_doc_is_edited doc
confirm_file_closed = False
End If
Exit Function
End If
Next

The Windows collection always works. The Documents collection should never be trusted.
 
B

Brian Knittel

Aw crud, that went out only half edited. I pasted in the subroutine from the project but was only partway through editing it into a generic example.

Please ignore the code in the previous reply.

Just use "for each wnd in Application.Windows" and look at wnd.Document to get the document objects, or wnd.Document.Name or wnd.Document.Path to get the filename or full file path.

Regards,
Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top