簡體   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