Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Workbooks.Open errors

Status
Not open for further replies.

segmentationfault

Programmer
Jun 21, 2001
160
US
Is there a way to deal with a failed Workbooks.Open without an On Error statement? For example, if the workbook is already open and changed, a dialog appears asking the user if she wants to reopen the workbook and lose the changes. If she clicks "No", the Open method throws an error. Is it possible to disregard this error without the cludgy On Error handlers?

If it is not possible, then I have considered encapsulating any call to Workbooks.Open in its own sub containing an On Error statement. As I understand it, though, an On Error statement retains relevance even after execution has left that subroutine. How can I use an On Error statement in one sub but then return to the default error behavior afterwards?
 
1: Why don't you check to see if the WB is already opened before attempting to open it?
Code:
Sub ListWorkbooks()
    Dim wb As Workbook
    For Each wb In Application.Workbooks
        MsgBox wb.Name
    Next wb
    Set wb = Nothing
End Sub
If you are not using error handling routines (not a good practice), you can return to the state with:
On Error Goto 0
Code:
Sub Sample()
    On Error Resume Next
    Workbooks.Open "C:\SomeFile.xls"
    If Err.Number <> 0 Then
        Err.Clear      'Error
        'Attach to the WB already opened
    End If
    On Error GoTo 0
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top