Use IF to Loop or not? AND IF w/ error codes?

E

Ed

I had an idea, and thought I would post it here to see if it survived
scrutiny. A line in my macro errored because another process wasn't
completely done. Is it possible and would it be okay to use On Error Resume
Next and the error code with an IF to loop back through the missed
instruction until it completes?

I'm thinking something like:

Do
On Error Resume Next
' Do This
If Err.Number <> 0 Then
Loop
Else Exit Do
End If
On Error GoTo 0

Ed
 
J

Jezebel

It's perfectly reasonable to ignore errors that are not harmful -- in fact
there are some things that you really can't do any other way. However, you
can't make the Loop instruction itself conditional. Here is cleaner code for
what you are trying to do --

On error resume next
Do
SomeFunction
Loop until Err.Number = 0
On error goto 0

But, this is VERY risky coding unless you can be confident that SomeFunction
returns only non-critical errors, and that it will succeed on subsequent
attempts. If the error is unrecoverable your code will loop for ever.
 
J

Jay Freedman

You can remove some of the risk by deciding in advance how many loops
you're willing to allow. Something like this:

Const MAXLOOPS = 1000
Dim nLoops As Long
nLoops = 0
On error resume next
Do
SomeFunction
nLoops = nLoops + 1
Loop Until (Err.Number = 0) Or (nLoops > MAXLOOPS)
If nLoops > MAXLOOPS Then
MsgBox "SomeFunction timed out"
End If
On error goto 0
 
J

Jezebel

You could also be selective about which particular errors to ignore by
checking the value of Err.Number
 
M

Malcolm Smith

Ed

I think that the solution here is the wrong way of going about things.
Exactly what is being done and what is the process for which you are
waiting?

Let's see if we can improve the design rather than rely on this rather,
um, interesting hack. As Steve pointed out if something happened which
you didn't expect then awful things would happen.

As for the simple loop idea; I don't like that either. What may take a
thousandth of a second on one machine may take three seconds on another.
What's wrong about writing a simple delay subroutine such as:

Public Sub Delay(nSeconds As Long)

Dim nStart As Long

nStart = Timer

Do While Timer < nStart + nSeconds
DoEvents
Loop

End Sub


Note the use of the DoEvents; after all if you are waiting for the other
process to finish you will actually want to allow the other process to get
going.


Hut I still feel that the design is wrong. What are you trying to do?

- Malc
www.dragondrop.com
 
E

Ed

Please excuse the long delay - the weekend jumped up and bit me!

Malcolm Smith said:
Ed

I think that the solution here is the wrong way of going about things.
Exactly what is being done and what is the process for which you are
waiting?

I'm saving a document twice, once using Save and immediately again using
SaveAs. If the process isn't complete when it drops to the next
instruction, it errors. Maybe because I've got Background Saves turned on?

Ed
 
E

Ed

Thanks for staying with me, Malcolm. I've included my code this time.
Basically, I'm setting a bookmark in my open doc, doing a save and SaveAs,
then re-opening my original doc and going to the bookmark. But I get an
error trying to go to the bookmark - it says my doc1 object has been deleted
(not the bookmark). After clicking out of the error message, the doc opens
and the bookmark is there. The only thing I can figure is the doc isn't
completely open by the time the macro wants to go to the bookmark.

Ed

Dim doc1 As Document
Set doc1 = ActiveDocument
Dim FName As String
Dim FPath As String
FName = doc1.Name
FPath = doc1.FullName

' Save place in doc
ActiveDocument.Bookmarks.Add ("Here")

' Save doc in place
doc1.Save
' Save backup
doc1.SaveAs FileName:="E:\Private\" & FName

' Return to original doc at bookmark; delete
ActiveDocument.Close
Documents.Open FPath
doc1.Bookmarks("Here").Select ' ERROR HERE-DOC1 "DELETED"?!?
doc1.Bookmarks("Here").Delete
 
M

Malcolm Smith

I think that the reason that you are getting that error is that you have
the pointer called doc1 which is pointing to the active document.

You then close the ActiveDocument. Which means that doc1 is not pointing
to a valid document any more.

This is where your problem lies.

- Malc
www.dragondrop.com
 
E

Ed

I see what I did. I set a STRING to the ActiveDocument name, but never set
the OBJECT to reflect the actual doc at that path. Okay, now I can wok
through that. Thanks so much, Malcolm.

Ed
 

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