Dr. Demento
Well-known Member
- Joined
- Nov 2, 2010
- Messages
- 618
- Office Version
- 2019
- 2016
- Platform
- Windows
Hey folks! Happy New Year!
I'm working on a sub that will loop thru folders/subfolders, open each file, and list the sheets in each file. However, I'm running into issues for files that are .xlsm or are password protected. Rather than click each time one of these files come's up, I'd like to programmatically click the appropriate button (Enable Macros for .xlsm files, and Cancel for password protected files).
Due to Admin requirements, I can't globally allow Enable Macros. I realize that the code below doesn't print (the sub I use is rather involved).
Thoughts?? Thanks y'all.
I'm working on a sub that will loop thru folders/subfolders, open each file, and list the sheets in each file. However, I'm running into issues for files that are .xlsm or are password protected. Rather than click each time one of these files come's up, I'd like to programmatically click the appropriate button (Enable Macros for .xlsm files, and Cancel for password protected files).
Due to Admin requirements, I can't globally allow Enable Macros. I realize that the code below doesn't print (the sub I use is rather involved).
Thoughts?? Thanks y'all.
Code:
Sub list_AllShtInFolder()
' ~~ Loop through every file in folder and list all sheet names
' [URL]https://www.mrexcel.com/forum/excel-questions/663583-loop-through-every-file-folder-list-all-sheet-names.html#5[/URL]
' ~~ Cycle through sub-folders and files in a user-specified root directory
' [URL]https://stackoverflow.com/a/22646086[/URL] || [URL]https://stackoverflow.com/questions/22645347/loop-through-all-subfolders-using-vba[/URL]
Const proc_name = "list_AllShtInFolder"
Dim fso As Object 'FileSystemObject
Set fso = CreateObject("Scripting.FileSystemObject") ' late binding
Dim Fldr As Object, _
subFldr As Object, _
Fl As Object
Dim queue As Collection
Set queue = New Collection
Dim arr As Variant
ReDim arr(1 To 1000000, 1 To 3)
arr(1, 1) = "Path": arr(1, 2) = "File": arr(1, 3) = "Worksheet"
Dim wbkDest As Workbook, _
wbk As Workbook
Set wbkDest = ActiveWorkbook
Dim shtDest As Worksheet, _
sht As Worksheet
Set shtDest = ActiveSheet
Dim filetype As String
filetype = "*.xlsx" 'The file type to search for
Dim i As Long, _
j As Long ' ~~ File counter
i = 2 'The first row of the active sheet to start writing to
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = wbkDest.path
.AllowMultiSelect = False
If .Show = -1 Then
queue.Add fso.GetFolder(fso.GetFolder(.SelectedItems(1) & "")) ' ~~ Starting Folder
Else
Exit Sub 'Cancel was pressed
End If
End With '.FileDialog
Do While queue.Count > 0
Set Fldr = queue(queue.Count)
queue.Remove (queue.Count) 'dequeue
'...insert any folder processing code here...
For Each subFldr In Fldr.SubFolders
queue.Add subFldr 'enqueue
Next subFldr
For Each Fl In Fldr.Files
If Fl.name Like filetype Then
' ...insert any file processing code here...
On Error Resume Next
Set wbk = Workbooks.Open(Fl, UpdateLinks:=False, ReadOnly:=True)
j = j + 1
If Not wbk Is Nothing Then
For Each sht In wbk.Sheets
arr(i, 1) = Fldr & ""
arr(i, 2) = wbk.name
arr(i, 3) = sht.name
i = i + 1
Next sht
wbk.Close SaveChanges:=False
Else
arr(i, 1) = Fldr & ""
arr(i, 2) = wbk.name
arr(i, 3) = "Unable to access workbook"
i = i + 1
End If 'wbk <> Nothing
On Error GoTo 0
End If 'Fl.name
Next Fl
Loop 'queue.Count
End Sub