Waiting for a specific named document to close

M

Mike NG

I have a piece of code in my excel VBA which opens a document (e.g.
Labels). Hope this is the right group to post it on since I am using
the Word objects in my code


What I am trying to do is wait for either the Labels document or the
Word application to be closed. The code up to this point here is
standard automation so I won't repeat it.

lsDocName is correctly assigned to the name Labels at the start of the
routine

The theory of the loop is that it should wait for lsDocName to no longer
be in the Documents Name list where lbDone will be true and the loop
will exit. Or in case the word application is closed, I have used the
On Error GoTo to get me out. Basically I couldn't work out if this was
the best way of testing this

What's actually happening is the loop is exiting too soon on me closing
Document1 which I opened from the File menu of Labels. This is
happening every time

What I think may be happening is loWord.Documents.Count may have a value
of 2 for example, but by the time liLoop has the value of 2, Document1
has been closed,so the test on Documents(2).Name fails with an out of
range index and the On Error condition fires

I can't think how to get round this because I need error trapping on in
case Word is closed and all the variables go null. I thought the answer
may be to have an if condition around the Documents Name test, but the
same thing could most probably happen again.

I could use Documents.Count waiting for it to go 0 as my condition for
exiting the loop, but I don't want to give in that easily. How is it
done please



Dim lsDocName As String
Dim lbDone As Boolean
Dim liLoop As Integer
Dim loWord As Word.Application

<standard automation>
loWord.Activate

'Wait for either word to close or the most
'recently opened document to be closed
lsDocName = loWord.Documents(1).Name

On Error GoTo dun
Do Until lbDone
DoEvents
lbDone = True

For liLoop = 1 To loWord.Documents.Count
If loWord.Documents(liLoop).Name = lsDocName Then
lbDone = False
Exit For
End If
Next
Loop

dun:
Set loWord = Nothing
 
H

Helmut Weber

Hi Mike,
there are some ways to achieve what you want.
One way would be looping backwards,
which is necessary every now and then, but not here.
for l = documents.count to 1 step -1
if documents(l).name = "TheOpen.doc" then
docOpen = true
endif
next
---
or loop through the documents collection
without a counter at all:
For Each oDoc In Documents
If oDoc.Name = "TheOpen.doc" Then ' case sensitive
DocClose = False
End If
Next
HTH
Greetings from Bavaria, Germany
Helmut Weber
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
 
M

Mike NG

Hi Mike,
there are some ways to achieve what you want.
One way would be looping backwards,
which is necessary every now and then, but not here.
for l = documents.count to 1 step -1
if documents(l).name = "TheOpen.doc" then
docOpen = true
endif
next
---
or loop through the documents collection
without a counter at all:
For Each oDoc In Documents
If oDoc.Name = "TheOpen.doc" Then ' case sensitive
DocClose = False
End If
Next
HTH
Greetings from Bavaria, Germany
Helmut Weber
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
Cheers - I never thought of looping backwards. In fact I think the only
solution that will work is the With ... End With solution - to
effectively freeze the contents of the array. I am sure that with the
For Each loop, I will end up in a similar situation where closing a
document may mean that the one I am interested in has been shuffled
right to the beginning of the array and I may miss it
 
P

Peter Hewett

Hi Mike NG

Although With blocks lock arrays, I don't think it's going to do anything if you
try using it with the Word Documents collection - which I assume you were
intending? You can lock an array as it's just a block of memory but you can't
control what going on within an object.

Hope the above makes sense

HTH + Cheers - Peter
 
M

Mike NG

Although With blocks lock arrays, I don't think it's going to do anything if you
try using it with the Word Documents collection - which I assume you were
intending? You can lock an array as it's just a block of memory but you can't
control what going on within an object.
I just wanted to test the value of each of the Name parameters - it
seems to work fine now
 

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