Excel VBA File Path of an Open Workbook

VBA & macros · Updated November 3, 2026

Excel VBA File Path of an Open Workbook

In Excel VBA, use ActiveWorkbook.Path to get the folder of the workbook currently active in Excel, or ThisWorkbook.Path to get the folder of the workbook that contains the running macro. Both return an empty string if that workbook has not been saved; use .FullName when you need the path and workbook filename together.

Path, FullName, and Name: What Each Returns

A workbook has several related properties, and choosing the right one prevents a common source of file-handling errors:

  • Workbook.Path returns the workbook’s folder location, without the workbook’s filename. For a saved local file, a result might be C:\Reports\2026.
  • Workbook.FullName returns the workbook’s name together with its location, such as C:\Reports\2026\Sales.xlsx.
  • Workbook.Name returns only the filename, such as Sales.xlsx.

These are properties of a particular Workbook object, so the expression before the dot matters. For example, ActiveWorkbook.Path asks for the active workbook’s folder. It does not necessarily refer to the workbook where your macro is stored. To display the complete location of a saved active workbook, use ActiveWorkbook.FullName.

Do not add the filename to .Path unless you intend to build a full filename yourself. Conversely, do not treat .FullName as a folder: it already includes the workbook filename. This distinction is useful when a macro reads or writes a neighboring file, reports which workbook it processed, or saves an output to the same folder.

ActiveWorkbook Versus ThisWorkbook

ActiveWorkbook means the workbook in the active Excel window at the moment the code runs. It is often the right choice when a user selects a workbook and runs a macro to process that workbook. But the active workbook can change: a macro may open another file, activate a different workbook, or display a dialog that leaves another workbook in focus.

ThisWorkbook always means the workbook that contains the VBA code. If a macro is stored in the workbook being processed, ThisWorkbook.Path is a dependable way to refer to that workbook’s folder. If the macro is stored in the Personal Macro Workbook, an add-in, or a separate controller file, ThisWorkbook.Path points to that container instead—not to the user’s currently active data workbook.

Choose the reference based on the job, rather than assuming they are interchangeable:

  • Use ActiveWorkbook when the user’s currently selected workbook is the target.
  • Use ThisWorkbook when the file containing the macro is the target.
  • Use a named workbook reference when the macro must work with one specific workbook that is already open.

Display or Store the Open Workbook’s Folder

This Windows VBA example reports the active workbook’s folder and checks whether it has been saved. Before running any macro, save a copy of the workbook: VBA changes cannot be undone with Ctrl+Z.

Sub ShowActiveWorkbookFolder()
    Dim wb As Workbook
    Dim folderPath As String

    Set wb = ActiveWorkbook

    If wb Is Nothing Then
        MsgBox "There is no active workbook."
        Exit Sub
    End If

    folderPath = wb.Path

    If Len(folderPath) = 0 Then
        MsgBox "Save the workbook before using its folder path."
    Else
        MsgBox folderPath
    End If
End Sub

To see the result without a message box, assign it to a variable and inspect that variable in the VBA editor, or print it to the Immediate window with Debug.Print folderPath. The Immediate window is useful for diagnosis because it shows the text VBA actually received. On Windows, the VBA editor’s Immediate window can be opened from the View menu.

Get the Path of a Particular Open Workbook

If your macro should refer to a workbook by name, retrieve it from the Workbooks collection instead of relying on whichever file happens to be active. The name must match the open workbook’s filename, including its extension.

For example, if Quarterly Sales.xlsx is open, set a workbook variable to Workbooks("Quarterly Sales.xlsx") and read that object’s Path or FullName. This is more explicit than ActiveWorkbook, especially in a macro that opens or switches between several files. If the named workbook is not open, VBA raises an error, so production code should check the open workbooks or handle that error before using the reference.

Workbook names are the names Excel displays for open files, not necessarily the name of the worksheet or the title of a tab. Two open files cannot be referred to safely by guessing their order in the collection; identify the intended workbook by a known name or retain the object variable when opening it. If the macro itself opens the target, capturing the returned workbook object immediately gives later code a clear reference even after another file becomes active.

Unsaved Workbooks and Save-As Situations

A new workbook that has not yet been saved has no established folder, so its Path is an empty string. Check Len(wb.Path) = 0 before building a filename or passing the path to file-system code. Otherwise, a macro may accidentally create a relative filename, fail to find a file, or write somewhere other than intended.

FullName is not a substitute for checking whether the workbook has been saved. For workflows that require a real disk location, test Path first and ask the user to save the workbook if it is blank. Saving a new workbook with Save As establishes its location; after that, the path properties can identify it. If a saved workbook is moved or saved to a new location, read the property again rather than keeping an old path in a variable for later use.

A workbook can also be open while a Save As operation is in progress. If your code depends on the final location, obtain the path after the save has completed. Do not assume that a proposed filename in a dialog is already the workbook’s saved location.

Build a File Location in the Same Folder

To create a path for a companion file, combine the workbook folder, a separator, and the new filename. In VBA, Application.PathSeparator supplies the separator for the current Excel platform. For example, after confirming that wb.Path is not blank, a macro can construct a path for summary.txt in the same folder like this:

Sub ShowCompanionFilePath()
    Dim wb As Workbook
    Dim folderPath As String
    Dim companionPath As String

    Set wb = ActiveWorkbook
    folderPath = wb.Path

    If Len(folderPath) = 0 Then
        MsgBox "Save the workbook before building a file path."
        Exit Sub
    End If

    companionPath = folderPath & Application.PathSeparator & "summary.txt"
    MsgBox companionPath
End Sub

This example constructs a string; it does not create the text file or check that the folder exists. If you build a path to a subfolder, that subfolder must already exist unless your code creates it. Keep extensions and filenames explicit, and use the complete path when calling file operations that need an exact location.

For cloud-hosted workbooks, do not assume every returned location is an ordinary local folder. Depending on how a file is opened and the Microsoft 365 setup, Excel may expose a URL-style location or a synced local location. A URL is not automatically interchangeable with a Windows file-system path, so choose file operations that support the location you actually receive.

Common Mistakes and Troubleshooting

  • Using the wrong workbook object: If a macro is in Personal.xlsb, ThisWorkbook.Path identifies Personal.xlsb. Use the target workbook’s object instead.
  • Getting a blank result: Check whether the workbook has ever been saved. An empty Path usually means there is no saved folder to return.
  • Expecting a filename from Path: Path is the folder. Use FullName for folder plus filename, or Name for the filename alone.
  • Appending a separator blindly: Build the path deliberately with Application.PathSeparator. Avoid adding separators in several places, which can produce malformed strings.
  • Assuming a path stays current: If the workbook is saved under a new name or moved, retrieve its properties again after saving.
  • Treating a cloud URL as a local path: Inspect the returned value before passing it to code that expects a local drive or network path.

When troubleshooting, print both wb.Path and wb.FullName, and verify which workbook object wb represents. This separates a workbook-selection problem from a save-location problem. If you are using a named reference, confirm that the workbook is open and that the name includes its extension.

Windows, Mac, and Excel for the Web

The core VBA properties—Path, FullName, and Name—are used the same way in desktop Excel for Windows and Mac. However, the path format and available file-system behavior can differ by platform. Windows paths commonly use drive letters and backslashes; Mac locations can use a different path representation. Use Application.PathSeparator when joining path components rather than hard-coding a separator for both platforms.

Excel for the web does not run VBA macros. To use this code, open the workbook in the Excel desktop application with macros enabled, and only enable macros in files you trust. A workbook’s file location is also not a permission to access every folder: normal operating-system and organizational access controls still apply. If a macro writes, moves, or deletes files, review its actions and keep a backup before running it.

Frequently Asked Questions

How do I get the full file path of the active workbook?
Use ActiveWorkbook.FullName. It includes the workbook’s filename as well as its location. Check ActiveWorkbook.Path first if the workbook might be unsaved.

Why does ActiveWorkbook.Path return blank?
The active workbook has probably not been saved yet. Save it to a location, then read Path again. Also confirm that the active workbook is the one you intended to inspect.

Should I use ThisWorkbook.Path or ActiveWorkbook.Path?
Use ThisWorkbook.Path for the workbook containing the macro. Use ActiveWorkbook.Path for the workbook currently active in Excel. They can refer to different files.

Does Workbook.Path include the workbook filename?
No. Path returns the folder location. Use FullName for the folder and filename together, or Name for just the filename.

Can VBA get the path in Excel for the web?
Excel for the web does not execute VBA. Open the workbook in desktop Excel to run a VBA macro; cloud locations may also be represented differently from local disk paths.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.