views:

30

answers:

2

I'd like to know how, if possible, to get the path of the selected file in an open Windows Explorer window. If not, would it at least be possible to get the folder path of an open Windows Explorer window?

The end reason I'm doing this, is a software tool I'm writing requires a user to select a file. I figure if they're already moving the file around with Windows Explorer and then start my tool up, it'd be good not to have to make them navigate to the folder again in the file open dialog box. My software would then be able to identify if it's got the right file extension and if so, just ask the user if they want to import that file.

Thanks.

A: 

So after some more creative Google Searching, I found a method using the ShellWindows class from ShDocVW.dll

In VB/A, with a reference set to ShDocVW.dll (Microsoft Internet Controls), here's the code I used to just get a collection of all the selected file names in all the open Explorer windows:

Function GetSelectedFilesInWinExplorers() As Collection
    Dim Result As New Collection
    Dim ExpWin As Object
    Set ExpWin = New SHDocVw.ShellWindows
    Dim CurrWin As SHDocVw.InternetExplorer
    On Error Resume Next
    Dim CurrSelFile As String
    For Each CurrWin In ExpWin
        If Not CurrWin.Document Is Nothing Then
            If Not CurrWin.Document.FocusedItem Is Nothing Then
                CurrSelFile = CurrWin.Document.FocusedItem.Path
                If CurrSelFile <> "" Then
                    Result.Add CurrSelFile
                    Debug.Print CurrSelFile
                End If
                CurrSelFile = ""
            End If
        End If
    Next CurrWin
    Set GetSelectedFilesInWinExplorers = Result
End Function

I had to use On Error Resume Next because for some reason the FocusedItem wouldn't be nothing but would still raise an error. That and I don't really care about using it in this context.

Jon Fournier
+1  A: 

I think Raymond Chen answered exactly your question with this blog post:

http://blogs.msdn.com/oldnewthing/archive/2004/07/20/188696.aspx

Its some pretty impressive code I think but as he says: "This is not an inherently difficult task. You just have to put together lots of small pieces." ;)

Heinrich Ulbricht
Thanks, that's pretty much what I used (ShellWindows), just using VBA
Jon Fournier