Solved

Open Windows explorer to folder if not already open

Posted on 2014-09-30
2
228 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
2 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now