将用户定义的函数添加到Visual Studio Excel加载项 [英] Add user defined function to Visual Studio Excel Add-in

查看:258
本文介绍了将用户定义的函数添加到Visual Studio Excel加载项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在visual studio中,我有一个Excel 2010加载项目。如何让该项目创建以下模块:

In visual studio I have an Excel 2010 Add-in project. How can I have that project create the following module:

我知道我可以使用该模块保存该工作簿,然后将其与我的添加一起使用。如果我可以使用我的add-在创建该模块...

I know I can save that workbook with that module then use it with my add in. It will be nice if I can have my add-in create that module...

推荐答案

可以创建模块。但是,为了使其工作,必须在Excel中选择信任对VB项目模型的访问设置。如果未选择信任设置,则会抛出访问被拒绝的错误。

It is possible to create the module. However for this to work the setting to "Trust access to the VB Project model" must be selected in Excel. It throws an error that access is denied if the trust setting is not selected.

using Excel = Microsoft.Office.Interop.Excel;
using VB = Microsoft.Vbe.Interop;

Excel.Application eApp = new Excel.Application();

eApp.Visible = true;
Excel.Workbook eBook = eApp.Workbooks.Add();

VB.VBProject eVBProj = (VB.VBProject)eBook.VBProject;
VB._VBComponent vbModule = eVBProj.VBE.ActiveVBProject.VBComponents.Add(VB.vbext_ComponentType.vbext_ct_StdModule);

String functionText = "Function MyTest()\n";
      functionText += "MsgBox \"Hello World\"\n";
      functionText += "End Function";

vbModule.CodeModule.AddFromString(functionText);

这篇关于将用户定义的函数添加到Visual Studio Excel加载项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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