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

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

Microsoft provides programming examples 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. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals 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 needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp
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 this to be a problem in the Microsoft products listed at the beginning of this article.


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.00c 8.00 97

Keywords : kbprg kbdta KbVBA
Version : WINDOWS:5.0,5.0c,97; Win95:7.0; winnt:5.0
Platform : Win95 WINDOWS winnt
Issue type :


Last Reviewed: November 9, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.