Home » excel » How to get Excel Template path from VSTO project

How to get Excel Template path from VSTO project

Posted by: admin April 23, 2020 Leave a comment

Questions:

I am working on a VSTO Excel 2010 Macro Enabled Template Project, that I plan to packaged with various other file/content/resources and installed (WiX toolset) onto the target machine.

It is intended that the user will click on a shortcut (e.g. from desktop) to the Excel Template, fire up Excel with the add-ins and then work from there. I also have some dotx reports in the installation directory that is to be triggered by VSTO Ribbon Button.

Question is, how can I find out the installation directory from the VSTO Ribbon button?

Since the new Excel Workbook came from an Excel Template, would be good if there is something like ThisWorkbook.Template.Path.

EDIT: I realize there are two parts to this question, the first part is answered here while the second needs clarification:

Suppose I have a template document mytemplate.xltm in C:\templates. I then double click on the file to open up Excel and a “New Workbook” is created by default from the file. Now how can I get back the path “C:\templates” from the “New Workbook”? I want to know where the file mytemplatex.xltm or better still, which file produced “New Workbook” from.

The answer to this question should give me “C\templates\mytemplate.xltm” in C# VSTO/Interop. Thanks.

How to&Answers:

Are you behind this Globals.ThisAddIn.Application.TemplatesPath?

Answer:

InstallLocation = Path.GetDirectoryName(new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath);