Microsoft KB Archive/116327

= Can't Trap No or Cancel in "Replace Existing" Dialog Box =

Article ID: 116327

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Excel 97 Standard Edition

-



This article was previously published under Q116327





SYMPTOMS
In Microsoft Excel, if you use the SaveAs method of the Workbook object to save a file, and you save the file with the same name and location as an existing file, you cannot determine whether the No or the Cancel button is chosen in the "Replace Existing" dialog box that appears when you save the file.



CAUSE
When you use the SaveAs method of the Workbook object to save a file, and the file already exists, both the No and the Cancel button in the "Replace Existing" dialog box cause the following error message to appear:

Run-time error '1004':

SaveAs method of Workbook class failed

Because of this behavior, you cannot trap this error, and you cannot determine in your macro whether the Cancel button was chosen, or the No button was chosen.



WORKAROUND
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. To work around this behavior, you can use the Dir function to determine whether the file already exists, and then use the MsgBox function to trap the response to either replace the existing file, or not. The following macro is an example of this method. Sub Save_File ' Dimension variables Dim FileToSave, ExistFile, Msg As String Msg = "File already exists. Save on top of existing file?" ' Define name and directory to save active workbook to     FileToSave = "C:\TEST.XLS" ' Check for existing file ExistFile = Dir(FileToSave) If ExistFile <> "" Then If MsgBox(Msg, vbExclamation + vbOKCancel) = vbOK Then ' OK chosen, replace following line with desired code MsgBox "Save over existing file" Else ' Cancel chosen, replace following line with desired code MsgBox "Cancel save" End If     ' If file does not exist, save file Else ThisWorkbook.SaveAs ("C:\TEST.XLS") End If  End Sub



STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.

