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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16956153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:29:50  来源:igfitidea点击:

check if a folder is open (vba)

vbadirectory

提问by Spymag

I am trying to check whether a specific folder is open or not using VBA . I have found this code:

我正在尝试使用 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

But i am getting an error that the object is not supporting this property or method.

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

Any ideas?

有任何想法吗?

回答by deviant

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. Code below.

感谢您的代码,它也帮助了我......我已经检查过 Wnd.Document.Folder.Self.Path 不适用于所有 Window 对象,例如 IE,这就是为什么它会给你一条错误消息,我有通过首先检查窗口是否是 Windows 资源管理器窗口来完成。代码如下。

Notes: Wnd.Document.Folder.Self.Pathgives 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 OpenFoldvariable 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