简体   繁体   English

检查文件夹是否打开(vba)

[英]check if a folder is open (vba)

I am trying to check whether a specific folder is open or not using VBA . 我正在尝试检查是否使用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. 但是我收到一个错误,该对象不支持此属性或方法。

Any ideas? 有任何想法吗?

Thanks for the code it also helped me... I have checked that 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. 感谢您提供的代码,它也对我有所帮助...我检查了Wnd.Document.Folder.Self.Path并不适用于所有Window对象,例如IE,这就是为什么它将给您错误消息的原因,首先检查窗口是否为Windows资源管理器窗口来完成此操作。 Code below. 下面的代码。

Notes: Wnd.Document.Folder.Self.Path gives you full path string so you might want to compare it with strFolder . 注意: Wnd.Document.Folder.Self.Path为您提供完整的路径字符串,因此您可能需要将其与strFolder进行比较。

If you want to compare it to OpenFold variable you might want to use 如果要将其与OpenFold变量进行比较,则可能要使用

Wnd.LocationName = OpenFold

Final 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.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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM