Excel VBA can retrieve the full path of the currently open workbook using the ThisWorkbook.Path property or the ActiveWorkbook.Path property, depending on whether you want the workbook containing the code or the one currently active. This is essential for saving files to the same folder, referencing external data, or automating folder-based tasks.

What You'll Need

  • Microsoft Excel installed (Excel 2010 or later recommended; works in all modern versions)
  • A workbook saved to disk (unsaved workbooks return an empty string)
  • Basic familiarity with the VBA editor (Alt + F11 opens it)

Step-by-Step

  1. Open the Excel workbook where you want to retrieve the file path. Ensure the workbook is saved—unsaved workbooks (e.g., "Book1.xlsx" before first save) will cause ThisWorkbook.Path to return an empty string.
  2. Press Alt + F11 to open the Visual Basic for Applications (VBA) editor.
  3. In the Project Explorer window (if hidden, press Ctrl + R to show it), locate your workbook under VBAProject (your_workbook_name.xlsm).
  4. Double-click the module where you want to add the code (e.g., Module1), or right-click the workbook name > Insert > Module to create a new one.
  5. Type or paste the following subroutine to display the current file path in a message box:
    Sub GetCurrentFilePath()
        Dim fullPath As String
        fullPath = ThisWorkbook.Path
        If fullPath = "" Then
            MsgBox "This workbook is unsaved. Save it first to get a path.", vbExclamation
        Else
            MsgBox "Current file path: " & fullPath, vbInformation
        End If
    End Sub
    This uses ThisWorkbook.Path to get the folder path (not including the filename or trailing backslash).
  6. Press Alt + Q to return to Excel, then press Alt + F8 to open the Macro dialog.
  7. 7. Select GetCurrentFilePath and click Run. A message box appears showing the folder path (e.g., C:\Users\YourName\Documents).

Common Mistakes

  • Using ActiveWorkbook.Path instead of ThisWorkbook.Path when the code resides in an add-in or another workbook. ThisWorkbook always refers to the workbook containing the running code; ActiveWorkbook refers to whichever workbook is currently active, which may differ.
  • Expecting a path for an unsaved workbook. Excel assigns temporary paths only after the first save, so ThisWorkbook.Path returns "" until then.
  • Assuming the path includes the filename. The .Path property returns only the folder path. To get the full filename, use ThisWorkbook.FullName (includes path and filename) or combine .Path with .Name: ThisWorkbook.Path & "\" & ThisWorkbook.Name.
  • Forgetting to save the workbook as macro-enabled (.xlsm) if storing code in it. Saving as .xlsx removes macros silently.

Tips

  • Use ThisWorkbook.Path & Application.PathSeparator for safer path concatenation across operating systems (though Excel VBA is Windows-focused, this avoids hardcoded backslashes).
  • For robust error handling, wrap path-dependent operations in On Error Resume Next and verify the path isn’t empty before proceeding.
  • To get just the folder name (not full path), use: Dir(ThisWorkbook.Path, vbDirectory) won’t work—instead, parse with Split(ThisWorkbook.Path, "\")(UBound(Split(ThisWorkbook.Path, "\"))) for the last folder segment.
  • When referencing external files in the same folder, build dynamic paths like: myFile = ThisWorkbook.Path & "\" & "data.xlsx" to avoid hardcoded references.
  • For network paths (e.g., \\server\share), ThisWorkbook.Path returns the full UNC path—no extra handling needed in VBA.

Troubleshooting

If the macro returns no path or throws an error:

  1. Verify the workbook is saved. Go to File > Save As and save it to a local or network location, then re-run the macro.
  2. Check for hidden workbooks. Close all other Excel windows and re-open only the target workbook to ensure ThisWorkbook refers to the correct file.
  3. Confirm macro security settings. In Excel, go to File > Options > Trust Center > Trust Center Settings > Macro Settings. Select "Disable all macros with notification" (not "Disable all macros without notification"). Restart Excel and enable macros when prompted.
  4. If using Excel Online, VBA is unsupported—this method only works in desktop Excel (Windows or Mac). On Mac, use the same code, but paths use colons instead of backslashes (e.g., Macintosh HD:Users:YourName:Documents).
  5. If the code runs but the message box shows nothing, check for invisible characters or line breaks in the path by assigning it to a cell: Cells(1, 1).Value = ThisWorkbook.Path and inspect the cell directly.

Frequently Asked Questions

Does ThisWorkbook.Path include the filename?

No. It returns only the folder path (e.g., C:\Projects), not the filename. Use ThisWorkbook.FullName for the complete path including filename.

Why does ActiveWorkbook.Path return a different path than ThisWorkbook.Path?

ActiveWorkbook.Path points to the currently focused workbook, which may be a different file if multiple workbooks are open. ThisWorkbook.Path always refers to the workbook containing the running VBA code—critical when using add-ins or external macro workbooks.

Can I get the path of a closed workbook?

No. VBA requires the workbook to be open to access Its.Path. To read properties of a closed workbook, use Windows Scripting or external tools like ADO—but this is beyond standard VBA and not recommended for beginners.

What happens if the file is on a network drive that’s disconnected?

Excel may still open the file if cached locally, but ThisWorkbook.Path might return an invalid or empty string if the network location is inaccessible. Always validate paths before file operations using Dir() or FileLen() to check existence.

How do I get just the filename without the path?

Use ThisWorkbook.Name for the filename only (e.g., "Report.xlsx"). To split the path and filename manually, combine Split() and UBound() on the full path string.

Is ThisWorkbook.Path reliable for automation across different Excel versions?

Yes. ThisWorkbook.Path has remained consistent since Excel 97 and works identically in Excel 2010 through Excel 2021 and Microsoft 365. The only exception is Excel Online, where VBA isn’t supported at all.