XL: Can't Trap No or Cancel in "Replace Existing" Dialog BoxLast reviewed: September 2, 1997Article ID: Q116327 |
The information in this article applies to:
SYMPTOMSIn 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.
CAUSEWhen 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 failedBecause 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.
WORKAROUNDTo 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. Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.
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 STATUSMicrosoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.
REFERENCESFor more information about the Dir function, click the Index tab in Microsoft Visual Basic Help, type the following text
Dirand then double-click the selected text to go to the "Dir function" topic. For more information about the SaveAs method, click the Index tab in Microsoft Visual Basic Help, type the following text
SaveAsand then double-click the selected text to go to the "SaveAs method" topic. "Microsoft Excel Visual Basic User's Reference" Chapter 9, "Handling Errors and Error Values"
|
Additional query words: 5.00 5.00c 7.00 8.00 97
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |