Cant find workbook

  • Thread starter Thread starter Todd Huttenstine
  • Start date Start date
T

Todd Huttenstine

The below code opens the workbook in Textbox6.
Workbooks.Open TextBox6.Value

If the workbook is not located, I get a VBA error, instead
I would like a messagebox to popup and say "Workbook not
found." and then exit sub.

Thanx

Todd Huttenstine
 
Try this:

On Error Resume Next 'disable error
myBook =TextBox6.Value
Set testOpened = Workbooks.Open(myBook)
If Not IsObject(testOpened) Then
MsgBox myBook & " not found"
End If
On Error GoTo 0 'reset error

....Chris
 
Here are a couple ways to trap the error:

Sub Way1()
If Dir(TextBox6.Value) <> "" Then
Workbooks.Open TextBox6.Value
Else
MsgBox "File cannot be found"
Exit Sub
End If
''Continues....
End Sub

Sub Way2()
On Error Resume Next
Workbooks.Open TextBox6.Value
If Err.Number <> 0 Then
MsgBox "Error opening workbook. The specific" & _
"Excel error message was " & Err.Description
Exit Sub
End If
''Continues....
End Sub
 
On Error GoTo e
Workbooks.Open TextBox6.Value

Exit Sub
e: MsgBox "Workbook not found."
 
Back
Top