View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default Workbook_BeforeSave

Your first problem is the GetSaveAsfilename does not save the file or
otherwise affect what the filename will be if it is saved. It mearly allows
the user to select a filename and return the fully qualified filename (path
and filename) as a string. You then have to save the file with your code.
The easiest is to turn off events and overtly do the save with your desired
filename to the selected path. I have used the GetSaveAsfilename approach,
but this could allow the user to select a different name which is discarded
(and could be misleading). You could put up the folder browser dialog
although this takes much more code. (see reference at bottom of post).

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
Cancel As Boolean)

Dim i as long, a as long, j as Long
Dim sStr as String, sStr1 as String

On Error goto ErrHandler
a = MsgBox("Are sure you want to save this workbook?", vbYesNo)

' Cancel the save action initiated by the user
Cancel = True
If a = vbNo Then
Else
' specify a worksheet - don't depend on which
' is visible unless it is
' a one sheet workbook
sStr = "TCR" & worksheets(1).Cells(20,29).Value & ".xls"
fileSaveName = Application.GetSaveAsFilename( _
sStr, fileFilter:="Excel Files (*.xls),*.xls")
Application.EnableEvents = False
if fileSaveName < "False" then
i = len(FileSaveName)
for j = i to 1 step -1
if mid(FileSaveName,j,1) = "\" then
sStr1 = Left(FileSaveName,j)
exit for
end if
Next
Thisworkbook.SaveAs sStr1 & sStr
else
Thisworkbook.SaveAs ThisWorkbook.Path & "\" & sStr
end if

End If
ErrHandler:
Application.EnableEvents = True
End sub



for the folder browser dialog:
John Walkenbach:
Tip #29
http://www.j-walk.com/ss/excel/tips/tip29.htm


--
Regards,
Tom Ogilvy



Bill Oertell wrote in message
...
I'm having trouble getting this code to work. The dialog box come up and
you can navigate to the directory you want, but the file still saves to

the
original file name in the original directory.

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

a = MsgBox("Are sure you want to save this workbook?", vbYesNo)

If a = vbNo Then
Cancel = True
Else
SaveAsUI = True
fileSaveName = Application.GetSaveAsFilename("TCR" & Cells(20, 29)

&
".xls", fileFilter:="Excel Files (*.xls),*.xls")
End If

End sub

What I want to do is force the user to save the file as TCR plus whatever

is
in cell AC20, ie TCRSomeone.xls, but I'd like them to be able to save it

in
the directory they choose.