VBA -- Don't allow user to change file name on saveas

winglessbuzzard

New Member
Joined
Jan 12, 2009
Messages
29
I have a large group of files and people using the files seem fond of renaming the files and changing the extension (.xlsm --> .xlsb..). I want to keep this from happening.

The users should be able to move the files wherever they want (other directories..etc), but not change the file name while in excel (I know they could still right-click the file and rename it from the file explorer... I'll leave that issue alone for now.)

my bad attempt at this:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim mywb As String

mywb = ThisWorkbook.Name

ThisWorkbook.SaveAs mywb

End Sub

... the above code doesn't quite get to what I'm trying... it saves the file as soon as the saveas dialog box opens, lets you rename the file and save it again anywhere with any new name/extension.

Just to be clear, all I'm trying to do is force the user to keep the original file name and extension while also allowing them to save the file in a different directory.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
just forcing the user to keep the file extension would be nice also... this code works:

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
     
    Dim varWorkbookName As String
    Dim FileFormatValue As Integer
     
    On Error GoTo Quit
    Application.EnableEvents = False
     
    If SaveAsUI = True Then
        varWorkbookName = Application.GetSaveAsFilename( _
        fileFilter:="Excel Macro Enabled Workbook (*.xlsm), *.xlsm")
        Cancel = True
         
        If varWorkbookName <> "False" Then
            Select Case LCase(Right(varWorkbookName, Len(varWorkbookName) - InStrRev(varWorkbookName, ".", , 1)))
            Case "xlsm": FileFormatValue = 52
            End Select
             
            ActiveWorkbook.SaveAs varWorkbookName
        End If
    End If
     
Quit:
     
    If Err.Number > 0 Then
        If Err.Number <> 1004 Then
             
            MsgBox "Error: " & Err.Number & Err.Description & vbCrLf & vbCrLf & vbCrLf & _
            "Title", vbCritical
             
        End If
    End If
     
    Application.EnableEvents = True
     
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,853
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top