Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default If user says "No" to FileSaveAs Overwrite?

My macro asks the user for a FileSaveAsName, then tries to save the file with:

ActiveWorkbook.SaveAs Filename:=sFileSaveName, ...

How can I tell if the user gets the "File exists, Overwrite?" prompt, and
answers "no"?

Thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,327
Default If user says "No" to FileSaveAs Overwrite?

Hi

You let the user do user things and check if the file path and filename is
identical to sFileSaveName in the end.

But I suspect this is not your final goal. You want to always overwrite, or
not allow overwrite, or not to ask the user ?

HTH. Best wishes Harald

"OceansideDJ" skrev i melding
...
My macro asks the user for a FileSaveAsName, then tries to save the file

with:

ActiveWorkbook.SaveAs Filename:=sFileSaveName, ...

How can I tell if the user gets the "File exists, Overwrite?" prompt, and
answers "no"?

Thanks



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default If user says "No" to FileSaveAs Overwrite?

Harald,
If the user picks "No" to the overwrite, I want to ask for another filename.

Currently, if the the user picks "no", I get runtime error 1004, Method
"SaveAs of object '_Workbook' failed.

Could I detect error 1004?
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 860
Default If user says "No" to FileSaveAs Overwrite?

Hi OceansideDJ,

OceansideDJ wrote:
If the user picks "No" to the overwrite, I want to ask for another
filename.

Currently, if the the user picks "no", I get runtime error 1004,
Method "SaveAs of object '_Workbook' failed.

Could I detect error 1004?


Yes, you can use On Error Goto or On Error Resume to trap runtime error
1004. Here's one way:

Sub Demo()
Dim vFileName As Variant
Dim bSaved As Boolean

vFileName = True
Do Until (vFileName = False) Or bSaved
vFileName = Application.GetSaveAsFilename
On Error Resume Next
If vFileName < False Then
ActiveWorkbook.SaveAs vFileName
bSaved = (Err.Number = 0)
End If
On Error GoTo 0
Loop

If bSaved Then MsgBox "Saved"
End Sub

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default If user says "No" to FileSaveAs Overwrite?

Jake,
Excellent. Thanks for a more elegant solution than mine.




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default If user says "No" to FileSaveAs Overwrite?

Another way is to check for yourself:

Option Explicit
Sub testme01()
Dim myFileName As Variant
Dim OkToSave As Boolean
Dim resp As Long

Do
myFileName = Application.GetSaveAsFilename _
(filefilter:="Excel files, *.xls")
If myFileName = False Then
Exit Sub
End If

OkToSave = True
If Dir(myFileName) = "" Then
'do nothing special
Else
resp = MsgBox(prompt:="Overwrite Existing file?", _
Buttons:=vbYesNoCancel)
Select Case resp
Case Is = vbCancel
MsgBox "Try again later"
Exit Sub
Case Is = vbNo
OkToSave = False
End Select
End If

If OkToSave Then
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=myFileName, _
FileFormat:=xlWorkbookNormal
Application.DisplayAlerts = True
Exit Do
End If
Loop

End Sub


OceansideDJ wrote:

Harald,
If the user picks "No" to the overwrite, I want to ask for another filename.

Currently, if the the user picks "no", I get runtime error 1004, Method
"SaveAs of object '_Workbook' failed.

Could I detect error 1004?


--

Dave Peterson

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel 2007 displays Locked by "another user", does not show User I Ana Excel Discussion (Misc queries) 2 January 19th 11 01:57 PM
How do I suppress the "Do you want to overwrite the destination cells" message Rojo Habe Setting up and Configuration of Excel 1 July 30th 09 02:24 PM
Excel - Golf - how to display "-2" as "2 Under" or "4"as "+4" or "4 Over" in a calculation cell Steve Kay Excel Discussion (Misc queries) 2 August 8th 08 01:54 AM
How do you turn off "Save a copy/Overwrite changes" dialog box Nicko Excel Discussion (Misc queries) 4 December 8th 05 05:22 PM
Find a workbook and "overwrite it with the latest Excel format" PSKelligan Excel Programming 2 October 26th 04 01:45 PM


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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"