Workbooks.Open() error handling
Hi
Use the error handler to avoid the crash, and assign the opened workbook to
a variable, so you can test if it was opened.
Dim MyBook As Workbook
On Error Resume Next
Set MyBook = Workbooks.Open("mybook.xlsm")
On Error GoTo 0
If MyBook Is Nothing Then
MsgBox "Sorry, the file was NOT found! Try again!"
' Then ask user to select a new file
Else
MsgBox "File was found and opened"
End If
Regards,
Per
"Robert Crandal" skrev i meddelelsen
...
My VBA module contains the following code:
Workbooks.Open ("mybook.xlsm")
If "mybook.xlsm" does not exist, then my VBA code
will crash immediately and the user will see an error
dialog box, plus the VBA program will stop running as
well.
If Workbooks.Open() fails, can I somehow instruct
VBA to detour to some alternate code rather than
crashing?? For example, can I use code somewhat
similiar to below:
If Workbooks.Open("mybook.xlsm") = FILE_WAS FOUND? then
MsgBox "File was found and opened"
Else
MsgBox "Sorry, the file was NOT found! Try again!"
' Then ask user to select a new file
End If
thank u
|