Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default Accessing Excel files within multiple levels of a directory

Let's say I have a directory structure like this:

FOLDER1
FOLDER 1A
FOLDER 1A1
FOLDER 1A2
FOLDER 1B
FOLDER 1B1
FOLDER 1B2

I want to be able to select a high level folder and then open all XLS
workbooks that are in every folder below it. I'm not even sure how to begin
with this.

Also, I think I've seen somewhere that there's a way to allow the user to
CANCEL during execution of a macro. How do I do that?

Thanks in advance,
Barb Reinhardt
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Accessing Excel files within multiple levels of a directory

You kinda have two questions here so lets start with the first. Here is some
code to look through folders and subfolders. Place this code in a standard
code module in a new workbook. (The code is compiled from a bunch of other
posts from Bob Phillips and others if I recall correctly but any errors would
undoubtedly be my own.)

Option Compare Text

Sub test()
Call ListFiles("H:\", Sheet2.Range("A2"), "xls", True)
End Sub

Public Sub ListFiles(ByVal strPath As String, _
ByVal rngDestination As Range, Optional ByVal strFileType As String = "*", _
Optional ByVal blnSubDirectories As Boolean = False)
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim strName As String

'Specify the file to look for...
strName = "*." & strFileType
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strPath)

For Each objFile In objFolder.Files
If objFile.Name Like strName Then
rngDestination.Value = objFile.Path
rngDestination.Offset(0, 1).Value = objFile.DateLastAccessed
Set rngDestination = rngDestination.Offset(1, 0)
End If
Next 'objFile
Set objFile = Nothing

'Call recursive function
If blnSubDirectories = True Then _
DoTheSubFolders objFolder.SubFolders, rngDestination, strName

Set objFSO = Nothing
Set objFolder = Nothing
End Sub


Function DoTheSubFolders(ByRef objFolders As Object, _
ByRef rng As Range, ByRef strTitle As String)
Dim scrFolder As Object
Dim scrFile As Object
Dim lngCnt As Long

On Error GoTo ErrorHandler
For Each scrFolder In objFolders
For Each scrFile In scrFolder.Files
If scrFile.Name Like strTitle Then
rng.Value = scrFile.Path
rng.Offset(0, 1).Value = scrFile.DateLastAccessed
Set rng = rng.Offset(1, 0)
End If
Next 'scrFile

'If there are more sub folders then go back and run function again.
If scrFolder.SubFolders.Count 0 Then
DoTheSubFolders scrFolder.SubFolders, rng, strTitle
End If
ErrorHandler:
Next 'scrFolder

Set scrFile = Nothing
Set scrFolder = Nothing
End Function


Question 2 - How to interupt code... you can try something similar to this...

Dim lng As Long
Dim counter As Long
On Error GoTo ErrorHandler
Application.EnableCancelKey = xlErrorHandler

For lng = 1 To 100000000
counter = counter + 2
Next lng
ErrorHandler:
If Err = 18 Then
If MsgBox("Do you want to stop?", vbYesNo, "Quit?") = vbYes Then
MsgBox counter
Exit Sub
Else
Resume
End If
Else
MsgBox counter
End If

--
HTH...

Jim Thomlinson


"Barb Reinhardt" wrote:

Let's say I have a directory structure like this:

FOLDER1
FOLDER 1A
FOLDER 1A1
FOLDER 1A2
FOLDER 1B
FOLDER 1B1
FOLDER 1B2

I want to be able to select a high level folder and then open all XLS
workbooks that are in every folder below it. I'm not even sure how to begin
with this.

Also, I think I've seen somewhere that there's a way to allow the user to
CANCEL during execution of a macro. How do I do that?

Thanks in advance,
Barb Reinhardt

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default Accessing Excel files within multiple levels of a directory

You'll want to write a function that takes a directory name, puts
together a list of all directories in it, calls itself for each of
those directories, then opens all the files in that directory. You'll
end up with all files in folder 1A1 opened first, then 1A2, then 1A,
then 1B1, then 1B2, then 1B, then 1.

Public Function OpenBigDir(DirName As String)
'Get array of directories under DirName
'If array contains more than zero directories
For each Dir in DirArray
OpenBigDir(Dir)
Next Dir
'Get array of files.
'If array contains more than zero files
For each File in Filearray
Open File
Next file
End Function

Let me know if you need more help than that.

On May 15, 1:02 pm, Barb Reinhardt
wrote:
Let's say I have a directory structure like this:

FOLDER1
FOLDER 1A
FOLDER 1A1
FOLDER 1A2
FOLDER 1B
FOLDER 1B1
FOLDER 1B2

I want to be able to select a high level folder and then open all XLS
workbooks that are in every folder below it. I'm not even sure how to begin
with this.

Also, I think I've seen somewhere that there's a way to allow the user to
CANCEL during execution of a macro. How do I do that?

Thanks in advance,
Barb Reinhardt



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Accessing Excel files within multiple levels of a directory

You could use application.filesearch. But that goes away in xl2007--and it's
kind of flakey in earlier versions.

Ron de Bruin has converted many of his sample macros to use FSO
(FileSystemObject). It seems to work fine for him (and others <bg).

http://www.rondebruin.nl/fso.htm

Barb Reinhardt wrote:

Let's say I have a directory structure like this:

FOLDER1
FOLDER 1A
FOLDER 1A1
FOLDER 1A2
FOLDER 1B
FOLDER 1B1
FOLDER 1B2

I want to be able to select a high level folder and then open all XLS
workbooks that are in every folder below it. I'm not even sure how to begin
with this.

Also, I think I've seen somewhere that there's a way to allow the user to
CANCEL during execution of a macro. How do I do that?

Thanks in advance,
Barb Reinhardt


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default Accessing Excel files within multiple levels of a directory

Thanks. It's going to take me a bit to digest this.

"Jim Thomlinson" wrote:

You kinda have two questions here so lets start with the first. Here is some
code to look through folders and subfolders. Place this code in a standard
code module in a new workbook. (The code is compiled from a bunch of other
posts from Bob Phillips and others if I recall correctly but any errors would
undoubtedly be my own.)

Option Compare Text

Sub test()
Call ListFiles("H:\", Sheet2.Range("A2"), "xls", True)
End Sub

Public Sub ListFiles(ByVal strPath As String, _
ByVal rngDestination As Range, Optional ByVal strFileType As String = "*", _
Optional ByVal blnSubDirectories As Boolean = False)
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim strName As String

'Specify the file to look for...
strName = "*." & strFileType
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strPath)

For Each objFile In objFolder.Files
If objFile.Name Like strName Then
rngDestination.Value = objFile.Path
rngDestination.Offset(0, 1).Value = objFile.DateLastAccessed
Set rngDestination = rngDestination.Offset(1, 0)
End If
Next 'objFile
Set objFile = Nothing

'Call recursive function
If blnSubDirectories = True Then _
DoTheSubFolders objFolder.SubFolders, rngDestination, strName

Set objFSO = Nothing
Set objFolder = Nothing
End Sub


Function DoTheSubFolders(ByRef objFolders As Object, _
ByRef rng As Range, ByRef strTitle As String)
Dim scrFolder As Object
Dim scrFile As Object
Dim lngCnt As Long

On Error GoTo ErrorHandler
For Each scrFolder In objFolders
For Each scrFile In scrFolder.Files
If scrFile.Name Like strTitle Then
rng.Value = scrFile.Path
rng.Offset(0, 1).Value = scrFile.DateLastAccessed
Set rng = rng.Offset(1, 0)
End If
Next 'scrFile

'If there are more sub folders then go back and run function again.
If scrFolder.SubFolders.Count 0 Then
DoTheSubFolders scrFolder.SubFolders, rng, strTitle
End If
ErrorHandler:
Next 'scrFolder

Set scrFile = Nothing
Set scrFolder = Nothing
End Function


Question 2 - How to interupt code... you can try something similar to this...

Dim lng As Long
Dim counter As Long
On Error GoTo ErrorHandler
Application.EnableCancelKey = xlErrorHandler

For lng = 1 To 100000000
counter = counter + 2
Next lng
ErrorHandler:
If Err = 18 Then
If MsgBox("Do you want to stop?", vbYesNo, "Quit?") = vbYes Then
MsgBox counter
Exit Sub
Else
Resume
End If
Else
MsgBox counter
End If

--
HTH...

Jim Thomlinson


"Barb Reinhardt" wrote:

Let's say I have a directory structure like this:

FOLDER1
FOLDER 1A
FOLDER 1A1
FOLDER 1A2
FOLDER 1B
FOLDER 1B1
FOLDER 1B2

I want to be able to select a high level folder and then open all XLS
workbooks that are in every folder below it. I'm not even sure how to begin
with this.

Also, I think I've seen somewhere that there's a way to allow the user to
CANCEL during execution of a macro. How do I do that?

Thanks in advance,
Barb Reinhardt

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing local workstation's TEMP directory Frederick Chow Excel Programming 5 March 20th 06 05:04 PM
Ascertaining the directory name (2 levels up) Paul Martin Excel Programming 3 September 7th 05 02:12 AM
How do I get Multiple subtotal levels in EXCEL? Minch Lewis Excel Discussion (Misc queries) 2 February 26th 05 06:04 PM
accessing a workbook cell in a different directory meefongh Excel Programming 1 October 18th 04 04:26 AM
Check if directory empty OR no of files in directory. Michael Beckinsale Excel Programming 2 December 4th 03 10:12 PM


All times are GMT +1. The time now is 06:07 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"