Save As Restrictions

jrisebo

Active Member
Joined
Mar 15, 2011
Messages
310
Office Version
  1. 365
Platform
  1. Windows
We keep our files in a shared network folder. Users can open the spreadsheet, and then should save as to a separate location. Our IT department wont restrict the folder to read only, so anyone can overwrite the files inside it. I want to eliminate one way to do that by having code in the file not allowing the spreadsheet be saved in the folder. Anyway to do that?
 
OK, I will ask one more time. Please post your version of my code that you are using, in its entirety.
The behavior doesn't seem quite right to me, so I want to make sure that you have everything in the correct place in the code.
I already did once. You copied some of the code.

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

Dim fname As String
Dim myDir As String

' Specify the folder path they CANNOT save to
myDir = "C:\Excel Files\"

' Get file save name
fname = Application.GetSaveAsFilename

' Check to see if they are trying to save to restricted directory
MsgBox myDir & vbCrLf & Left(fname, Len(myDir))
If UCase(Left(fname, Len(myDir))) = UCase(myDir) Then
Cancel = True
MsgBox "You cannot save file to " & myDir & " directory.", vbOKOnly, "ERROR!"
Else
MsgBox "File Saved"
End If
 
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
I already did once. You copied some of the code.
I had asked for it after I asked you to add the MsgBox line, because it is not making sense that the second part of the Message Box is returning "False". It should be returning some values.
I wanted to make sure that you had the MsgBox in the correct place, which it appears that you do.

I cannot seem to reproduce the behavior you are describing. The code works for me.
Can you do a screen print of the save button you are clicking? I want to make sure that we are doing the same thing.
 
Upvote 0
I had asked for it after I asked you to add the MsgBox line, because it is not making sense that the second part of the Message Box is returning "False". It should be returning some values.
I wanted to make sure that you had the MsgBox in the correct place, which it appears that you do.

I cannot seem to reproduce the behavior you are describing. The code works for me.
Can you do a screen print of the save button you are clicking? I want to make sure that we are doing the same thing.
Here is the button I press:
Screen 6.PNG


Regardless, If I press that or do File:Save it pops up this SaveAs dialog box:
Screen 1.PNG


Which makes sense, as we dont want it to save, we want it to Save As. But this is not the normal dialog box (you cant pick what type of excel File)
 
Upvote 0
OK, this isn't quite a simple as I thought, as I think we need to account for both the "Save" and "SaveAs" options.
Let me play around with it and see what I can come up with.
 
Upvote 0
OK, this isn't quite a simple as I thought, as I think we need to account for both the "Save" and "SaveAs" options.
Let me play around with it and see what I can come up with.
Thanks. Yea, if they hit either one, Its desired that the file not be able to be saved in that directory.
 
Upvote 0
One other thought. I am not sure if this will satisfy your need, but what about making the file itself read-only?
They will still be able to them save copies to that network directory, but they won't be able to overwrite the ORIGINAL file?
Would something like that work?
 
Upvote 0
OK, with a little help with some friends, and borrowing some logic from this link here: How to force users to save as a macro enabled workbook?, I think I found something that works. It cancels the original save and creates its own prompt, where we can always capture the file folder.

See how it works for you:
VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    Dim xFileName As String
    Dim myDir As String

'   Specify the folder path they CANNOT save to
    myDir = "C:\Excel Files\"
    
'   Cancel original save and prompt for save
    Cancel = True
    xFileName = Application.GetSaveAsFilename(, "Excel Macro-Enabled Workbook (*.xlsm), *.xlsm", , "Save As xlsm file")
    
'   Check to see if file name is restricted folder
    If UCase(Left(xFileName, Len(myDir))) = UCase(myDir) Then
        MsgBox "You cannot save file to " & myDir & " directory.", vbOKOnly, "TRY AGAIN!"
        Cancel = True
        Exit Sub
    End If
        
'   Save file if save was not cancelled
    If xFileName <> "False" Then
        Application.EnableEvents = False
        ActiveWorkbook.SaveAs Filename:=xFileName, FileFormat:=xlOpenXMLWorkbookMacroEnabled
        Application.EnableEvents = True
    Else
        MsgBox "Action Cancelled"
        Cancel = True
        Exit Sub
    End If
    
End Sub
 
Upvote 0
Solution
OK, with a little help with some friends, and borrowing some logic from this link here: How to force users to save as a macro enabled workbook?, I think I found something that works. It cancels the original save and creates its own prompt, where we can always capture the file folder.

See how it works for you:
VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    Dim xFileName As String
    Dim myDir As String

'   Specify the folder path they CANNOT save to
    myDir = "C:\Excel Files\"
   
'   Cancel original save and prompt for save
    Cancel = True
    xFileName = Application.GetSaveAsFilename(, "Excel Macro-Enabled Workbook (*.xlsm), *.xlsm", , "Save As xlsm file")
   
'   Check to see if file name is restricted folder
    If UCase(Left(xFileName, Len(myDir))) = UCase(myDir) Then
        MsgBox "You cannot save file to " & myDir & " directory.", vbOKOnly, "TRY AGAIN!"
        Cancel = True
        Exit Sub
    End If
       
'   Save file if save was not cancelled
    If xFileName <> "False" Then
        Application.EnableEvents = False
        ActiveWorkbook.SaveAs Filename:=xFileName, FileFormat:=xlOpenXMLWorkbookMacroEnabled
        Application.EnableEvents = True
    Else
        MsgBox "Action Cancelled"
        Cancel = True
        Exit Sub
    End If
   
End Sub
Thanks, will try and get to it later today and let you know how it works!
 
Upvote 0
Ok, this works great, just as asked for.

One issue though is we do have a macro button in the spreadsheet that also does a Save As and rename to another file path, based on a cell value we type in.

i.e. - If cell value is 1000, then it saves it to a separate folder location in folder 1000. If cell value is 2000, it does a save as to folder 2000, etc. It does this automatically without opening a dialog box, since the path is predetermined based on cell value.

Is there a way to modify that specific macro to ignore the code you provided if the macro button is pressed? Currently the code you provided interferes also with our macro button.

Thanks
 
Upvote 0
Please post the VBA code you have for that button.
 
Upvote 0

Forum statistics

Threads
1,213,565
Messages
6,114,337
Members
448,568
Latest member
Honeymonster123

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