Results 1 to 12 of 12
  1. #1
    JustCallMeAl
    Guest

    Documents Collection Failure? (VBA/Word97 SR2)

    I have a situation where using the following code does not produce a list of all the open documents.

    For Each aDoc In Documents
    aName = aName & aDoc.Name & vbCr
    Next aDoc
    MsgBox aName

    This should create a message box listing all the documents open, but it isn't.

    Now for some more detail.

    The code is called after a loop of creating documents via code. This number of documents is controlled by the user clicking on a command button to create a new document. The documents are also saved. Therefore, if the user uses this process to create two documents, for example, looking under the Window menu, we will see two documents listed: ExampleDoc1.doc and ExampleDoc2.doc. Running the above macro should yield a message box containing both file names.

    However, my users and I are getting a different result. We get the message box listing ExampleDoc1.doc twice!

    And, unfortunately, it seems to be sporadic... [img]/forums/images/smilies/sad.gif[/img]

    What I have done to "remedy" the problem is to use the Window.Panes collection. That seems to produce the expected results.

    Have others had similar experiences with the Documents collection? Is this a known "bug"? Is the problem "produced" by creating the documents by code?

    Thanks.

  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Documents Collection Failure? (VBA/Word97 SR2)

    u need to test the documents.count to verify the size of the documents collections.
    I've not yet seen VBA lie to me about the number of documents.

  3. #3
    JustCallMeAl
    Guest

    Re: Documents Collection Failure? (VBA/Word97 SR2)

    Why? The for each/next takes care of that, as it looks at each item in the collection.

    Further, I did just that. I changed the for each/next to a for/next where I went from 1 to Documents.Count and I also tried Documents.Count to 1 step -1. Produced the same results.

    Also, I know for a fact that I had only two documents open, and they were saved to unique names. When I ran the above code, I got listed one document name twice. So the For Each/Next worked the proper number of times, it just didn't get the right file name.

  4. #4
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Documents Collection Failure? (VBA/Word97 SR2)

    Hi Al,

    I guess if you put an aDoc.Activate into the loop to see which Document is processed, and single-step through the macro, the error doesn't occur?

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>Klaus

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Documents Collection Failure? (VBA/Word97 SR2)

    I guess my question is: assuming this could be caused by the documents changing sequence in the collection so that you queried the same one twice, what could cause the documents to change sequence?

  6. #6
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Documents Collection Failure? (VBA/Word97 SR2)

    "Why?"

    Because, if you have encountered a bug, using an alternative mechanism to verify the document count might identify a problem.

  7. #7
    JustCallMeAl
    Guest

    Re: Documents Collection Failure? (VBA/Word97 SR2)

    As I had stated above, I had already done that without a change in the results.

  8. #8
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Documents Collection Failure? (VBA/Word97 SR2)

    Al,

    (Just grasping at straws..) Could this be some sort of timing problem?

    After each new document is created via code, internally Word has to update the Documents collection.
    Perhaps under certain circumstances, for ex. due to high CPU usage due to other processes, Word has not yet finished updating the Documents collection when the 'For Each aDoc in Documents' statement runs. (Whether that can even happen would depend on whether Word's internal processes and the execution of VBA code are capable of running asynchonously - VBA runs in the same process space as Word, but perhaps via different threads? - a bit over my head...)

    Would the problem go away if you built in a little delay between the creation of the documents and the running of the For Each loop?

    Gary

  9. #9
    JustCallMeAl
    Guest

    Re: Documents Collection Failure? (VBA/Word97 SR2)

    Good questions, Gary.

    The creation of the documents occurs, and that macro closes out.

    The process that runs through the documents collection can then occur from 1 second later to 1 quadzillion seconds later. It is invoked from another macro.

    Further, while I was trying to find out what was happening, I had two documents open (which I had created by the first macro), and I was working on it for a very long time. Therefore, there was a lot of time between creation and cycling through the documents collection.

    My only thought on this is that creating these documents by macros does cause some internal "variable" to NOT be updated correctly.

    So, I was looking to see if others had experienced this phenomenon. Apparently not.

    The really weird part is that I can cycle through the panes collection, asking for the pane.document.name, and get the correct result. Documents collection? No. Panes collection? Yes.

  10. #10
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Documents Collection Failure? (VBA/Word97 SR2)

    I'm curious: Did you try my suggestion?

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>Klaus

  11. #11
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Documents Collection Failure? (VBA/Word97 SR2)

    Al,

    Well it it does sound like a Word/VBA bug, only doubt there is why no one else has appeared to stumble on it previously.

    With regard to returning the document name via Documents, does:

    aDoc.Windows(1).Caption

    do any good?

    Gary

  12. #12
    JustCallMeAl
    Guest

    Re: Documents Collection Failure? (VBA/Word97 SR2)

    No. And no reason not to, just didn't. Since I was looking for whether this was a bug or not and I already had a working workaround, I just chose not to.

    Thanks, anyway.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •