ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Do you want to replace it? (https://www.excelbanter.com/excel-programming/337360-do-you-want-replace.html)

AA2e72E

Do you want to replace it?
 
From VB script, when executing the code:

ActiveWorkbook.SaveAs FileName:="C:\ZZ.XLS"

a dialogue comes up when the file exists already, requesting whether the
filename should be overwritten. How can I retrieve the option exercised by
the user: YES, NO, CANCEL?



Jim Rech

Do you want to replace it?
 
What you should do is determine whether the file exists before the SaveAs
and ask the user what he wants to do:

Sub a()
Dim Resp As Integer
Dim FName As String
FName = "c:\abc.xls"
If Dir(FName) < "" Then
Resp = MsgBox(FName & " exists. Replace it?", vbYesNoCancel)
Select Case Resp
Case vbYes: MsgBox "User chose replace"
Case vbNo: MsgBox "User chose not to replace"
Case vbCancel: MsgBox "User chose cancel"
End Select
End If
End Sub

Of course in place of the message boxes in the Select you'd put the
appropriate actions, for Yes doing a SaveAs with Application.DisplayAlerts
set to False so the user is not prompted to replace the existing file.

--
Jim
"AA2e72E" wrote in message
...
| From VB script, when executing the code:
|
| ActiveWorkbook.SaveAs FileName:="C:\ZZ.XLS"
|
| a dialogue comes up when the file exists already, requesting whether the
| filename should be overwritten. How can I retrieve the option exercised by
| the user: YES, NO, CANCEL?
|
|




All times are GMT +1. The time now is 10:58 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com