Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Check File Name before Save Event


I am distributing a file to clients and they are returning it to me.
want to send it out with the name "workbook1". When they return it t
me, I want to stop them from saving it under the same name. The cod
below works given that the file is already named "workbook1". Howeve
it also prevents the user from changing the name. Instead of using th
Name property as the test, I need to have the user attempt to save th
file and somehow pass the proposed name, that goes into the Save o
Save as filename box, to a variable and use this as the test tha
drives the Save Event.

Thanks


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

Dim FileName As String

FileName = ThisWorkbook.Name

If FileName = "Workbook1.xls" Then
MsgBox ("You cannot save this file using this name?" & vbCr & _
"It is recommended that you save it using your name as a
extension" & vbCr & _
"to this name(i.e. Workbook1JohnSmith)")

Cancel = True

End If

End Su

--
ExcelMonke
-----------------------------------------------------------------------
ExcelMonkey's Profile: http://www.excelforum.com/member.php...nfo&userid=522
View this thread: http://www.excelforum.com/showthread.php?threadid=26427

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 70
Default Check File Name before Save Event

The following BeforeSave procedure keeps asking until the user enters a valid file
name or cancels:

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

Dim FileName As String

If ThisWorkbook.Name = "Workbook1.xls" Then
Do
' returns path and name
FileName = Application.GetSaveAsFilename _
(Title:="Browse to a directory and enter a file name")

' check the name
If Right(FileName, 13) = "Workbook1.xls" Then
' don't use Workbook1.xls
MsgBox "That file name is not permitted." & vbCrLf & _
"Please select another file name.", vbExclamation, _
"Invalid File Name"
ElseIf Len(Dir(FileName)) 0 Then
' duplicate name
MsgBox "A file with that name already exists." & vbCrLf & _
"Please select another file name.", vbExclamation, _
"File Exists"
ElseIf FileName = "False" Then
Exit Do
Else
Exit Do
End If
Loop

Cancel = True

If FileName < "False" Then
Application.EnableEvents = False
ThisWorkbook.SaveAs FileName
Application.EnableEvents = False
End If
End If
End Sub


- Jon
-------
Jon Peltier, Microsoft Excel MVP
Peltier Technical Services
Tutorials and Custom Solutions
http://PeltierTech.com/
_______

ExcelMonkey wrote:

I am distributing a file to clients and they are returning it to me. I
want to send it out with the name "workbook1". When they return it to
me, I want to stop them from saving it under the same name. The code
below works given that the file is already named "workbook1". However
it also prevents the user from changing the name. Instead of using the
Name property as the test, I need to have the user attempt to save the
file and somehow pass the proposed name, that goes into the Save or
Save as filename box, to a variable and use this as the test that
drives the Save Event.

Thanks


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

Dim FileName As String

FileName = ThisWorkbook.Name

If FileName = "Workbook1.xls" Then
MsgBox ("You cannot save this file using this name?" & vbCr & _
"It is recommended that you save it using your name as an
extension" & vbCr & _
"to this name(i.e. Workbook1JohnSmith)")

Cancel = True

End If

End Sub



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
check folder event Mark[_17_] Excel Programming 6 June 18th 04 03:08 PM
Save File to Another Directory, but not change Users File Save location Mike Knight Excel Programming 1 May 28th 04 09:06 PM
Change Event.....Spell Check CLR Excel Programming 7 February 1st 04 05:05 PM
Help - Change Event triggered on File Save As Dee Veloper Excel Programming 4 October 29th 03 02:16 AM
After save event Bart[_3_] Excel Programming 2 July 22nd 03 12:07 PM


All times are GMT +1. The time now is 12:21 AM.

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"