简体   繁体   中英

Using VBA in Excel to force save in directory

I have a huge Excel spreadsheet that I need to allow access to a large set of users so they can manipulate it for their customers, but I don't want them to be able to overwrite the original file (a variable easily set in Excel) or save their file outside the current folder - so I want to force them in a "saveas" mode, and force the file to be saved in that folder. Otherwise, they won't be able to save. I'm not much of a VBA person, and I've found a lot of examples that may work, but nothing seems to be exactly what I need or maybe I'm not smart enough to figure it out. I found this code, but I'm not sure it FORCES the issue. Help?

I've tried to manage this in GPOs but everything seems to give them access to download the folder and save in other places.

Sub ExampleToSaveWorkbookSet()

Dim wkb As Workbook
'Adding New Workbook
Set wkb = Workbooks.Add
'Saving the Workbook
wkb.SaveAs "C:\WorkbookName.xls"
'OR
'wkb.SaveAs Filename:="C:\WorkbookName1.xls"

End Sub

Expected output is the amended Excel file saved in the original directory with a different name, or not at all.

Here's a macro that runs on open and immediately saves as .xlsx to a user location you can specify. Unfortunately the original needs to be .xlsm to store a macro. This macro is to be located in the "ThisWorkbook" object. It will exit before making a copy when you open the workbook.

Private Sub Workbook_Open()
    Dim wb As Workbook
    Set wb = ActiveWorkbook
    vWbName = wb.Name
    vUserProf = Environ("USERPROFILE")
    vx = InStr(1, vUserProf, "Users\")
    If "<Use your own profileID>" = Mid(vUserProf, vx + 6) Then Exit Sub
    vDir = vUserProf & "\Downloads\"
    vWbName = Left(vWbName, Len(vWbName) - 5) & ".xlsx"
    wb.SaveAs vDir & vWbName, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    MsgBox "You are now using a copy of the original"
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM