从VBA(Excel)访问VSTO应用程序 - 插件类型 [英] Accessing a VSTO application-addin types from VBA (Excel)

查看:477
本文介绍了从VBA(Excel)访问VSTO应用程序 - 插件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个用于Excel的VSTO应用程序加载项(而不是一个文档插件),我们希望将一个事件暴露给VBA代码,以便当该事件在加载项中触发时,VBA宏可以执行一些操作。我如何获得VBA代码能够订阅VSTO应用程序中定义的事件 - 加载项?



我认为,由于加载在Excel进程,这不应该太棘手,但还没有找到方法。



BTW,使用VS 2008和Excel 2007。



谢谢!

解决方案

VSTO不是通常可以从其他DLL调用的DLL。 VSTO基本上是COM暴露的.NET代码,在一个独立的AppDomain内操作的包装器中运行。虽然您的VSTO加载项在技术上是一个正在加载到Excel的DLL,但它的操作更像是顶级EXE,而不是暴露给其他来电者的DLL库。



个人而言,我将创建一个标准的.NET程序集 - 也就是避免使用VSTO,并使用正确的属性将其暴露给COM。这个过程在这里有很好的解释: COM Interop Exposed - 第2部分,在将.NET事件暴露给COM。



如果您真的坚持使VBA能够调用VSTO,则必须通过 Office.COMAddIn.Object 属性,通过覆盖 RequestComAddInAutomationService 方法启用。该过程在文章 Andrew Whitechapel的VSTO加载项,COMAddIns和RequestComAddInAutomationService



我希望这有帮助!



Mike


We have a VSTO application-addin (not a document-addin) for Excel, and we want to expose an event to VBA code so that the VBA macro can do some action when this event fires in the addin. How can I get the VBA code to be able to subscribe to an event defined in the VSTO application-addin?

I'd think that since the addin is loaded in the Excel process, this shouldn't be too tricky, but haven't found a way yet.

BTW, using VS 2008 and Excel 2007.

Thanks!

解决方案

VSTO is not a DLL that can generally be called from other DLLs. VSTO is basically COM-exposed .NET code operating from within a wrapper operating from within a separate AppDomain. Although your VSTO add-in is technically a DLL that is being loaded into Excel, it operates more like a top-level EXE rather than as a DLL library exposed to other callers.

Personally, I would create a standard .NET assembly -- that is, avoid using VSTO for this -- and expose it to COM using the correct attributes. The process is well explained here: COM Interop Exposed - Part 2, under the section titled "Exposing .NET Events to COM".

If you really insist on enabling VBA to be able to call VSTO, then you'll have to operate via the Office.COMAddIn.Object property which is enabled by overriding the RequestComAddInAutomationService method. The process is discussed in detail in the article VSTO Add-ins, COMAddIns and RequestComAddInAutomationService by Andrew Whitechapel.

I hope this helps!

Mike

这篇关于从VBA(Excel)访问VSTO应用程序 - 插件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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