Pages

▼

Saturday, May 22, 2010

How to iterate through all Subdirectories till the last directory in VBA

List all Level SubDirectories using VBA

The following code lists all the directories under c:\Temp

Function GetSubDir(ByVal sDir)

    Dim oFS As New FileSystemObject
    Dim oDir
    
    Set oDir = oFS.GetFolder(sDir)
    For Each oSub In oDir.SubFolders
        MsgBox oSub.Path
        GetSubDir oSub.Path
    Next oSub
End Function


You can call the function like shown below

GetSubDir "C:\Temp\"


The code uses FileSystemObject from Microsoft Scripting RunTime. You need to add reference to this library (see figure below)



See also VBA Dir Function to Get Sub Directories

4 comments:

  1. Hi Shasur

    I need your help on this

    I need one macro by which i can take data from text boxes in slides(to excel or notepad or in first slide)

    for example : i show a engine pic in one slide and call out its parts in text boxes. I need to make a list of all part which i included in Text box..

    Please please help me on this

    ReplyDelete
  2. Hi Rahul

    Check if this post (http://vbadud.blogspot.com/2010/05/how-to-extract-textbox-contents-from.html) helps you

    Cheers
    Shasur

    ReplyDelete
  3. Anonymous4:20 AM

    this is not working it says
    sub or function not defined oSub

    ReplyDelete
    Replies
    1. Hi . I believe you have set the Option Explicit. This is good

      Please declare Dim oSub. It should work for you

      Delete