EXCEL: How To? Prevent abort in SaveAs when file exists ???
Esteemed Forum Participants and Lurkers:
========================================
EXCEL SaveAs Function Returns
I'm doing a SaveAs in my EXCEL VBA program:
aworkbook.SaveAs fileName:="TEST.xls"
If the file already exists, a modal dialog box pops up with the message:
A file named 'TEST.xls' already exists in this location. Do you want to replace it?
... and 3 buttons: Yes No Cancel
If Yes is selected, the program continues normally.
If No or Cancel is selected, the program aborts into MS Visual Basic with the message:
Run-time error '1004': Method 'SaveAs' of object '_Workbook' failed
How can I determine the returns from the No and Cancel buttons and process these returns in my program without having the program abort?
Thank you for your gracious comments, suggestions, and assistance.
Re: EXCEL: How To? Prevent abort in SaveAs when file exists ???
You need to add an error handler and trap for 1004 error.
VB Code:
Private sub command1_click
On error goto myerror
activeworkbook.Save
If activeworkbook.saved = true then
'blah
endif
exit sub
myerror:
if err.number = 1004 then
'canceled
endif
end sub
Re: EXCEL: How To? Prevent abort in SaveAs when file exists ???
RobDog:
Thanks ...
A significant part of the question I was asking: When the error is generated, is there any way to tell which button ... No or Cancel ... was clicked?
Re: EXCEL: How To? Prevent abort in SaveAs when file exists ???
If your saving the wb as the same name as the activeworkbook then it was in this line.
VB Code:
If activeworkbook.saved = true then
If the .Saved property is true then it was overwritten.
You could also just check for the file's existance before the save and handle it that way.
Re: EXCEL: How To? Prevent abort in SaveAs when file exists ???
I still can't figure out why there are 2 buttons (besides the "Yes") in the popup ... "No" and "Cancel" ... and what the difference is between them. They both seem to generate the exact same error, so how does one differentiate between them if I want them to program them to have different results?
I know how to program around this, but I am trying to figure out the buttons in this particular modal dialog box.
Re: EXCEL: How To? Prevent abort in SaveAs when file exists ???
I looked deeper into this and I dont see any way (yet) to differenciate between No and Cancel. :(
I guess its best just to test for the file existnace first. ;)
Re: EXCEL: How To? Prevent abort in SaveAs when file exists ???
That makes us even! Thanks for your help. I guess I'll have to program around it for now, but it will bug me until I figure out how to do it.
Re: EXCEL: How To? Prevent abort in SaveAs when file exists ???
I know how you feel, but I think if they havent given us the ability to do it in 2003 that they probably wont anytime soon. :(
In Word you can execute a dialog (not shown) and get results, but I tried in Excel but its unsupported.