Is there a way to prevent a file from being re-named.
Phil
Is there a way to prevent a file from being re-named.
Phil
[vba]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[/vba]
Peace of mind is found in some of the strangest places.
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
Hmmmm ,
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:
[VBA]
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
[/VBA]
Marcster.
Pretty good Marcster! Really need to get rid of the renamed book though (and can simplify)...[vba]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[/vba]
You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you
The major part of getting the right answer lies in asking the right question...
Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.
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
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..
Regards, Zack Barresse
Check out the KB! :|: BOARD TAGS: WHAT ARE THEY AND HOW DO I USE THEM
What is a Microsoft MVP? | Free Microsoft Courses | My Book on Excel Tables