VBA code for file access settings

gordsky

Well-known Member
Joined
Jun 2, 2016
Messages
556
Office Version
  1. 2016
Platform
  1. Windows
Hi All,

Not sure if his is even possible but looking for a vba code to replicate the following.......

I have a large number of files which had permissions set through "right click, properties, security. edit (etc)" so that only the individual and their line manager can access the file from explorer. Unfortunately the person who initiated this didn't seem to anticipate staff changes in the company. this means now the only way to make a change when someone leaves is to manually change each file. Is there a way to bulk change these through VBA.

I know how to loop through a folder and access files individually through VBA but wouldn't have a clue on how or if it is possible to change the permissions.
Any help would be appreciated.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
How about something like:

VBA Code:
    Dim myPermission As Office.Permission
    Set myPermission = ActiveWorkbook.Permission
'
' Remove all UserPermissions that have been added to the Permission collection and disable restrictions on the active document
    myPermission.RemoveAll
    Set myPermission = Nothing
 
Upvote 0
How about something like:

VBA Code:
    Dim myPermission As Office.Permission
    Set myPermission = ActiveWorkbook.Permission
'
' Remove all UserPermissions that have been added to the Permission collection and disable restrictions on the active document
    myPermission.RemoveAll
    Set myPermission = Nothing
JohnnyL thanks for the reply, much appreciated, can you expand on what the code does.? Does it remove permissions for everyone or does it reset the permissions to all have access?
 
Upvote 0
If I read it correctly, it will remove all previously added users from the Permissions 'list' and set the file to a state of as if it were never restricted in any way. In other words, everyone will have full access to it.
 
Upvote 0
If I read it correctly, it will remove all previously added users from the Permissions 'list' and set the file to a state of as if it were never restricted in any way. In other words, everyone will have full access to it.
Thanks for the explanation. Tried the code this morning on a test file but it didn't work. By that I mean it ran fine with no errors but did nothing to the permissions. They remained as they were before running. Any ideas?
 
Upvote 0
What happens when you run the following:

VBA Code:
Sub CheckUsers()
    Dim myPermission As Office.Permission, UsersPermission As Office.UserPermission
    Set myPermission = ActiveWorkbook.Permission
'
    myPermission.RemoveAll
'
    If myPermission.Enabled Then
        For Each UsersPermission In myPermission
            strIRMInfo = strIRMInfo & UsersPermission.UserId & vbCrLf & " - Permissions: " & UsersPermission.Permission & vbCrLf & _
                    " - Expiration Date: " & UsersPermission.ExpirationDate & vbCrLf
        Next
'
        MsgBox strIRMInfo, vbInformation + vbOKOnly, "IRM Information"
    Else
        MsgBox "This document is not restricted, Or you don't have authorization.", vbInformation + vbOKOnly, "IRM Information"
    End If
'
    Set UsersPermission = Nothing
    Set myPermission = Nothing
End Sub
 
Upvote 0
I have a large number of files which had permissions set through "right click, properties, security. edit (etc)" so that only the individual and their line manager can access the file from explorer. Unfortunately the person who initiated this didn't seem to anticipate staff changes in the company. this means now the only way to make a change when someone leaves is to manually change each file. Is there a way to bulk change these through VBA.
You can change file permissions using the DOS ICACLS command from a command window:

You don't really need VBA, but if you wish ICACLS commands can be run using the VBA Shell function or the WScript.Shell object.
 
Upvote 0
What happens when you run the following:

VBA Code:
Sub CheckUsers()
    Dim myPermission As Office.Permission, UsersPermission As Office.UserPermission
    Set myPermission = ActiveWorkbook.Permission
'
    myPermission.RemoveAll
'
    If myPermission.Enabled Then
        For Each UsersPermission In myPermission
            strIRMInfo = strIRMInfo & UsersPermission.UserId & vbCrLf & " - Permissions: " & UsersPermission.Permission & vbCrLf & _
                    " - Expiration Date: " & UsersPermission.ExpirationDate & vbCrLf
        Next
'
        MsgBox strIRMInfo, vbInformation + vbOKOnly, "IRM Information"
    Else
        MsgBox "This document is not restricted, Or you don't have authorization.", vbInformation + vbOKOnly, "IRM Information"
    End If
'
    Set UsersPermission = Nothing
    Set myPermission = Nothing
End Sub
It defaults to the not restricted / dont have authorization message
 
Upvote 0
Ok it sounds like you are not dealing with IRM, so that approch is not going to work.

The method @John_w suggested is probably what you need.
 
Upvote 0
Ok it sounds like you are not dealing with IRM, so that approch is not going to work.

The method @John_w suggested is probably what you need.
thanks for your time on this one. Ill give that a try but suspect I wont have sufficient access rights to be able to run something at commandline level
 
Upvote 0

Forum statistics

Threads
1,215,232
Messages
6,123,759
Members
449,120
Latest member
Aa2

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