Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-08-2019, 11:02 AM
eduzs eduzs is offline List all word files in folder and subfolders Windows 10 List all word files in folder and subfolders Office 2010 32bit
Expert
List all word files in folder and subfolders
 
Join Date: May 2017
Posts: 260
eduzs is on a distinguished road
Default List all word files in folder and subfolders

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
Reply With Quote
  #2  
Old 06-08-2019, 01:18 PM
eduzs eduzs is offline List all word files in folder and subfolders Windows 10 List all word files in folder and subfolders Office 2010 32bit
Expert
List all word files in folder and subfolders
 
Join Date: May 2017
Posts: 260
eduzs is on a distinguished road
Default

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.
Reply With Quote
  #3  
Old 06-08-2019, 05:21 PM
Lugh's Avatar
Lugh Lugh is offline List all word files in folder and subfolders Windows 10 List all word files in folder and subfolders Office 2016
Competent Performer
 
Join Date: May 2019
Location: USA
Posts: 137
Lugh is on a distinguished road
Default

Quote:
Originally Posted by eduzs View Post
I need to list all word files in a given folder and their subfolders.
If I needed something like this repeatedly, I'd look for a file system solution rather than a Word-based solution. It's a file problem, not Word, and a file-based solution would work for any kind of files.

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.
Reply With Quote
  #4  
Old 06-08-2019, 07:17 PM
eduzs eduzs is offline List all word files in folder and subfolders Windows 10 List all word files in folder and subfolders Office 2010 32bit
Expert
List all word files in folder and subfolders
 
Join Date: May 2017
Posts: 260
eduzs is on a distinguished road
Default

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.
Reply With Quote
  #5  
Old 06-08-2019, 10:47 PM
macropod's Avatar
macropod macropod is offline List all word files in folder and subfolders Windows 7 64bit List all word files in folder and subfolders Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #6  
Old 06-09-2019, 06:20 AM
eduzs eduzs is offline List all word files in folder and subfolders Windows 10 List all word files in folder and subfolders Office 2010 32bit
Expert
List all word files in folder and subfolders
 
Join Date: May 2017
Posts: 260
eduzs is on a distinguished road
Default

Thanks! It's a another approach.
Reply With Quote
Reply

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
List all word files in folder and subfolders Word Macro - change date in footer for all files in a folder patidallas22 Word VBA 2 03-09-2012 08:14 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:27 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft