Excel VBA: Check Folder Permissions

dhancy

Board Regular
Joined
Jul 10, 2013
Messages
123
Office Version
  1. 365
Platform
  1. Windows
I have a VBA script that cycles through subfolders looking for a specified file.

Often, there are subfolders to which I do not have access. My script ends with a "permission denied" error.

Is there something I can add to my code that will check permissions on a folder before attempt to access it? In the code snippet below. the script errors out on the "For" line.

Code:
Dim objFolder as Object
Dim objFile as Object

Set objFolder = objFSO.GetFolder(targetFolder)
For Each objFile In objFolder.Files
...
Next

Thanks!
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
It is possible to ignore the error. Do you need to know where it was triggered?

Code:
Sub ImportFiles()
Dim fso As New filesystemobject, objFolder As Object, fls As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFolder = fso.GetFolder("c:\pub\client\")
On Error Resume Next
For Each fls In objFolder.Files
    fso.MoveFile fls.path, "c:\pub\"
Next
Set fso = Nothing
If Err.Number <> 0 Then MsgBox "An error occurred."
Err.Clear
On Error GoTo 0
End Sub
 
Upvote 0
It is possible to ignore the error. Do you need to know where it was triggered?

Code:
Sub ImportFiles()
Dim fso As New filesystemobject, objFolder As Object, fls As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFolder = fso.GetFolder("c:\pub\client\")
On Error Resume Next
For Each fls In objFolder.Files
    fso.MoveFile fls.path, "c:\pub\"
Next
Set fso = Nothing
If Err.Number <> 0 Then MsgBox "An error occurred."
Err.Clear
On Error GoTo 0
End Sub

I know it's being triggered at the "FOR" statement. So I envision having a check right after the "Set objFolder" line.

I'm thinking I could put something like this:
Code:
if ObjFolder.permision <> 999 then ' or whatever the property/value is for this...
...
end if
 
Upvote 0
What I am saying is that it is not necessary to check permissions. Using the “On Error Resume Next” statement, the code will ignore the error and proceed to the next executable line.
Is this a suitable solution for you? You can test it by simply adding the statement on the beginning of the code.
I am also wondering if a folder or a file causes the error.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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