Method 'SaveAs of Object'_Workbook' failed error handling routine help needed.

blbat

Active Member
Joined
Mar 24, 2010
Messages
338
Office Version
  1. 2016
  2. 2013
The following code takes the results of previous working code (not shown here) that updates an existing Excel file.


Code:
With ActiveWorkbook.Sheets("mysheetname")
    .Copy
    ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & Application.PathSeparator & "mysheetname", FileFormat:=51
End With


When this code executes it properly calls the Excel Message:
"this file already exists in this location. Do you want to replace it?"
with the Yes | No | Cancel buttons.

If the user selects "Yes" there is no error.
If the user selects "No" or "Cancel" the Run-time error '1004' is thrown, with message "Method 'SaveAs' of Object '_Workbook' failed"

I need some help with an error handler, but don't know the proper way, or location to put it.
I placed an handling routine inside the "With" statement as such:


Code:
With ActiveWorkbook.Sheets("mysheetname")
    On Error GoTo ErrHandle
        .Copy
        ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & Application.PathSeparator & "mysheetname", FileFormat:=51
    ErrHandle:
        If Err.Number = 1004 Then
              MsgBox "File exists, you must select 'Yes" to overwrite"
              Resume
          End If

End With

and it works...but is not optimal.I need to capture the user response "Cancel" to exit sub. otherwise they are in an endless loop.

any ideas would be greatly appreciated.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Is it your intent to offer the user a choice about overwriting an existing file? Because if not then you can just use this:
VBA Code:
    With ActiveWorkbook.Sheets("mysheetname")
        .Copy
        Application.DisplayAlerts = False
        ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & Application.PathSeparator & "mysheetname", FileFormat:=51
        Application.DisplayAlerts = True
        ActiveWorkbook.Close False
    End With

If you DO want to offer a choice, then this way is expedient, if not too elegant.
VBA Code:
    With ActiveWorkbook.Sheets("mysheetname")
        .Copy
        On Error Resume Next
        ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & Application.PathSeparator & "mysheetname", FileFormat:=51
        On Error GoTo 0
        ActiveWorkbook.Close False
    End With

Personally, I like to add code to do the testing so I can better control what I do with user response
VBA Code:
    Dim FilePath As String, WritePerm As Boolean
    With ActiveWorkbook.Sheets("mysheetname")
        FilePath = ThisWorkbook.Path & Application.PathSeparator & .Name & ".xlsx"
        
        With CreateObject("Scripting.FileSystemObject")
            WritePerm = False
            If .FileExists(FilePath) Then
                Select Case MsgBox("File already exists. Overwrite?", vbYesNoCancel Or vbQuestion, Application.Name)
                    Case vbYes
                        WritePerm = True
                    Case vbCancel
                        Exit Sub
                End Select
            Else
                WritePerm = True
            End If
        End With
        
        If WritePerm Then
            .Copy
            Application.DisplayAlerts = False
            ActiveWorkbook.SaveAs Filename:=FilePath, FileFormat:=51
            Application.DisplayAlerts = True
            ActiveWorkbook.Close False
        End If
    End With
 
Upvote 0
Solution
I appreciate the help rlv01.
I liked what you did with that last bit...so used it.
The Only thing I did different is directly address the Case vbNo
although testing it with that Case vbNo commented out showed no difference in the execution of the code. Must be some Excel native cabability to handle "No"?

marking it as SOLVED.


Code:
 Dim FilePath As String, WritePerm As Boolean

    With ActiveWorkbook.Sheets("mysheetname")
        FilePath = ThisWorkbook.Path & Application.PathSeparator & .Name & ".xlsx"
        
        With CreateObject("Scripting.FileSystemObject")
            WritePerm = False
            If .FileExists(FilePath) Then
                Select Case MsgBox("File already exists. Overwrite?", vbYesNoCancel Or vbQuestion, Application.Name)
                    Case vbYes
                        WritePerm = True
                    Case vbNo
                        Exit Sub
                    Case vbCancel
                        Exit Sub
                End Select
            Else
                WritePerm = True
            End If
        End With
        
        If WritePerm Then
            .Copy
            Application.DisplayAlerts = False
            ActiveWorkbook.SaveAs Filename:=FilePath, FileFormat:=51
            Application.DisplayAlerts = True
            ActiveWorkbook.Close False
        End If
    End With
 
Upvote 0
The Only thing I did different is directly address the Case vbNo
although testing it with that Case vbNo commented out showed no difference in the execution of the code. Must be some Excel native cabability to handle "No"?

Nothing wrong with putting in a Case vbNo . I left it out because it was not needed due to the way the code is structured. But it does not hurt anything.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,213
Members
452,618
Latest member
Tam84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top