![]() |
|
|
|
#1
|
|||
|
|||
|
I need to list all word files in a given folder and their subfolders.
I searched in this forum and a few others as well, but found no examples of it. I found some codes so complex that it always returns errors and with 1 or more sub / functions, I imagine that this does not need to be so complex. I saw this gmayor post in an another thread "See https://www.gmayor.com/document_batch_processes.htm which will handle the files and folders.", but it's a "add-in" and I need just a single SUB to adapt to my needs. I need a single SUB that does this "simple" task.Thanks |
|
#2
|
|||
|
|||
|
I think this is impossible without at least one sub and one function:
I've solved the problem with a solution based in a post elsewhere and some adaptations: Any improvement suggestions? (it will be perfect if it's only a SUB without public or functions) Thanks Code:
Public Arr() As String
Public Counter As Long
Sub LoopThroughFilePaths()
Dim MyArr, i As Long, x As Integer, strPath As String, sFile As String, sFileList(), oDoc As Document
strPath = "d:\temp\"
MyArr = GetSubFolders(strPath)
MyArr(0) = strPath
For x = 0 To UBound(MyArr)
sFile = Dir$(MyArr(x) & IIf(Right(MyArr(x), 1) <> "\", "\", "") & "*.*")
Do Until sFile = ""
i = i + 1
ReDim Preserve sFileList(i)
sFileList(i) = MyArr(x) & IIf(Right(MyArr(x), 1) <> "\", "\", "") & sFile
sFile = Dir$
Loop
Next x
For x = 1 To UBound(sFileList)
Set oDoc = Word.Documents.Open(sFileList(x), Visible:=False)
Debug.Print oDoc.Name ' I will insert here what I want to do with the docs
oDoc.Close (True)
Next x
Counter = 0
End Sub
Function GetSubFolders(RootPath As String)
Dim fso As Object, fld As Object, sf As Object, MyArr
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(RootPath)
For Each sf In fld.SUBFOLDERS
ReDim Preserve Arr(Counter + 1)
Arr(Counter + 1) = sf.Path
Counter = Counter + 1
MyArr = GetSubFolders(sf.Path)
Next
GetSubFolders = Arr
Set sf = Nothing
Set fld = Nothing
Set fso = Nothing
End Function
Last edited by eduzs; 06-09-2019 at 06:19 AM. |
|
#3
|
||||
|
||||
|
Quote:
Check your file manager to see if it supports 'flat' folder listings and scripting. If it's not a frequent job, then a manual approach is easy if your file manager has the appropriate functionality: 1 Click 'Flat View'; 2 Sort by extension; 3 Highlight all the DOC? files; 4 Copy File Names; 5 Paste wherever needed. |
|
#4
|
|||
|
|||
|
Thanks for replying, listing the files is not my main goal.
In fact with this code I will be able to do things with all documents in folder and subfolders, which is not the scope of this thread, so I did not post here the entire code. |
|
#5
|
||||
|
||||
|
Try:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim StrFolder As String, StrFileList As String
' Browse for the starting folder
StrFolder = GetTopFolder
If StrFolder = "" Then Exit Sub
StrFolder = StrFolder & "\*.doc"
If UBound(Split(CreateObject("wscript.shell").Exec("Cmd /c Dir """ & StrFolder & """ /B/S").StdOut.ReadAll, vbCrLf)) > 0 Then
StrFileList = CreateObject("wscript.shell").Exec("Cmd /c Dir """ & StrFolder & """ /B/S").StdOut.ReadAll
End If
Application.ScreenUpdating = True
MsgBox StrFileList
End Sub
Function GetTopFolder() As String
Dim oFolder As Object
GetTopFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetTopFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#6
|
|||
|
|||
|
Thanks! It's a another approach.
|
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| open word documents in folder and subfolders | shu | Word VBA | 1 | 07-30-2018 02:51 AM |
| Searching through folders/ subfolders and rename files if certain condition is met | mihnea96 | Excel | 1 | 05-15-2017 07:09 AM |
| Macro to change/convert/delete txt files in folder+subfolders | NoS | Word VBA | 4 | 03-03-2016 12:10 PM |
| VBA Word - Search Within Files Containing A String - Copy Files to New Folder | jc491 | Word VBA | 0 | 01-09-2016 12:00 PM |
Word Macro - change date in footer for all files in a folder
|
patidallas22 | Word VBA | 2 | 03-09-2012 08:14 AM |