Saturday, October 6, 2012

VBA List all directories - Word


Sub List_All_Directories()

'______________________________________
'
'This VBA macro lists all directories, for example C:\
'This quick macro works in Word
'______________________________________
'

MyPath = "C:\"
MyName = Dir(MyPath, vbDirectory)

Do While MyName <> ""

    If MyName <> "." And MyName <> ".." Then

        If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
         
            With Selection
            WholeName = UCase(MyPath + MyName)
            .TypeText Text: = WholeName
               'if you also want the date and time, add this:
               ' & vbTab & FileDateTime(WholeName) & vbNewLine
            .InsertParagraphAfter
            .Collapse direction:=wdCollapseEnd
            Counter = Counter + 1
            End With

        End If

    End If
 
    MyName = Dir

Loop

End Sub

No comments:

Post a Comment

You may comment or show me other VBA tricks, but don't rest assured I'll always reply because I only have 24 hours in a day's hard work, and only a few minutes a week to update this blog... I'll try my best though...