Saving Undeletable Files in VBA

wraa

New Member
Joined
Aug 23, 2010
Messages
13
Hi All,

I have created a macro that, in the end, saves the file using the following code:

PHP:
ActiveWorkbook.SaveAs Filename:=oneback & "\" & deptname & "_QT Report_" _
        & reportdateQ & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
This code usually works for saving the file, but we recently had a requirement to make the resulting files undeletable by non-administrators. However, if the administrator denies permissions for the network folder that the files reside in, the Save As code above fails. It looks like the data becomes trapped in a temp file that cannot be deleted under low permission settings. The excel file does save, but it has a size of 0 bytes.

Is there a way to either:

  • Configure the Save As code so that it succeeds with minimal permissions?
  • Automatically (with VBA) set permissions on each resulting file after it is saved so that the file cannot be deleted?
  • Set the security permissions such that the macro can successfully save the file and the resulting file cannot be deleted by non-administrators.
Non-administrators will be running the macro to create/save the files, but should not have permission to delete the resulting file.

Thanks!

Will
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hi Will,
You can try to save workbook on local drive, then copy it to the shared folder by the aid of FileCopy statement or by using of Shell function.
Alternatively with all permissions you can set ReadOnly file attribute using SetAttr statement, but it's not as strong as network permissions.
Regards
 
Upvote 0
Thanks a bunch for your help! However, I am still running into problems.

In my code, I am now initially saving the file one folder up in the network, where permissions aren't as strict. This part seems to work. When I attempt to move that file into the secured folder, the code fails (it also fails when testing the code with two unsecured folders). The error that I get is Path/File Access error.

Do you know what is wrong with my code? I also tried similar code with FileCopy, and this did not work either.

Code:
 oneback = Left(ThisWorkbook.Path, InStrRev(ThisWorkbook.Path, "\") - 1)
    twoback = Left(Left(ThisWorkbook.Path, InStrRev(ThisWorkbook.Path, "\") - 1), _
    InStrRev(Left(ThisWorkbook.Path, InStrRev(ThisWorkbook.Path, "\") - 1), "\") - 1)
    ChDir ThisWorkbook.Path

    Application.DisplayAlerts = False

    ActiveWorkbook.SaveAs Filename:=twoback & "\" & examplefilename & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        
    Application.DisplayAlerts = True

    Name twoback & "\" & examplefilename & ".xlsx" As oneback & "\" & examplefilename
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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