Error Handling

D

dsc

The following code has a problem:

OnError GoTo Handler



(snip)



TryAgain:



DirString = ActiveDocument.Variables("CompanyName").Value

MyDocLocation = "C:\My Documents\" & DirString & "\" & NameString

CheckString = "C:\My Documents\" & DirString

PathExists = Len(VBA.Dir(PathName:=CheckString, Attributes:=vbDirectory))



Err.Clear





If Not PathExists = 0 Then

ActiveDocument.SaveAs FileName:=MyDocLocation


Else

SaveHere = MyDocLocation

With SaveWorkingFileForm

SaveWorkingFileForm.TextBox1.Text = MyDocPath

SaveWorkingFileForm.TextBox1.SelStart = 0

SaveWorkingFileForm.TextBox1.SelLength =
Len(SaveWorkingFileForm.TextBox1.Text)

SaveWorkingFileForm.Show

End With

End If



Handler:

If Err.Number = 5153 Then

With SaveWorkingFileForm4

SaveWorkingFileForm4.Label2.Caption = "There is already a file
named " _

& NameString & " in directory " & CheckString & ", and it can't
be " _

& "overwritten because " _

& "it is open for editing. You must either:" & (Chr(13)) _

& "1. Give your file a different name." & (Chr(13)) _

& "2. Change the company name." & (Chr(13)) _

& "3. Cancel this macro."

SaveWorkingFileForm4.Show

GoTo TryAgain

End With

End If

Resume Next



What this is supposed to do is handle error 5153, trying to save a file with
the same name as an open file. It only kicks in where the path exists. The
line ActiveDocument.SaveAs FileName:=MyDocLocation generates error 5153,
sending us down to Handler:.



It works fine the first time through. However, if the user clicks on the
form's OK button again with the same file name in the TextBox, instead of
going to Handler: on error, it generates word's default macro error
dialogue, with its "debug" and other buttons.



Does anyone know why OnError GoTo Handler works the first time and then
stops?



All assistance gratefully received.



dsc

Yokohama
 
C

Cindy M -WordMVP-

Hi Dsc,
It works fine the first time through. However, if the user clicks on the
form's OK button again with the same file name in the TextBox, instead of
going to Handler: on error, it generates word's default macro error
dialogue, with its "debug" and other buttons.

Does anyone know why OnError GoTo Handler works the first time and then
stops?
If I'm not mistaken, it's because you GoTo back into the code while still
within the error handler. The error handler can't be recursive (handle and
error within an error handling action). Try using Resume [Label] instead of
GoTo.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

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