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
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