Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Opening a folder and auto-selecting file in it

1 view
Skip to first unread message

Faraz Azhar

unread,
Oct 11, 2008, 10:33:05 AM10/11/08
to
Hello

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

mayayana

unread,
Oct 11, 2008, 1:26:15 PM10/11/08
to
Set a reference to

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

Larry Serflaten

unread,
Oct 11, 2008, 1:56:46 PM10/11/08
to

"Faraz Azhar" <itzf...@gmail.com> wrote

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


mayayana

unread,
Oct 11, 2008, 2:36:16 PM10/11/08
to

I tested this on XP. It does work. However,
the flag parameter is a bit confusing. A flag of
4 is supposed to "Deselect all but the specified item."
But it does not select the specific item! On the
other hand, if you just use a flag of 1 (Select
the item), the selection will be added to any
current selection. So to select one and only one
item, it needs two calls:

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.

Steve Gerrard

unread,
Oct 11, 2008, 2:41:36 PM10/11/08
to
mayayana wrote:
> I tested this on XP. It does work. However,
> the flag parameter is a bit confusing. A flag of
> 4 is supposed to "Deselect all but the specified item."
> But it does not select the specific item! On the
> other hand, if you just use a flag of 1 (Select
> the item), the selection will be added to any
> current selection. So to select one and only one
> item, it needs two calls:
>
> SFV.SelectItem FI, 4
> SFV.SelectItem FI, 17
>
> 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.

mayayana

unread,
Oct 11, 2008, 2:43:47 PM10/11/08
to
>
> file = "D:\temp\test.txt"
> Shell "explorer """ & file & """, /select", vbNormalFocus
>

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.


Faraz Azhar

unread,
Oct 13, 2008, 5:59:47 AM10/13/08
to
Thank you all. The Shell thing works ok for me.
0 new messages