Open Containing Folder

confused

I ran into a problem, and maybe some of you out there have as well: Ever have an Excel file open and need to open the folder it's in?

Here's how it happens: After I browse to a folder and open the workbook I want, I close the Explorer window. It's a bad habit, but I can't help it. Inevitably, I need to open another file in the same folder. So I have to browse the entire folder tree again to get back to the folder I was in. No more!

This code will open Windows Explorer to the folder where the current file is located. The "Declare" statement should be placed at the top of a standard module. Note that it works on saved workbooks only. It uses a Windows API to open Windows Explorer.

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
Sub OpenContainingFolder()

  On Error GoTo ErrorHandler

  Dim currentWkbkPath As String

  currentWkbkPath = ActiveWorkbook.Path

  ShellExecute 0, "open", currentWkbkPath, 0, 0, 1

ProgramExit:
  Exit Sub
ErrorHandler:
  MsgBox Err.Number & " - " & Err.Description
  Resume ProgramExit
End Sub

Usable in other Office programs too! For example, MS Word:

Sub OpenContainingFolder()

  On Error GoTo ErrorHandler

  Dim currentDocPath As String

  currentDocPath = ActiveDocument.Path

  ShellExecute 0, "open", currentDocPath, 0, 0, 1

ProgramExit:
  Exit Sub
ErrorHandler:
  MsgBox Err.Number & " - " & Err.Description
  Resume ProgramExit
End Sub

and MS Access:

Sub OpenContainingFolder()

  On Error GoTo ErrorHandler

  Dim currentDBPath As String

  currentDBPath = Application.CurrentProject.Path

  ShellExecute 0, "open", currentDBPath, 0, 0, 1

ProgramExit:
  Exit Sub
ErrorHandler:
  MsgBox Err.Number & " - " & Err.Description
  Resume ProgramExit
End Sub

although the Access VBA code must be tied to a specific DB. Now go, and never have this problem again!

About JP

I'm just an average guy who writes VBA code for a living. This is my personal blog. Excel and Outlook are my thing, with a sprinkle of Access and Word here and there. Follow this space to learn more about VBA. Keep Reading »


Related Articles:


Share This Article:

Share and bookmark this articledelicious buttonfacebook buttonlinkedin buttonstumbleupon buttontwitter button

comment bubble 8 Comment(s) on Open Containing Folder:

  1. Reuvain writes:

    Thanks JP!
    This will be a big time saver for me.

  2. Rick Rothstein (MVP - Excel) writes:

    If I understand what your code is doing correctly, then (for Excel) I think this one-liner will do the same thing…

    Sub OpenContainingFolder()
        Application.Dialogs(xlDialogOpen).Show ActiveWorkbook.Path
    End Sub
    
  3. Reuvain writes:

    Rick, that's true but JP's solution opens the folder with Windows Explorer instead of the Excel Open File dialog.

  4. Rick Rothstein (MVP - Excel) writes:

    @Reuvain,

    Ah, yes, okay, in that case, what about this one-liner instead then…

    Sub OpenContainingFolder()
        Shell "explorer.exe " & ActiveWorkbook.Path & "\", vbNormalFocus
    End Sub
    
  5. sachinmal writes:

    Sorry to be asking a basic question, but if i want this Macro to be available for ALL of the excel files that I open, where (which file) should this code be put in? Is there a global file in which this can be put, which will automatically load/execute this code?

Mentions:

  1. [...] can write a little code that will allow you to open the containing folder of any Office file. See this blog for instructions. Fortunately for my colleague she is a programmer. I suspect she's going to take this route. [...]

  2. [...] in my college's Computer Science department. (Thanks, Tina!) The original code comes from the Code for Excel and Outlook blog. While it may look scary, it took less than 5 minutes to set it [...]

This article is closed to new comments. Why?
Random Data Generator