Dir is a rather limited function in the sense that you can have only
one Dir loop. Creating another Dir loop from within the outer loop
will cause an invalid procedure call. For example, the following code
is fine:
Sub OneLoop()
Dim FName As String
FName = Dir("C:\VBAModules\*.bas")
Do Until FName = vbNullString
Debug.Print FName
FName = Dir
Loop
End Sub
This has only one Dir loop. However, examine the following code:
Sub TwoLoops()
Dim FName As String
Dim N As Long
FName = Dir("C:\VBAModules\*.bas")
Do Until FName = vbNullString
N = N + 1
Debug.Print FName
If N = 10 Then
FName = Dir("C:\DVDs\*.*")
Do Until FName = vbNullString
Debug.Print FName
FName = Dir
Loop
End If
FName = Dir '<<<< BLOWS UP
Loop
End Sub
Here, you have the outer Dir loop looking at C:\VBAModules. At some
point (arbitrarily triggered when a counter = 10), the code enters an
inner Dir loop, looking at C:\DVDs. This inner loop resets all of
Dir's inner parameters, and when the inner loop exits and control
returns to the outer loop, which was originally looking at
C:\VBAModules, Dir is all screwed up internally and you get the
invalid proc call error. Dir does not pick up where it left off.
As a general rule, you should NEVER do anything with the Dir function
when you are using it to loop through a directory. For all but the
most simple tasks, I never use Dir. Instead, I use the
Scripting.FileSystemObject when allows you to run multiple seach loops
and provides much more information that Dir.
Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
[email on web site]