Solved

Open Windows explorer to folder if not already open

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
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.

839 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