FileSystemObject and hidden files

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

FileSystemObject and hidden files

Post by agibsonsw »

Hello. (Excel 2003)
I have a procedure which uses the FileSystemObject of the Windows Scripting Runtime to list subfolders.
It falls over with a Permission Denied error message. I assume the folder might be hidden or a system folder?
It loops with:
For Each SubFolder In OfFolder.SubFolders
How can I either ignore this subfolder or list it despite its attribute? Thanks, Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78957
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: FileSystemObject and hidden files

Post by HansV »

Try using an error handler:

Code: Select all

Sub MyCode()
  ...

  On Error GoTo ErrHandler

  For Each SubFolder In OfFolder.SubFolders
    ...
    ...
ContinueHere:
  Next SubFolder

  ....
  Exit Sub

ErrHandler:
  If Err = 70 Then
    ' Permission denied
    Resume ContinueHere
  Else
    MsgBox Err.Description, vbExclamation
  End If
End Sub
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: FileSystemObject and hidden files

Post by agibsonsw »

That's a solution, thank you. It's a shame I can't check the file attribute before the error is generated, but never mind. Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.