Relative file path VBA

Schadenfreud

New Member
Joined
Jan 22, 2014
Messages
29
I have a WinForm, which I'm calling via VBA's Shell command:
Code:
Sub Button1_Click()
    Shell "C:\\TemplateFuncitons.exe " + ActiveWorkbook.FullName, 1
End Sub
I made this module an Excel Add-in and added it as a button in the quick access toolbar. Now, the idea is for the WinForm.exe and the Excel.xla to always be in the same directory (go hand in hand). I want to make a relative path for the .exe which will be the path to the folder where the .xla is + the string "TemplateFunctions.exe" in the end.

Does anyone have any idea how/if I can achieve this?
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
You can get the path of the addin with ThisWorkbook.Path


Code:
Sub Button1_Click()
    Dim sEXEPath As String
    Dim sCmd As String
    sEXEPath = ThisWorkbook.Path & "\TemplateFunctions.exe"
    sCmd = sEXEPath & " " & ActiveWorkbook.FullName & ", 1"
    Shell sCmd
End Sub
 
Upvote 0
This code gives me a file not found error. Last time I fixed it with making all the '\' into '\\', is there any way to do this here, without, for example, using a regex?

Also, would ThisWorkbook.Path get me the path to the .xlsx file invoking the module or the path to the .xla which contains the module?
 
Last edited:
Upvote 0
It would get the path of the xla that contains the module.

You could try surrounding the whole exe path with double quotes.

Code:
Sub Button1_Click()    Dim sEXEPath As String
    Dim sCmd As String
    sEXEPath = Chr(34) & ThisWorkbook.Path & "\TemplateFunctions.exe" & Chr(34)
    sCmd = sEXEPath & " " & ActiveWorkbook.FullName & ", 1"
    Shell sCmd
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,928
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top