Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default SaveAs dialog box

I have a Template (xlt) that when the user clicks the save button (on the
excel menu bar), i'm running the following code so that it will save the new
filename in a specific desired format.

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim fname, pname
fname = Range("SiteCity")
fname = "Charter - " & fname
Application.GetSaveAsFilename (fname)
End Sub

However it is actually saving the file twice, once from my code, then
normally from Excel as when this sub ends it follows the normal save routine
(since the user clicked the save button).
Q1: Is there any way to stop the second routine from saving the file?
Q2: Is there a way to ensure the recommend path is also set the "My
Documents"?

thx,
Ron
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default SaveAs dialog box

Actually, it's only saving the file once.

Application.getsaveasfilename doesn't actually save the file. It just returns
the filename that the user chose. It's up to you to do the save yourself.

Maybe something like:

Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

Dim FName As String
Dim UserFNameChoice As Variant
Dim resp As Long

Cancel = True 'don't let excel save it

FName = Me.Worksheets("sheet1").Range("SiteCity").Value
FName = "Charter - " & FName & ".xls"

UserFNameChoice = Application.GetSaveAsFilename(InitialFileName:=FNa me, _
filefilter:="Excel Files, *.xls")

If UserFNameChoice = False Then
'user hit cancel, do nothing
Else
resp = vbYes
'check to see if then name already exists
If Dir(UserFNameChoice) < "" Then
resp = MsgBox(Prompt:="Overwrite existing file?", Buttons:=vbYesNo)
If resp = vbNo Then
MsgBox "Try later with a different name"
End If
End If

If resp = vbYes Then
'stop the "are you sure you want to overwrite" message
Application.DisplayAlerts = False
'stop the _beforesave event from firing
Application.EnableEvents = False
On Error Resume Next
Me.SaveAs Filename:=UserFNameChoice, FileFormat:=xlWorkbookNormal
If Err.Number < 0 Then
MsgBox "An error occurred:" & vbLf _
& Err.Number & vbLf & Err.Description _
& vbLf & vbLf & "FILE NOT SAVED!"
Err.Clear
Else
MsgBox "Saved as: " & vbLf & UserFNameChoice
End If
On Error GoTo 0
Application.EnableEvents = True
Application.DisplayAlerts = True
End If
End If
End Sub


Ronio wrote:

I have a Template (xlt) that when the user clicks the save button (on the
excel menu bar), i'm running the following code so that it will save the new
filename in a specific desired format.

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim fname, pname
fname = Range("SiteCity")
fname = "Charter - " & fname
Application.GetSaveAsFilename (fname)
End Sub

However it is actually saving the file twice, once from my code, then
normally from Excel as when this sub ends it follows the normal save routine
(since the user clicked the save button).
Q1: Is there any way to stop the second routine from saving the file?
Q2: Is there a way to ensure the recommend path is also set the "My
Documents"?

thx,
Ron


--

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
SaveAs Dialog comes up twice??? chemicals Excel Programming 3 February 23rd 07 08:10 PM
SaveAs Dialog Box Noemi Excel Programming 1 November 8th 06 09:15 AM
SaveAs dialog Gary''s Student Excel Discussion (Misc queries) 2 February 23rd 06 03:11 PM
SaveAs Dialog box Daniel Bonallack Excel Programming 2 April 1st 05 09:25 AM
Saveas Dialog Box Tom Ogilvy Excel Programming 0 September 15th 04 04:57 PM


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

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

About Us

"It's about Microsoft Excel"