连接C#和VBA不起作用 [英] Hooking up C# and VBA doesn't work

查看:122
本文介绍了连接C#和VBA不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我一直在尝试在C#中实现一个简单的函数addNumbers(double a,double b)并从VBA调用它。我在这里继续:扩展您的VBA C#,VB.NET或C ++ / CLI的代码 [ ^ ]



C#中的代码:



Hi,

I've been trying to implement a simple function addNumbers(double a, double b) in C# and call it from VBA. I've proceeded as here: Extend your VBA code with C#, VB.NET, or C++/CLI[^]

Code in C#:

using System
using System.Runtime.InteropServices; // Guid, ClassInterface, ClassInterfaceType
using System.Globalization; // CultureInfo
[Guid("97E1D9DB-8478-4E56-9D6D-26D8EF13B100")]
public interface IAddNumbers {
    double addNumbers(double a, double b)
};

[Guid("BBF87E31-77E2-46B6-8093-1689A144BFC6")]
[ClassInterface(ClassInterfaceType.None)]
public class TestClass : IAddNumbers {
    public double addNumbers(double a, double b) {
        return a + b;
    {
}



VBA:

首先我在注册表中注册了文件TestClass.dll然后添加了一个VBA环境中的参考,如上面的网站所示。问题是VBA根本看不到TestClass。我不能声明这样的变量:




VBA:
first I registered the file TestClass.dll in the registry and then added a reference in the VBA environment as indicated in the website above. The problem is that VBA does not see TestClass at all. I cannot declare a variable like this:

Dim addNumObj as TestClass
or
Dim addNumObj as new TestClass





有谁知道为什么这不起作用/ VBA没有看到.dll?

Guids是什么以及它们是如何生成的?我从网站上的例子中拿了Guids。



非常感谢任何帮助。



Does anyone know why this doesn't work/VBA is not seeing the .dll?
What are the Guids for and how are they generated? I took the Guids from the example on the website.

Many thanks for any help.

推荐答案

好像,您的.dll已准备好公开COM,但您没有提到项目的设置已正确设置。请阅读: Generics和Com Visible .NET库 [ ^ ]



如需了解更多信息,请参阅:

示例COM类(C#编程指南) [ ^ ]

将.NET组件公开给COM [ ^ ]



如果要在非开发机器上使用此.dll,则必须使用<在GAC中注册a href =https ://msdn.microsoft.com/en-us/library/h627s4zy%28v=vs.110%29.aspx> Regasm工具 [ ^ ]。
Seems, your .dll is ready to expose COM, but you didn't mention that project's settings have been properly set. Please, read this: Generics and Com Visible .NET libraries[^]

For further information, please see:
Example COM Class (C# Programming Guide)[^]
Exposing .NET Components to COM[^]

If you want to use this .dll on non-development machine, you have to register it in GAC using Regasm tool[^].


这篇关于连接C#和VBA不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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