?
Solved

Open Windows explorer to folder if not already open

Posted on 2014-09-30
2
Medium Priority
?
290 Views
Last Modified: 2014-09-30
I am using the following code to launch Windows explorer to a specified folder. Is there any easy way to test if there is already a window displaying the folder so I don't reopen it?
    Dim sFilePath As String

    On Error GoTo btnOpenFolder_Click_Error
   
    If Dir(PLANPATH & Me.Customer & "\" & Me.JobName, vbDirectory) <> "" Then
        sFilePath = PLANPATH & Me.Customer & "\" & Me.JobName
    Else
        sFilePath = PLANPATH
    End If
   
    On Error Resume Next

    Shell "explorer.exe " & sFilePath, vbNormalFocus

    On Error GoTo 0
    Exit Sub


btnOpenFolder_Click_Error:

    MsgBox Err.Number & " " & Err.Description, , "Error"
0
Comment
Question by:Rob4077
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 25

Accepted Solution

by:
chaau earned 2000 total points
ID: 40353873
Use ShellExecute Windows API. It is smart enough to not open another explorer if one already opened for the path:

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub Command0_Click()
 Dim sFilePath As String

    On Error GoTo btnOpenFolder_Click_Error
   
    sFilePath = "c:\temp"
   
    On Error Resume Next

    'Shell "explorer.exe " & sFilePath, vbNormalFocus
    ShellExecute 0&, vbNullString, sFilePath, vbNullString, vbNullString, vbNormalFocus

    On Error GoTo 0
    Exit Sub


btnOpenFolder_Click_Error:

    MsgBox Err.Number & " " & Err.Description, , "Error"
End Sub

Open in new window

I have changed the sFilePath for testing in the code above
0
 

Author Comment

by:Rob4077
ID: 40353902
That was an easy solution. Thanks!!!!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question