Aller au contenu

Obtenir le chemin d'un fichier dans le dossier de l'EXE avec VBA

Cette fonction VBA récupère le chemin complet vers un fichier situé dans le même répertoire que l’EXE de votre application. Elle est particulièrement utile pour accéder à des ressources externes ou à des companion files que vous distribuez avec votre classeur protégé.

👉 Insérez la fonction suivante dans un module VBA :

Public Function PathToFile(ByVal Filename As String) As String
Dim XLSPadlock As Object
Dim exePath As String
On Error GoTo Err
Set XLSPadlock = Application.COMAddIns("GXLSForm.GXLSFormula").Object
exePath = XLSPadlock.PLEvalVar("EXEPath")
' Use Application.BuildPath to correctly join the path and filename
PathToFile = Application.BuildPath(exePath, Filename)
Exit Function
Err:
PathToFile = ""
End Function

Vous pouvez ensuite appeler la fonction :

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

👉 Voir aussi : VBA API Cookbook & Recipes