In order to check if a folder or any of its sub-folders are open, please try the next function:
Function isFoldSubFoldOpen(strFolder As String, Optional boolSubFld As Boolean = False) As Boolean
Dim oShell As Object, Wnd As Object, sFld As Variant
Set oShell = CreateObject("Shell.Application")
If boolSubFld Then
Dim fso As Object, fold As Object, colSFld As New Collection
Set fso = CreateObject("Scripting.FileSystemObject")
Set fold = fso.GetFolder(strFolder)
AllSubFolders fold, colSFld
End If
For Each Wnd In oShell.Windows
If Wnd.name = "Windows Explorer" Or Wnd.name = "File Explorer" Then
If Wnd.Document.folder.Self.path = strFolder Then isFoldSubFoldOpen = True: Exit Function
If boolSubFld Then
For Each sFld In colSFld
If Wnd.Document.folder.Self.path = sFld Then
Debug.Print Wnd.Document.folder.Self.path
isFoldSubFoldOpen = True: Exit Function
End If
Next sFld
End If
End If
Next Wnd
End Function
It also needs a recursive Sub, which places all sub-folders in a collection:
Private Sub AllSubFolders(FSOFolder As Object, colSFld As Collection)
Dim objSubfold As Object, objFile As Object
For Each objSubfold In FSOFolder.SubFolders
colSFld.Add objSubfold 'place the subfolder in the collection
AllSubFolders objSubfold, colSFld 'recursively call the sub itself
Next
End Sub
The above function can be called to only return True if the folder is open (without the second parameter, or with it False) or any of its subfolders:
Debug.Print isFoldSubFoldOpen(FolderPath) 'check if the folder is open
Debug.Print isFoldSubFoldOpen(FolderPath, True) 'check if the folder or any of its sub-folders are open