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

Re: Opening a folder and auto-selecting file in it by mayayana

mayayana
Sat Oct 11 12:26:15 CDT 2008

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.

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



Re: Opening a folder and auto-selecting file in it by Larry

Larry
Sat Oct 11 12:56:46 CDT 2008


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

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

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



Re: Opening a folder and auto-selecting file in it by mayayana

mayayana
Sat Oct 11 13:36:16 CDT 2008


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.




Re: Opening a folder and auto-selecting file in it by Steve

Steve
Sat Oct 11 13:43:16 CDT 2008

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.




Re: Opening a folder and auto-selecting file in it by mayayana

mayayana
Sat Oct 11 13:43:47 CDT 2008

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



Re: Opening a folder and auto-selecting file in it by Faraz

Faraz
Mon Oct 13 04:59:47 CDT 2008

Thank you all. The Shell thing works ok for me.