Introduction
When using VBA macros to output a new file to the same folder as the current workbook or to log the location of an open file, you need to retrieve the workbook’s “Full Path” (the complete path combining the file location and name).
VBA provides convenient properties to retrieve this path information. In this article, I will explain the differences and usage of three main properties: .Path, .Name, and the most useful one, .FullName.
The 3 Main Properties
To get workbook path information, you mainly use the following three properties of the Workbook object.
| Property | Information Retrieved | Example (for C:\Users\User\Documents\Report.xlsx) |
| .Path | Folder path (without trailing \) | C:\Users\User\Documents |
| .Name | File name (including extension) | Report.xlsx |
| .FullName | Full path (Folder + File name) | C:\Users\User\Documents\Report.xlsx |
Completed VBA Code
Below is sample code that uses these three properties to retrieve information about the workbook where the macro is written and displays it in a message box.
Sub ShowWorkbookPathInfo()
' Declare variables
Dim targetBook As Workbook
' Target the workbook where this macro is written
Set targetBook = ThisWorkbook
' --- Consideration for workbooks that have never been saved ---
If targetBook.Path = "" Then
MsgBox "This workbook has not been saved yet.", vbInformation
Exit Sub
End If
' --- Get information from each property ---
Dim folderPath As String
Dim fileName As String
Dim fullBookPath As String
' Get folder path (.Path)
folderPath = targetBook.Path
' Get file name (.Name)
fileName = targetBook.Name
' Get full path (.FullName) - Most useful
fullBookPath = targetBook.FullName
' --- Display results in a message box ---
Dim message As String
message = "[Workbook Path Information]" & vbCrLf & vbCrLf & _
"Folder Path (.Path):" & vbCrLf & folderPath & vbCrLf & vbCrLf & _
"File Name (.Name):" & vbCrLf & fileName & vbCrLf & vbCrLf & _
"Full Path (.FullName):" & vbCrLf & fullBookPath
MsgBox message, vbInformation, "Path Info"
End Sub
Key Points of the Code
1. Getting the Folder Path: .Path
The .Path property returns only the path of the folder where the file is saved.
Important: This path string does not include a backslash (\) at the end. Therefore, if you want to manually create a full path by combining .Path and .Name, you must add the separator yourself, like this: myBook.Path & “\” & myBook.Name.
2. Getting the File Name: .Name
The .Name property returns the file name including the extension, such as Report.xlsx or MacroBook.xlsm.
3. Getting the Full Path: .FullName (Recommended)
The .FullName property returns the complete full path, combining the folder path and the file name at once. In most cases, using this property is the easiest and most error-free method. If you need the full path, use .FullName instead of combining .Path and .Name.
4. Note on Unsaved Workbooks
If targetBook.Path = "" Then ...
For a workbook that has been newly created but never saved (e.g., “Book1”), the .Path property returns an empty string (""). You can use this behavior to check “whether this workbook has been saved or not.”
Summary
Make sure to use the three properties for getting workbook path information correctly:
- .Path: Use when you only need the folder location.
- .Name: Use when you only need the file name.
- .FullName: Use when you need the full path of the file. (This is the most convenient in most cases).
These properties are essential basics for handling the file system in VBA, such as saving/loading files or linking with other applications.
