我正在尝试使用VBA检查特定文件夹是否打开。我找到了这段代码:
Sub test1()
Dim OpenFold As Variant
Dim oShell As Object
Dim Wnd As Object
Dim strFolder
OpenFold = "mysubfolder"
strFolder = "U:\myfolder\" & OpenFold
Set oShell = CreateObject("Shell.Application")
For Each Wnd In oShell.Windows
If Wnd.Document.Folder.Self.Path = OpenFold Then 'this is where it gives me the error
Exit Sub ' Folder is open - exit this Sub
End If
Next Wnd
Application.ThisWorkbook.FollowHyperlink Address:=strFolder, NewWindow:=True
End Sub
但是我收到一个错误,该对象不支持此属性或方法。
有什么想法吗?
答案 0 :(得分:1)
感谢代码,它也帮助了我......我已经检查过了 Wnd.Document.Folder.Self.Path不适用于所有Window对象,例如IE,这就是为什么它会给你一个错误信息,我已经通过首先检查Windows是否是Windows资源管理器窗口来完成它。代码如下。
注意:
Wnd.Document.Folder.Self.Path
为您提供完整的路径字符串,因此您可能希望对其进行比较
与strFolder
。
如果您想将其与OpenFold
变量进行比较,您可能需要使用
Wnd.LocationName = OpenFold
最终守则:
Sub test1()
Dim OpenFold As Variant
Dim oShell As Object
Dim Wnd As Object
Dim strFolder
OpenFold = "mysubfolder"
strFolder = "U:\myfolder\" & OpenFold
Set oShell = CreateObject("Shell.Application")
For Each Wnd In oShell.Windows
If Wnd.Name = "Windows Explorer" Then
If Wnd.Document.Folder.Self.Path = strFolder Then Exit Sub
End If
Next Wnd
Application.ThisWorkbook.FollowHyperlink Address:=strFolder, NewWindow:=True
End Sub