Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
Macro for Listing Files in a Folder
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. |
#2
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
Macro for Listing Files in a Folder
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 Word MVP web site http://word.mvps.org DebPgh wrote: 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. |
#3
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
Macro for Listing Files in a Folder
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 "Graham Mayor" wrote: 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 Word MVP web site http://word.mvps.org DebPgh wrote: 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. |
#4
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
Macro for Listing Files in a Folder
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 Word MVP web site http://word.mvps.org DebPgh wrote: 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 "Graham Mayor" wrote: 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 Word MVP web site http://word.mvps.org DebPgh wrote: 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. |
#5
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
Macro for Listing Files in a Folder
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 Mayor wrote: 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. DebPgh wrote: 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 "Graham Mayor" wrote: 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 Word MVP web site http://word.mvps.org DebPgh wrote: 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. -- Greg Maxey - Word MVP My web site http://gregmaxey.mvps.org Word MVP web site http://word.mvps.org McCain/Palin '08 !!! |
#6
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
Macro for Listing Files in a Folder
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 Word MVP web site http://word.mvps.org Greg Maxey wrote: 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 Mayor wrote: 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. DebPgh wrote: 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 "Graham Mayor" wrote: 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 Word MVP web site http://word.mvps.org DebPgh wrote: 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. |
#7
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
Macro for Listing Files in a Folder
Odd. I downloaded the code as posted and ran it in both Word2003 and
Word2007 without issues. Graham Mayor wrote: It falsl over at Set oFld = fso.GetFolder(pFolder) even with Microsoft ScriptingRuntime. I have a feeling I am missing something here Greg Maxey wrote: 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 Mayor wrote: 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. DebPgh wrote: 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 "Graham Mayor" wrote: 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 Word MVP web site http://word.mvps.org DebPgh wrote: 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. -- Greg Maxey - Word MVP My web site http://gregmaxey.mvps.org Word MVP web site http://word.mvps.org McCain/Palin '08 !!! |
Reply |
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Listing of word files in a folder | Microsoft Word Help | |||
Have a lot of files in Word. How can a paste a listing of these? | Microsoft Word Help | |||
how do I print a file listing within a folder? | Microsoft Word Help | |||
listing files in numeric order | Microsoft Word Help | |||
listing of files ot spaced evenly | Microsoft Word Help |