繁体   English   中英

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

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

我正在尝试检查是否使用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

但是我收到一个错误,该对象不支持此属性或方法。

有任何想法吗?

感谢您提供的代码,它也对我有所帮助...我检查了Wnd.Document.Folder.Self.Path并不适用于所有Window对象,例如IE,这就是为什么它将给您错误消息的原因,首先检查窗口是否为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

暂无
暂无

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

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