Macro for Listing Files in a Folder

  • Thread starter Thread starter DebPgh
  • Start date Start date
D

DebPgh

Hi,

In the Microsoft Knowledge Base articles, there is a wonderful macro that
enables you to list all of the files in a particular folder (Article
ID:306248, WD2002: Sample Macro to List All Files in a Folder). It was
designed for Word 2002, but it also works in Word 2003. However, it doesn't
work in Word 2007. When run, the macro says there are no files in the folder
even though there are files.

Can anyone tell me what changes to make so that this macro will work? Or is
there another macro somewhere or some other process that will allow me to
list the files in a folder (along with the file size and last modified date
and time)?

Thanks in advance for your help.
 
The routine used to list the folder content does not work in 2007 as you
have found. Rather than rewrite the macro to use an alternative routine, use
the free and rather more powerful PrintFolders utility that you can download
from my web site instead.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
Graham,

Thanks for your suggestion. The two websites you referred me to look like
they have a lot of good info.

The problem is that I need this capability for my office computer. Our
company won't let us download any programs from the Internet. Using a macro
was a good way to get around this problem.

Deb
 
Perhaps you could ask your IT support to check it out?

In the meantime

Sub ListFolder()
Dim TargetDoc As Document
Dim fDialog As FileDialog
Dim sPath As String
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
.Title = "Select Folder to list and click OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User"
Exit Sub
End If
sPath = fDialog.SelectedItems.Item(1) & Chr(92)
End With
On Error Resume Next
Set TargetDoc = Documents.Add
Selection.TypeText "File Listing of the """ & sPath & """ Folder" & vbCr &
vbCr
sName = Dir$(sPath)
Do While sName <> ""
Selection.TypeText sName & vbCr
sName = Dir$
Loop
End Sub

will give you a filename listing for a selected folder.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
Graham,

I have no doubt that you answered the OP which I didn't read. You can get a
lot of additional information using the FileSystemObject (requires reference
to Microsoft Scripting Runtime):

Option Explicit
Dim arrFiles() As String
Dim i As Long
Sub ListFolder()
Dim TargetDoc As Document
Dim fDialog As FileDialog
Dim sPath As String
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
.Title = "Select Folder to list and click OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User"
Exit Sub
End If
sPath = fDialog.SelectedItems.Item(1) & Chr(92)
End With
arrFiles = GetFileNames(sPath)
On Error Resume Next
Set TargetDoc = Documents.Add
With TargetDoc.Range
.Font.Size = 8
.InsertAfter "File Listing of the """ & sPath & """ Folder" & vbCr & vbCr
For i = 0 To UBound(arrFiles, 2)
.InsertAfter "File name: " & arrFiles(0, i) & vbTab & "Size: " _
& arrFiles(1, i) & vbTab & "Type: " & arrFiles(2, i) _
& vbTab & "Last Modified: " & arrFiles(3, i) & vbCr & vbCr
Next i
End With
End Sub
Function GetFileNames(ByRef pFolder As String) As String()
Dim fso As New FileSystemObject
Dim oFld As Folder
Dim oFile As File
i = 0
If fso.FolderExists(pFolder) Then
Set oFld = fso.GetFolder(pFolder)
For Each oFile In oFld.Files
ReDim Preserve arrFiles(3, i)
arrFiles(0, i) = oFile.Name
arrFiles(1, i) = oFile.Size
arrFiles(2, i) = oFile.Type
arrFiles(3, i) = oFile.DateLastModified
i = i + 1
Next
GetFileNames = arrFiles
End If
End Function


Graham said:
Perhaps you could ask your IT support to check it out?

In the meantime

Sub ListFolder()
Dim TargetDoc As Document
Dim fDialog As FileDialog
Dim sPath As String
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
.Title = "Select Folder to list and click OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User"
Exit Sub
End If
sPath = fDialog.SelectedItems.Item(1) & Chr(92)
End With
On Error Resume Next
Set TargetDoc = Documents.Add
Selection.TypeText "File Listing of the """ & sPath & """ Folder" &
vbCr & vbCr
sName = Dir$(sPath)
Do While sName <> ""
Selection.TypeText sName & vbCr
sName = Dir$
Loop
End Sub

will give you a filename listing for a selected folder.

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 
It falsl over at
Set oFld = fso.GetFolder(pFolder)
even with Microsoft ScriptingRuntime. I have a feeling I am missing
something here ;)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
Odd. I downloaded the code as posted and ran it in both Word2003 and
Word2007 without issues.

Graham said:
It falsl over at
Set oFld = fso.GetFolder(pFolder)
even with Microsoft ScriptingRuntime. I have a feeling I am missing
something here ;)

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 

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

Back
Top