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

Last reviewed: September 2, 1997
Article ID: Q116327
The information in this article applies to:
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for Windows NT, version 5.0
  • Microsoft Excel for Windows 95, version 7.0
  • Microsoft Excel 97 for Windows

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

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.

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

STATUS

Microsoft 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.

REFERENCES

For more information about the Dir function, click the Index tab in Microsoft Visual Basic Help, type the following text

   Dir

and 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

   SaveAs

and 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
Keywords : kbprg PgmOthr kbcode kbprg
Version : 5.00 5.00c 7.00 97
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 2, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.