VBA Code Library
This macro opens a folder using VBA. If the folder is already opened in a Windows Explorer window, it will show the open window instead of opening a new window.
All you have to do is copy and paste the
Open Folder Using VBA
Private Sub OpenFolder(strDirectory As String) 'DESCRIPTION: Open folder if not already open. Otherwise, activate the already opened window 'DEVELOPER: Ryan Wells (wellsr.com) 'INPUT: Pass the procedure a string representing the directory you want to open Dim pID As Variant Dim sh As Variant On Error GoTo 102: Set sh = CreateObject("shell.application") For Each w In sh.Windows If w.Name = "Windows Explorer" Or w.Name = "File Explorer" Then If w.document.folder.self.Path = strDirectory Then 'if already open, bring it front w.Visible = False w.Visible = True Exit Sub End If End If Next 'if you get here, the folder isn't open so open it pID = Shell("explorer.exe " & strDirectory, vbNormalFocus) 102: End Sub
Sub OpenFolderDemo() 'Demo - opens the folder location saved to the variable strPath Dim strPath As String strPath = "C:\Windows" Call OpenFolder(strPath) End Sub
I hope you found this VBA Code Library example helpful. Submit a comment below if you like this macro or have more questions. As always, subscribe to my email list, share this article on social media and follow me on Google+ and Twitter for more great VBA content!
Get these macros delivered right to your spreadsheet for free
Access tutorials and import my macros without ever leaving ExcelShow me - It's free
About Ryan Wells
Ryan Wells is a Nuclear Engineer and professional VBA Developer. He is the lead developer of several VBA applications, including PDF VBA - a leading Excel Add-in for exporting Excel Objects, like charts and tables, to PDFs. Discover more of his popular Excel Add-ins, including Mouse To Macro and CF Shapes, at his dedicated Excel Add-ins page.Follow