0

I use the following code to select a file in a Windows Explorer window:

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() opens the folder in the last active window;

objShellAPP.Windows.Count - 1 is not necessarily that window.

How can I get the last active window for the With objShellAPP.Windows(---).document line?

Thank you.


See:

Open and Select a file in an existing Explorer window

VBS: Select one file in a folder

Yaron
  • 133
  • 2
  • 13

1 Answers1

0

In the following code the first Windows Explorer window is activated and then a folder is opened there (I use QTTabbar).

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

As for a better way to distinguish between Windows-Explorer and Internet-Explorer:

VBS: Distinguis between Windows-Explorer and Internet-Explorer

Yaron
  • 133
  • 2
  • 13