Solved

Open Windows explorer to folder if not already open

Posted on 2014-09-30
2
248 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

773 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