我正在使用下面的代码打开特定文件夹。
我需要在打开之前检查一下,如果是的话文件夹或子文件夹是否已在 Windows 资源管理器中打开,以防止重复。
感谢任何有用的评论和答案。
Sub Prevent_opening_duplicate_folder ()
Dim Folder_Path As String
Folder_Path = "D:\Users\Waleed\Desktop\Test"
Shell "explorer """ & Folder_Path & ""
DoEvents
End Sub
这将做到这一点:
Function isFolderOpen(Path As String) As Boolean
Dim sh As Object, w As Object, Document
Set sh = CreateObject("shell.application")
For Each w In sh.Windows
If w.Name = "Windows Explorer" Or w.Name = "File Explorer" Then
Debug.Print w.Document.folder.self.Path
Set Document = w.Document
If Document.folder.self.Path = Path Then
isFolderOpen = True
Exit Function
End If
End If
Next
End Function
参考:Excel VBA 仅打开文件资源管理器的一个实例 https://answers.microsoft.com/en-us/msoffice/forum/all/excel-vba-to-open-only-one-instance-of-file/3eb1fd47-d256-4ecf-af8b-9c67a0874ab8
附录
Function isFolderOpenOrSubFolder(Path As String) As Boolean
Dim sh As Object, w As Object, Document
Set sh = CreateObject("shell.application")
For Each w In sh.Windows
If w.Name = "Windows Explorer" Or w.Name = "File Explorer" Then
Set Document = w.Document
If Left(Document.folder.self.Path, Len(Path)) = Path Then
isFolderOpenOrSubFolder = True
Exit Function
End If
End If
Next
End Function
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)