Handling Errors with Workbooks.OpenText

D

David

I've got this line of code:

Workbooks.OpenText FileName:=sFilePath & ".xls", _
DataType:=xlDelimited, Tab:=True

The file is opened using the path and filename entered by the user
into a form.

How can I handle errors so that if the file/path is invalid or the
file doesn't exist, it gives an error message rather than falling
over?

Thanks.
 
P

papou

Hello David
Use an Error Handler eg:
Sub tester()
On Error GoTo tester_Error

Workbooks.OpenText Filename:=SFilepath & ".xls", _
DataType:=xlDelimited, Tab:=True

On Error GoTo 0
Exit Sub

tester_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure
tester of Module Module1"

End Sub

HTH
Cordially
Pascal
 
D

David

Hello David
Use an Error Handler eg:
Sub tester()
On Error GoTo tester_Error

Workbooks.OpenText Filename:=SFilepath & ".xls", _
DataType:=xlDelimited, Tab:=True

On Error GoTo 0
Exit Sub

tester_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure
tester of Module Module1"

End Sub

HTH
Cordially
Pascal

"David" <[email protected]> a écrit dans le message de (e-mail address removed)...








- Show quoted text -

Thanks Pascal, that was exactly what I wanted.
 

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