Directory of where Office solution has been placed

72 views Asked by At

I am trying to access the string which represents the path of the current directory my application has been placed in. I have an excel templates made in visual studio which needs to be populated by the names of files in the current directory (which the document have been placed) and its sub-directories. I cannot find anywhere a solution which actually works, I have found some attempts at a solution: get path for my .exe and various others , which do not work. I currently have the user enter the desired path but I would like to automatically find the path or find an alternative way to collect these names.

2

There are 2 answers

1
dotNET On BEST ANSWER

This depends upon how you're distributing your solution. Office add-ins and templates, when distributed through the standard ClickOnce mode, end up in a cryptically-named folder (inside another cryptically-named folder) that lives somewhere in AppData folder. You'll not be able to use the normal ways of getting the path of currently existing application, as they'll most probably return the path of EXCEL.EXE itself. This SO post provides a good way of getting the current folder in VSTO solutions.

Alternately you can consider using MSI for distributing your template/add-in. This is a longer path and has many caveats, but you'll end up installing your add-in/template in Program Files folder.

0
tbm0115 On

I'm not as familiar with c#, but here's what I do in vb:



    Dim dir as string = Application.StartupPath

You can do the same thing in excel VBA:



    Dim dir as string = Application.ActiveWorkbook.Path