Skip to content

Get Path to a File in the EXE's Folder with VBA

This VBA function retrieves the full path to a file located in the same directory as your application’s EXE. It is particularly useful for accessing external resources or companion files that you distribute alongside your protected workbook.

👉 Insert the following function into a VBA module:

Public Function PathToFile(ByVal Filename As String) As String
Dim XLSPadlock As Object
Dim exePath As String
Set XLSPadlock = Application.COMAddIns("GXLSForm.GXLSFormula").Object
exePath = XLSPadlock.PLEvalVar("EXEPath")
If exePath = "" Then
PathToFile = ""
Exit Function
End If
' PLEvalVar("EXEPath") already ends with a backslash, but do not rely on it
If Right$(exePath, 1) <> "\" Then exePath = exePath & "\"
PathToFile = exePath & Filename
End Function

You can then call the function:

Sub Test_File()
DoSomethingWith(PathToFile("data.xls"))
End Sub

👉 See Also: VBA API Cookbook & Recipes