How do I open a particular folder windows explorer from VB and
automatically highlight a particular file in windows explorer ?
I know this is possible cuz Ive done it earlier, I just cant remember
which APIs did it. Please advise.
Thank you
- Microsoft Internet Controls
- Microsoft Shell Controls and Automation
The following code goes in a button click. It assumes
that you have a folder "C:\windows\desktop\New Folder"
and that among the files in that folder is one named "test.txt".
----------------------
Private Sub Command1_Click()
Dim SH As Shell
Dim SFV As ShellFolderView
Dim FIs As FolderItems, FI As FolderItem
Dim Wins As Object
Dim IE As InternetExplorer
Dim FIVs As FolderItemVerbs, FIV As FolderItemVerb
Set SH = New Shell
SH.Open "C:\Windows\desktop\New Folder"
Set Wins = SH.Windows
For Each IE In Wins
If IE.LocationName = "New Folder" Then
Set SFV = IE.Document
Set FIs = SFV.Folder.Items
For Each FI In FIs
If FI.Name = "test.txt" Then
' FI.InvokeVerb "&Open"
SFV.SelectItem FI, 17
Exit For
End If
Next
Set FIs = Nothing
Set SFV = Nothing
End If
Next
Set Wins = Nothing
Set SH = Nothing
End Sub
------------------
I *think* this works. I'm on Win98SE and the SelectItem
method does not work here. Yet if I comment that line
and uncomment the line above with InvokeVerb then
test.txt is opened in Notepad. And if I add a few debug.print
lines to see what's happening I find that the ShellFolderView
object is OK, I can get the specified folder as a Folder object,
I can list its FolderItems OK with the FI.Name property....
So everthing is in order. But SelectItem does not work. I
seem to remember that that was fixed in 2000 or ME, but
I'm not certain. You'll have to test it on the target machine.
There may be several ways to skin that cat, here is one using
VB's Shell command:
file = "D:\temp\test.txt"
Shell "explorer """ & file & """, /select", vbNormalFocus
LFS
SFV.SelectItem FI, 4
SFV.SelectItem FI, 17
The second call here is 17 -- 16 + 1.
1 sets selection.
16 sets focus.
----------------------------
SelectItem Method
----------------------------------------------------------------------------
----
object.SelectItem vItem, dwFlags
dwFlags:
0 Deselect the item.
1 Select the item.
3 Put the item in edit mode.
4 Deselect all but the specified item.
8 Ensure the item is displayed in the view.
16 Give the item the focus.
It is a bit cryptic, but I suspect that a single call with 5 (1 + 4) would get
it done, or if you want focus as well, 21 (1 + 4 + 16) would work as well. Just
4 would leave it selected if it already was selected, but not select it if it
was not.
Interesting idea. It doesn't work on Win98, though.
Maybe it's the same issue that ShellFolderView has.
When I run your code I see the folder opened but the
file is not selected -- the same thing that the
ShellFolderView method does on Win98. Presumably your
method works OK on XP.