我使用以下代码在 Windows 资源管理器窗口中选择一个文件:
Set objShellAPP = CreateObject("Shell.Application")
objShellAPP.Open(WScript.Arguments(0))
WScript.Sleep(300)
On Error Resume Next ' For new unsaved files.
With objShellAPP.Windows(objShellAPP.Windows.Count - 1).document
.SelectItem .Folder.Items.Item(WScript.Arguments(1)), 29
End With
Set objShellAPP = Nothing
objShellAPP.Open()
在最后一个活动窗口中打开文件夹;
objShellAPP.Windows.Count - 1
不一定是那个窗口。
我如何才能获得该With objShellAPP.Windows(---).document
线路的最后一个活动窗口?
谢谢。
看:
答案1
在下面的代码中,第一个 Windows 资源管理器窗口被激活,然后在那里打开一个文件夹(我使用快捷键标签栏)。
Set objShellApp = CreateObject("Shell.Application")
Dim winFolder
For Each wFolder In objShellApp.Windows
On Error Resume Next
isFolder = wFolder.document.folder ' An IE window does not support ".document.folder". We get the first Windows Explorer window.
If Err = 0 Then
wFolder.Visible = True
Set winFolder = wFolder
Exit For
End If
Next
objShellApp.Open(WScript.Arguments(0))
WScript.Sleep(300)
If NOT WScript.Arguments(1) = "" Then
If winFolder = Empty Then
Set winFolder = objShellApp.Windows(objShellApp.Windows.Count - 1) ' A Windows Explorer window opened with objShellApp.Open(WScript.Arguments(0)) is the last one in the index (Count - 1).
End If
On Error Resume Next ' For new unsaved files.
With winFolder.document
.SelectItem .Folder.Items.Item(WScript.Arguments(1)), 29
End With
End If
Set winFolder = Nothing
Set objShellApp = Nothing
至于区分 Windows-Explorer 和 Internet-Explorer 的更好方法: