如何从VSTO项目获取Excel模板路径 [英] How to get Excel Template path from VSTO project

查看:1465
本文介绍了如何从VSTO项目获取Excel模板路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VSTO Excel 2010宏启用模板项目,我打算与各种其他文件/内容/资源进行打包,并将其安装在目标机器上。



用户将点击快捷方式(例如从桌面)到Excel模板,使用加载项启动Excel,然后从那里工作。我还在安装目录中有一些由VSTO Ribbon Button触发的dotx报告。



问题是,我如何从VSTO功能区中找到安装目录按钮?



由于新的Excel工作簿来自Excel模板,如果有类似ThisWorkbook.Template.Path的东西会很好。



编辑:我知道这个问题有两个部分,第一部分回答了这里,而第二个需要澄清:



假设我有一个模板文件mytemplate.xltm在C:\templates。然后双击该文件打开Excel,并从文件默认创建一个新建工作簿。现在我如何从新工作簿中获取路径C:\templates?我想知道文件mytemplatex.xltm或更好的文件,哪个文件生成新工作簿。



这个问题的答案应该是给我C \ C#VSTO / Interop中的templates\mytemplate.xltm。谢谢。

解决方案

  InstallLocation = Path.GetDirectoryName(new Uri(System.Reflection.Assembly 。.GetExecutingAssembly()基本代码).LocalPath); 


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.

解决方案

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

这篇关于如何从VSTO项目获取Excel模板路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆