Link to home
Start Free TrialLog in
Avatar of Robert Berke
Robert BerkeFlag for United States of America

asked on

How to use IWebBrowser2 object to navigate a windows folder.

I am trying to learn how to program navigation of a  Windows Folder using Explorer.

As a test project, I want to use the IWebBrowser2 object to select a specific file in a previously opened folder.

In other words, I want it to be similar to the DOS command line  < explorer /select,"C:\aatmpK\test.xlsm" >
the following code comes close, but the .navigate command is not exactly right.

An Excel vba solution would be ideal, but any solution that uses the IWebBrowser object would be OK.

can anybody help?
 
Sub test 

Dim FolderPath, FullPath, PathOpen
FolderPath = "C:\aatmpK"
FullPath = "c:\aatmpk\test.xlsm"

Dim WasFound As Boolean, i As Long, ExplorerNoDups As String
Dim wndw As Object

  With CreateObject("shell.application")
    For Each wndw In .Windows
     On Error Resume Next
        Dim IsFolder As Boolean
        IsFolder = False
        IsFolder = TypeName(wndw.document) Like "IShellFolderViewDual*"
        
     On Error GoTo 0
     If IsFolder Then
        If Not TypeName(wndw) Like "IWebBrowser*" Then Stop
        
        PathOpen = Replace(wndw.document.Folder.Self.path, "s:", "myserver\myshare", 1, 1, 1)
        If LCase(FolderPath) = LCase(PathOpen) Then
            
             wndw.navigate FullPath  ' < this does not seem to do what I want
             WasFound = True
             Exit For
        End If
        ExplorerNoDups = ExplorerNoDups & "<" & PathOpen & ">"
     End If
    Next
    If WasFound = False Then MsgBox "Folder was not open"
  End With

 
End Function

Open in new window

Avatar of Qlemo
Qlemo
Flag of Germany image

The code works as is - of course you need to change the last line to End Sub ;-), but that is it. I've used paths and files located on a share for test, if that matters.
What is the error or misbehaviour you get?
Avatar of Robert Berke

ASKER

Right now the code OPENS c:\aatmpk\test.xlsm in excel.

I want the code to SELECT the file in windows explorer.

rberke (Bob)
Got it. I don't think any web control object allows that.
Thanks for trying. i hope someone else can figure this out.
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That was closer, but it still didn't work.  

First, I needed to define Filename with
     2:  Dim  filename as String.

Then line 23 aborts with "Object doesn't support this action".  
This is because  .Folder.Items.Item(Filename)  wants a variant.  
I then tried 2: Dim Filename as variant. That stopped the abort, but the selection still did not change.
I finally decided that line 23 wants an integer  index not a name.   The following code seems to work.

But, lots of Windows collections allow either an integer or a name, so I wonder why folder.items.item(,,) won't allow it?

Anyway, here is the working code. I wonder if anybody can eliminate the loop that finds the index

Sub test22()
Dim FileName As String
Dim FolderPath, FullPath, PathOpen, var

FolderPath = "C:\empty"
FileName = "test.xlxsm"
Debug.Print "-----------" & now
Dim WasFound As Boolean, i As Long, ExplorerNoDups As String, intIndex As Variant
Dim wndw As Object

    With CreateObject("shell.application")
    For Each wndw In .Windows
        Dim IsFolder As Boolean
        IsFolder = False
        On Error Resume Next
        IsFolder = TypeName(wndw.document) Like "IShellFolderViewDual*"
        On Error GoTo 0
        If IsFolder Then
        If Not TypeName(wndw) Like "IWebBrowser*" Then Stop
        With wndw.document
            PathOpen = Replace(wndw.document.Folder.Self.path, "s:", "myserver\myshare", 1, 1, 1)
            If LCase(FolderPath) = LCase(PathOpen) Then
                intIndex = 0
                For Each var In wndw.document.Folder.Items
                    If var.name = FileName Then
                        WasFound = True
                        Exit For
                    End If
                    intIndex = intIndex + 1
                Next
                If WasFound Then
                    wndw.document.SelectItem wndw.document.Folder.Items.item(intIndex), 1 + 4 + 8 + 16
                Else
                    MsgBox "folder " & FolderPath & " does not contain " & "test.xlsx"
                    Exit Sub
                End If
            End If
        End With
        End If
    Next
    If WasFound = False Then MsgBox "Folder  " & FolderPath & " was not open"
    End With

End Sub

Open in new window

I've tested my code in Excel VBA (without Option Explicit, so I was missing the Dim), and it worked with the name.

Explicitly dimming the variable as String causes an error, that is correct. As a variant works fine for me, the file gets selected in my Explorer window, and scrolls to window up/down if required.
I have retest your code and it works fine.  I must have done something wrong.  I was playing with various folders, and perhaps I looked at the wrong one.

Anyway,  Thanks a bunch.