在VBA中引用程序集,而无需将该程序集注册为COM组件. [英] Reference assembly in VBA without registering the assembly as a COM component.

查看:114
本文介绍了在VBA中引用程序集,而无需将该程序集注册为COM组件.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用VBA与.NET编写的库进行交互的应用程序.然后将这些库的程序集注册为COM组件.我试图摆脱COM注册部分.本质上,无论程序集是否已注册,我都希望该应用程序只要存在程序集就能够运行.

据我所知,这似乎是有可能的.我在此处

I have an application that uses VBA to interface with libraries written in .NET. The assemblies for these libraries is then registered as COM components. I am trying to get rid of the COM registration part. Essentially, I want the application to be able to function as long as the assembly is present, regardless of whether or not the assembly has been registered.

From what I can gather, it seems to be possible. I found some promising looking code samples here and here. From these, I have tried the following:

Application.VBE.ActiveVBProject.References.AddFromFile "C:\SomeDirectory\SomeAssembly.dll"




and

Dim VBAEditor As VBIDE.VBE
Dim VBProj As VBIDE.VBProject

Set VBAEditor = Application.VBE
Set VBProj = VBAEditor.ActiveVBProject

VBProj.References.AddFromFile "C:\SomeDirectory\SomeAssembly.dll"



都行不通.第一个告诉我期望一个变量名",而第二个则告诉我类型标识符无效".


所以,有人知道我在做什么错吗?


另外,我是VBA的新手.因此,如果可能,请保持简单的解释.



Neither works. The first one tells me "Expecting a var name", while the second one complains that "Type identifier is invalid".


So, anyone know what I''m doing wrong?


Also, I''m VERY new to VBA; so, if possible, please keep the explanations simple.

推荐答案

存在问题....主要与安全性有关...当您尝试混合使用VBA时和.net代码. COM包装器为您封送了所有这些信息,这就是为什么它在注册后将起作用的原因.另外...我认为AddFromFile方法正在寻找RegisterCOMComponent入口点,该入口点在直接的.net程序集中不存在,但如果该程序集已标记为用于COM interop,则可以. (尝试在常规的.net程序集上运行regsvr32.您应该收到有关缺少入口点的消息)

您无需谈论主机应用程序是什么,因此很难说是否可以使用纯净的.net解决方案.如果它是MS Office 2007或更高版本,则可以使用Office组件在.NET中进行所有操作(以及更多操作).另外,它将适合于安全方案.例如,如果您对外接程序进行代码签名,则在尝试运行外接程序时不会提示用户输入安全警告.

纯粹的.net解决方案还可以让您定义接口并在运行时发现/加载程序集.方便地换出一些功能组件,而其余的应用程序保持不变.
There are problems.... mostly related to security... when you try to mix & match VBA and .net code. The COM wrapper marshals all of this for you which is why it will work if it is registered. Also... I think that the AddFromFile method is looking for the RegisterCOMComponent entry point which doesn''t exist in a straight .net assembly but does if the assembly has been marked for COM interop. (Try running regsvr32 on a regular .net assembly. You should get a message about the entry point missing)

You don''t talk about what the host app is so it''s hard to say if a pure .net solution would be available. If it is MS Office 2007 or higher, you can do everything (and more) in .NET using the Office components. Plus, it will fit into the security scheme. For example, if you code-sign the add-in, users will not get prompted for security warnings when trying to run the add-in.

A pure .net solution would also let you define interfaces and discover/load assemblies at runtime. Handy for swapping out some functional component while leaving the rest of the app the same.


这篇关于在VBA中引用程序集,而无需将该程序集注册为COM组件.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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