PDA

View Full Version : Solved: File Name Protection



Philcjr
10-17-2005, 12:53 PM
Is there a way to prevent a file from being re-named.:think:

Phil

austenr
10-17-2005, 06:24 PM
Private Sub workbook_beforesave(ByVal SaveAsUI As Boolean, cancel As Boolean)
MsgBox ("Sorry you cannot save this workbook using this method")
SaveAsUI = False
cancel = True
End Sub

Philcjr
10-17-2005, 08:51 PM
Austenr,

Thanks for your reply, but what I had in mind was someone trying to change the file name through windows explorer, or right click & rename.

As I write this, I think that it is impossible to lock down a file name.

Anyone else with some thoughts?

Phil

Marcster
10-18-2005, 02:42 AM
Hmmmm :think:,

How about something along the lines of say,
When workbook gets opened it checks the name of the workbook
against a variable. If it's the same then ok, else a sub could
run which saves a copy of the file to the same location with the variable
as the workbook name. Then possibly saves the 'new' workbook and deletes
the 'old' one.
Maybe something on the lines of:

Private Sub Workbook_Open()
If ActiveWorkbook.Name = "Book1.xls" Then
MsgBox "This workbook name is correct"
Else
MsgBox "Wrong workbook name!"
Call renameFile
End If
End Sub
Sub renameFile()
ActiveWorkbook.SaveAs Filename:= _
"Book1.xls", FileFormat:=xlNormal, _
Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
CreateBackup:=False
End Sub


Marcster.

johnske
10-18-2005, 05:51 AM
Pretty good Marcster! :thumb Really need to get rid of the renamed book though (and can simplify)...Private Sub Workbook_Open()
Dim ThisBook As String
ThisBook = ActiveWorkbook.Name
If ThisBook <> "FancyButtons2.xls" Then
ActiveWorkbook.SaveAs Filename:="FancyButtons2.xls"
Kill ThisBook
End If
End Sub

Philcjr
10-18-2005, 06:10 AM
Marcus,
Nice one, very interesting approach you took.

Johnske,
Thanks for the code clean-up.

I will insert this code and see how it works. Thanks all for your time and effort.

Phil

Zack Barresse
10-18-2005, 10:09 AM
Just as a side note, you can't stop somebody from renaming the file via Windows. I believe if the users rights are set low enough then they may not have sufficient privelages to access/change this information. Even with workbook protection and/or encryption it will not stop somebody from right-clicking the file and choosing Rename..