View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Jake Marx[_3_] Jake Marx[_3_] is offline
external usenet poster
 
Posts: 860
Default VBA Question - Canceling a 'Save As' command from a user form

Hi V-Man,

A few things you can do here. If the user clicks Cancel, then
GetSaveAsFilename will return False. So if you set up your variable as a
Variant, you can check for that. If the user is prompted to overwrite and
clicks No, VBA will raise a runtime error. So you can ignore that error if
you'd like.

Here's an example:

Private Sub SaveButton_Click()
Dim vName As Variant

On Error GoTo ErrHandler

vName = Application.GetSaveAsFilename( _
fileFilter:="Excel Files (*.xls), *.xls, Text Files" & _
"(*.txt), *.txt, All Files (*.*), *.*")

If Not vName = False Then
ActiveWorkbook.SaveAs Filename:=CStr(vName)
End If

ExitRoutine:
Exit Sub
ErrHandler:
If Err.Number < 1004 Then
MsgBox "Unexpected error." & vbLf & CStr(Err.Number) & _
": " & Err.Description, vbExclamation, "Error"
End If
Resume ExitRoutine
End Sub

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

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


V-Man wrote:
Hi all,

I have built a user form in VBA that allows a user to enter and save
data in a template. This data is saved on Sheet 1 of my workbook. I
created a 'save' button on my template that works just fine until the
user is asked if they want to replace a current copy of the file. If
the user clicks 'no', then I get one of two responses: 1) it saves my
file as 'FALSE' or it generates a 1004 run time error.

Here is the code for my save button:

Private Sub SaveButton_Click()
fName = Application.GetSaveAsFilename( _
fileFilter:="Excel Files (*.xls), *.xls, Text Files (*.txt),
*.txt, All Files (*.*), *.*")
ActiveWorkbook.SaveAs Filename:=fName

End Sub

Any suggestions?