Insert into a document the names of all files in a selected folder

Article contributed by Bill Coan

Sub InsertNamesOfFilesInAFolder()

Dim MyPath As String
Dim MyName As String

'let user select a path
With Dialogs(wdDialogCopyFile)
    If .Display() <> -1 Then Exit Sub
    MyPath = .Directory
End With

'strip quotation marks from path

If Len(MyPath) = 0 Then Exit Sub

If Asc(MyPath) = 34 Then
    MyPath = Mid$(MyPath, 2, Len(MyPath) - 2)
End If

'get files from the selected path
'and insert them into the doc

MyName = Dir$(MyPath & "*.*")
Do While MyName <> ""
    Selection.InsertAfter MyName & vbCr
    MyName = Dir
Loop

'collapse the selection
Selection.Collapse wdCollapseEnd

End Sub