create a document containing a list of all documents in a folder

C

Christine

I need to create a document that contains a listing of the contents of a folder. I have done this before but now can't find the document I saved the Macro in. I have found a posting (http://word.mvps.org/faqs/general/PrintDocList.htm) that tells me how to print this (and I can't make it work!) but I can't find one that tells me how to create a document listing the contents (like an index to the folder)

Any help you can provide will be appreciated
 
J

Jay Freedman

Christine said:
I need to create a document that contains a listing of the contents of a folder. I have done this before but now can't find the document I saved the Macro in. I have found a posting (http://word.mvps.org/faqs/general/PrintDocList.htm) that tells me how to print this (and I can't make it work!) but I can't find one that tells me how to create a document listing the contents (like an index to the folder).

Any help you can provide will be appreciated.
Hi, Christine,

Try this one. Using the Copy dialog to acquire the folder name is a
bit clunky, but telling you how to do anything better would take a
while unless you had some programming background.

Public Sub FolderListing()
Dim ListDoc As Document
Dim FolderName As String, FileName As String

With Dialogs(wdDialogCopyFile)
If .Display = -1 Then
FolderName = .directory
Else
Exit Sub
End If
End With

FolderName = Replace(FolderName, Chr(34), "")

Set ListDoc = Documents.Add
ListDoc.Range.Text = "Contents of " & FolderName & vbCr & vbCr

FileName = Dir(FolderName & "*.*")
Do While FileName <> ""
ListDoc.Range.InsertAfter FileName & vbCr
FileName = Dir()
Loop

Set ListDoc = Nothing
End Sub
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Top