ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Failure at File Save (https://www.excelbanter.com/excel-programming/387166-failure-file-save.html)

Paige

Failure at File Save
 
I am doing a file save as, per the code below.

Dim fName as Variant
Do
fName = Application.GetSaveAsFilename(InitialFileName:="",
FileFilter:="Microsoft Excel Workbook (*.xls), *.xls")
Loop Until fName < False
ActiveWorkbook.SaveAs Filename:=fName, FileFormat:=xlWorkbookNormal

It works fine if the user types in a new file name and saves, or if the user
selects an existing file and elects to replace the existing file with the new
one. However, it doesn't work in the following scenario:

User selects an existing filename and hits 'Save'; a dialog box comes up
that says "File name already exists...do you want to replace it?", with the
options of 'Yes', 'No', and 'Cancel'. If the user selects either 'Cancel' or
'No' on the dialog (versus 'Yes'), it gives a run-time 1004 error: Method
'Save as' of '_Workbook' failed and goes to the last line of my code above.
I can't figure out how to capture these two scenarios ('Cancel' or 'No'). If
they select 'Cancel' or 'No', it just needs to go back to the saveas dialog
box.

Can someone assist me with this please?


Tom Ogilvy

Failure at File Save
 
Dim fName as Variant
again:
Do
fName = Application.GetSaveAsFilename(InitialFileName:="",
FileFilter:="Microsoft Excel Workbook (*.xls), *.xls")
Loop Until fName < False
On error Resume next
ActiveWorkbook.SaveAs Filename:=fName, FileFormat:=xlWorkbookNormal
On Error goto 0
if ActiveWorkbook.FullName < fName then goto again

--
Regards,
Tom Ogilvy

"Paige" wrote:

I am doing a file save as, per the code below.

Dim fName as Variant
Do
fName = Application.GetSaveAsFilename(InitialFileName:="",
FileFilter:="Microsoft Excel Workbook (*.xls), *.xls")
Loop Until fName < False
ActiveWorkbook.SaveAs Filename:=fName, FileFormat:=xlWorkbookNormal

It works fine if the user types in a new file name and saves, or if the user
selects an existing file and elects to replace the existing file with the new
one. However, it doesn't work in the following scenario:

User selects an existing filename and hits 'Save'; a dialog box comes up
that says "File name already exists...do you want to replace it?", with the
options of 'Yes', 'No', and 'Cancel'. If the user selects either 'Cancel' or
'No' on the dialog (versus 'Yes'), it gives a run-time 1004 error: Method
'Save as' of '_Workbook' failed and goes to the last line of my code above.
I can't figure out how to capture these two scenarios ('Cancel' or 'No'). If
they select 'Cancel' or 'No', it just needs to go back to the saveas dialog
box.

Can someone assist me with this please?



All times are GMT +1. The time now is 12:19 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com