问题描述
我正在尝试使用VBA检查特定文件夹是否打开。我找到了以下代码:
I am trying to check whether a specific folder is open or not using VBA . I have found this code:
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
但是我收到一个错误,即对象不支持此属性或方法。
But i am getting an error that the object is not supporting this property or method.
有什么想法吗?
推荐答案
感谢代码,它也对您有所帮助我...我已经检查了
Wnd.Document.Folder.Self.Path不适用于所有Window对象,例如IE,这就是为什么它将给您错误消息的原因,我已经做到了首先检查窗口是否为Windows资源管理器窗口。下面的代码。
Thanks for the code it also helped me... I have checkedthat the Wnd.Document.Folder.Self.Path does not apply to all Window object, for example IE, that is why it will give you an error message, I have done it by checking first if the windows is a Windows Explorer window. Code below.
注意:
Wnd.Document.Folder.Self.Path
为您提供完整路径字符串,因此您可能希望将
与 strFolder
进行比较。
Notes:Wnd.Document.Folder.Self.Path
gives you full path string so you might want to compare itwith strFolder
.
如果要与您可能要使用的 OpenFold
变量
If you want to compare it to OpenFold
variable you might want to use
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
这篇关于检查文件夹是否打开(vba)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!