Consulting

Results 1 to 7 of 7

Thread: Solved: File Name Protection

  1. #1
    VBAX Tutor Philcjr's Avatar
    Joined
    Jul 2005
    Location
    Bedminster, NJ
    Posts
    208
    Location

    Solved: File Name Protection

    Is there a way to prevent a file from being re-named.

    Phil

  2. #2
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    [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.

  3. #3
    VBAX Tutor Philcjr's Avatar
    Joined
    Jul 2005
    Location
    Bedminster, NJ
    Posts
    208
    Location
    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

  4. #4
    VBAX Mentor Marcster's Avatar
    Joined
    Jun 2005
    Posts
    434
    Location
    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.

  5. #5
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    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.

  6. #6
    VBAX Tutor Philcjr's Avatar
    Joined
    Jul 2005
    Location
    Bedminster, NJ
    Posts
    208
    Location
    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

  7. #7
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    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..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •