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

LVL 5
rberkeConsultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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?
0
rberkeConsultantAuthor Commented:
Right now the code OPENS c:\aatmpk\test.xlsm in excel.

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

rberke (Bob)
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Got it. I don't think any web control object allows that.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

rberkeConsultantAuthor Commented:
Thanks for trying. i hope someone else can figure this out.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
I have been correct, the web browser control does not support that, but the FolderView object does - which is the .Document, luckily.
In the .Folder.Items.Item method you have to use the file name without path, but that is better anyway for you logic (doesn't make much sense to have the path repeated in two variables).
Sub test()

Dim FolderPath, FullPath, PathOpen
FolderPath = "C:\aatmpK"
Filename = "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
      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(.Folder.Self.Path, "s:", "myserver\myshare", 1, 1, 1)
          If LCase(FolderPath) = LCase(PathOpen) Then
            .SelectItem .Folder.Items.Item(Filename), 1 + 4 + 8 + 16
             WasFound = True
             Exit For
          End If
        End With
        ExplorerNoDups = ExplorerNoDups & "<" & PathOpen & ">"
     End If
    Next
    If WasFound = False Then MsgBox "Folder was not open"
  End With

End Sub

Open in new window

SelectItem flags: https://msdn.microsoft.com/en-us/library/windows/desktop/bb774047(v=vs.85).aspx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rberkeConsultantAuthor Commented:
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

0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0
rberkeConsultantAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.