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
Russ - 17 Aug 2007 19:34 GMT
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?
> I'm having problems with a VBA application I've written using macros and
> Forms,
[quoted text clipped - 42 lines]
> Thanks
> Brian

Signature
Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
Russ - 17 Aug 2007 19:41 GMT
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
> I'm having problems with a VBA application I've written using macros and
> Forms,
[quoted text clipped - 42 lines]
> Thanks
> Brian

Signature
Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
Russ - 17 Aug 2007 19:52 GMT
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.
> I'm having problems with a VBA application I've written using macros and
> Forms,
[quoted text clipped - 42 lines]
> Thanks
> Brian

Signature
Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
Brian Knittel - 17 Aug 2007 22:08 GMT
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:
>> UCT0816A.DOC
>> UCT0816B.DOC
>> textentry.doc
>> textentry.doc
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