View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Bob Kilmer[_2_] Bob Kilmer[_2_] is offline
external usenet poster
 
Posts: 71
Default How can I skip a (Sub)Folder in a Scan?

On what line of code does the error crop up? I am guessing you need to be
more surgical with your error handling. Catch the specific error in
procedure where it occurs, goto an error handler, clean up, then have the
code restart where it can try to get the *next* folder using Resume <label
in your error handler.

On Error Goto errhandler
get_folder:
'code gets next folder here
'more code
Exit Sub
errhandler:
'code to clean up, if nec.
Resume get_folder 'go try next.
Ens Sub

"SuperJas" wrote in message
...
Hi,

I've tried out Bob Phillip's clever code of creating a list of files

within a main folder (and drilling down into all subfolders), pasted below.

I am using this to create a list of files within a particular directory

(say, N:\Main Dir) to identify files which have been out-of-date and may be
archived for space.

Unfortunately, I've run into folders that have 'Permission Denied' (Error

# 70), tripping up the macro. How can I get the macro to skip to the next
folder when it hits this error? I have tried a simple "On Error Resume
Next", but it gives weird results (not all files listed, some file names
missing etc).

Thanks a million for your help!

SuperJas.

---------------------------------------------------------

Option Explicit

Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" _
(ByVal pidl As Long, _
ByVal pszPath As String) As Long

Private Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" _
(lpBrowseInfo As BROWSEINFO) As Long

Private Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type

Dim FSO As Object
Dim cnt As Long
Dim level As Long
Dim arFiles

Sub Folders()
Dim i As Long
Dim sFolder As String

Set FSO = CreateObject("Scripting.FileSystemObject")

arFiles = Array()
cnt = -1
level = 1

sFolder = GetFolder()
ReDim arFiles(1, 0)
If sFolder < "" Then
SelectFiles sFolder
Worksheets.Add.Name = "Files"
With ActiveSheet
For i = LBound(arFiles, 2) To UBound(arFiles, 2)
.Hyperlinks.Add Anchor:=.Cells(i + 1, arFiles(1, i)),

_
Address:=arFiles(0, i), _
TextToDisplay:=arFiles(0, i)
Next
.Columns("A:Z").EntireColumn.AutoFit
End With
End If

End Sub

'-----------------------------------------------------------------------
Sub SelectFiles(ByVal sPath)
'-----------------------------------------------------------------------
Dim fldr As Object
Dim Folder As Object
Dim file As Object
Dim Files As Object

Set Folder = FSO.GetFolder(sPath)

Set Files = Folder.Files
For Each file In Files
cnt = cnt + 1
ReDim Preserve arFiles(1, cnt)
arFiles(0, cnt) = Folder.path & "\" & file.Name
arFiles(1, cnt) = level
Next file

level = level + 1
For Each fldr In Folder.Subfolders
SelectFiles fldr.path
Next

End Sub


'-------------------------------------------------------------
Function GetFolder(Optional ByVal Name As String = "Select a folder.")
As String
'-------------------------------------------------------------
Dim bInfo As BROWSEINFO
Dim path As String
Dim oDialog As Long

bInfo.pidlRoot = 0& 'Root folder = Desktop

bInfo.lpszTitle = Name

bInfo.ulFlags = &H1 'Type of directory to

Return
oDialog = SHBrowseForFolder(bInfo) 'display the dialog

'Parse the result
path = Space$(512)

GetFolder = ""
If SHGetPathFromIDList(ByVal oDialog, ByVal path) Then
GetFolder = Left(path, InStr(path, Chr$(0)) - 1)
End If

End Function