How to solve runtime error 5 in vba?

Discussion in 'AutoCAD' started by LLily, Feb 24, 2005.

  1. LLily

    LLily Guest

    I have two loops using Dir$ in vba. If exit the inside loop after an empty string is returned, then make another call to Dir$ with no arguments, the runtime error 5 occurs. Dose anybody know how to solve it?

    Thanks,
     
    LLily, Feb 24, 2005
    #1
  2. You might consider using the FileSystemObject in the Microsoft Scripting
    libary. It's already installed on your tartget machines and is much more
    object oriented as well as user friendly. Folder objects support
    subfolder and file collections making it much easier to loop through the
    contents of any given folder.
     
    Frank Oquendo, Feb 24, 2005
    #2
  3. LLily

    Tim Arheit Guest


    Dir$ cannot be called recursively. The following won't work:

    s = Dir$("some Direrectory")
    do while s <> ""
    ' This resets dir to look at the other directory
    s2 = dir$(some other directory)
    do while s2 <> ""
    s2 = dir$
    loop

    ' dir is done at this point so further calls will result in an
    error.
    s = dir$
    loop


    Easiest way to solve is to maintain a lists of directories we want to
    scan, and then scan only one directory at once.

    Dim cDir as Collection

    set cDir = New Collection


    cDir.Add "First directory to scan"

    Do While cDir.Count > 0
    ' Scan the next directory in the collection
    s = dir$(sdir.Item(1))
    sDir.Remove 1

    do while s <> ""
    s = dir$

    if s is a directory then
    cDir.Add "next directory to scan"
    Endif
    loop
    Loop


    -Tim
     
    Tim Arheit, Feb 24, 2005
    #3
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.