Loop Help

moishy

Registered User.
Local time
Tomorrow, 01:34
Joined
Dec 14, 2009
Messages
264
I'm trying to create a new document based on a template which is stored on a network drive, to merge data to.

It will not load the first time, since there seems to be a Word 2010 bug, that Word is still initializing. It succeeds after several tries.

How can I create a loop that will try to create the document until it succeeds (try once every second for 5 seconds).

I thought I'd use something like this:
Code:
For i = 0 To 5
   Set WordDoc = WordApp.Documents.Open("Z:\Templates\Test.dot")
   i = i + 1
   [COLOR=Red]if opening succeeded then exit for[/COLOR]
Next i

The red line is where i need help.

Is there a better way of doing this?
 
Please show how you are Opening word.

You could try searching for Access and Word or similar. vbaInet has several posts on the subject
 
Code:
Dim oWord As Object
Set oWord = CreateObject("Word.Application")
 
I tried a quick search but didn't find anything, can you point me to some relevant posts?
 
I thought I'd use something like this:
Code:
For i = 0 To 5
   Set WordDoc = WordApp.Documents.Open("Z:\Templates\Test.dot")
   i = i + 1
   [COLOR=Red]if opening succeeded then exit for[/COLOR]
Next i
The red line is where i need help.

Here is a working example of how to use Exit For...

Code:
    For Each frm In Forms
      If frm.Name = MePointer.Name Then
        With frm
          .Filter = vbNullString
          .FilterOn = False
          .OrderBy = vbNullString
          .OrderByOn = False
        End With
        [B][COLOR=Red]Exit For[/COLOR][/B]
      End If
    Next frm
I must scan through the Forms collection and check each Form's Name property as I (in this area of the code) have opened the form via its class and the Form's Name is not added to the Forms collection as an index.

Once the loop code finds the form, it does not need to loop any farther.

Have you had failure of Word opening that has you considering such a remedy?
 
I'm considering looping until Err.Number <> 4198. Any comments?
 
@jdraw
I can't find anything relevant.
 
I don't think that the loop will help you.

If loading the word document fails it probably displays a hidden dialog box . Your application will hang until the document opens so I would suggest making word visible to see what is going on.

Code:
oWord.visible = True

You might also want to kill any WINWORD tasks that you may have left hanging around.
 

Users who are viewing this thread

Back
Top Bottom