Solved

Open Windows explorer to folder if not already open

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

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

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…
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

679 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