简体   繁体   English

无法在VBA中使用我的简单VB类库

[英]Not able to use my simple VB class library in VBA

So this is my first post in this great community and I'm an absolute beginner, I'm sure I'll get good advice from here. 所以这是我在这个伟大社区的第一篇文章,我是一个绝对的初学者,我相信我会从这里得到很好的建议。

I mad this simple VB class library in Visual Studio 2017 我在Visual Studio 2017中使用这个简单的VB类库

Public Class Addition
    Public Function Add(No1 As Long, No2 As Long) As Long
        Return No1 + No2
    End Function
End Class

I made it COM visible and checked Register for COM interop. 我让它可见COM并检查注册COM互操作。 and build the project. 并构建项目。

In my access VBA project, I've added the reference to my Dll without a problem and put the following code behind the click event of a button. 在我的访问VBA项目中,我已经添加了对我的Dll的引用没有问题,并将以下代码放在按钮的单击事件后面。

Private Sub Command0_Click()
Dim myCalc As ShwDolphin.Addition
Set myCalc = New ShwDolphin.Addition
Dim Num As Long
Num = myCalc.Add(2, 5)
Debug.Print Num
End Sub

"ShwDolphin" is VB assembly name. “ShwDolphin”是VB程序集名称。

But I always get this error message "Runtime error 429 Active X component can't create an object" 但我总是收到此错误消息“运行时错误429 Active X组件无法创建对象”

Can you please tell me what I'm doing wrong here? 你能告诉我这里我做错了什么吗? This is driving me crazy. 这真让我抓狂。

Thank you very much. 非常感谢你。

Reading the comments, my guess is that you have a difference in what I call "bitness". 阅读评论,我的猜测是,你所谓的“位数”有所不同。 Default projects in Visual Studio are generally 32-bits. Visual Studio中的默认项目通常为32位。 If you build a 32-bit COM DLL, it can only be loaded by 32-bit processes. 如果您构建一个32位COM DLL,它只能由32位进程加载。 If your Office installation is 64-bit, it will never work. 如果您的Office安装是64位,它将永远不会工作。 What you will need to do is build/register it for 64-bits. 您需要做的是构建/注册64位。

Now, if you build for MSIL and not any specific processor (x86 or x64), you don't have to really rebuild for 64-bits. 现在,如果您为MSIL而不是任何特定处理器(x86或x64)构建,则不必为64位进行真正的重建。 All that is necessary is to register for 64-bits. 所有必要的是注册64位。 (The default build configuration is MSIL.) (默认构建配置是MSIL。)

So you need to use the 64-bit regasm in C:\\windows\\Microsoft.NET\\Framework64\\v4.0.30319\\regasm.exe 所以你需要在C:\\ windows \\ Microsoft.NET \\ Framework64 \\ v4.0.30319 \\ regasm.exe中使用64位regasm

use that regasm with options of /tlb /codebase "thenameofyour.dll" 使用带有/ tlb / codebase“thenameofyour.dll”选项的regasm

If you built x64 and Office is 32-bit, then do the opposite: use the 32-bit regasm, but odds are that you are using defaults which is 32-bit. 如果您构建了x64并且Office是32位,那么请执行相反的操作:使用32位regasm,但可能性是您使用的是32位默认值。

You can also look at this answer: activex can't create object by vbs but can by classic asp 你也可以看看这个答案: activex不能通过vbs创建对象,但可以通过经典的asp创建

So after much frustration, I was able to make it work by doing the following: 因此,经过多次挫折之后,我能够通过以下方式使其工作:

1- Make a new project with the same code (It was easier than removing previous dlls). 1-使用相同的代码创建一个新项目(这比删除以前的dll更容易)。

2- Disable "Register for COM interop" option in project properties of Visual Studio. 2-在Visual Studio的项目属性中禁用“注册COM互操作”选项。

3- Build project. 3-建立项目。

4- Use use the 64-bit regasm in C:\\windows\\Microsoft.NET\\Framework64\\v4.0.30319\\regasm.exe to register my assembly. 4-使用C:\\ windows \\ Microsoft.NET \\ Framework64 \\ v4.0.30319 \\ regasm.exe中的64位regasm来注册我的程序集。

5- Add reference to the generated tlb file in the VBA editor. 5-在VBA编辑器中添加对生成的tlb文件的引用。

Thanks to everyone for your generous help, thanks to this community. 感谢大家的慷慨帮助,感谢这个社区。 I don't know why I had to do this way, but I'm using 64-bit windows and 64-bit office. 我不知道为什么我必须这样做,但我使用的是64位Windows和64位办公室。 Also like @dee said adding interface was not necessary, it just worked. 也像@dee说添加界面是没有必要的,它只是起作用。

To be able to use custom .NET class in VBA, a *.dll must expose methods (and properties) to COM automation. 为了能够在VBA中使用自定义.NET类,* .dll必须将方法(和属性)公开给COM自动化。 What this means to you? 这对你意味着什么? You have to create an interface. 你必须创建一个接口。

Public Interface IAddition
    Function Add(No1 As Long, No2 As Long) As Long
End Interface

Public Class Addition
    Implements IAddition
    Public Function Add(No1 As Long, No2 As Long) As Long Implements IAddition.Add
        Return No1 + No2
    End Function
End Class

Usage: 用法:

Private Sub Command0_Click()
Dim myCalc As ShwDolphin.IAddition
Set myCalc = New ShwDolphin.Addition
Dim Num As Long
Num = myCalc.Add(2, 5)
Debug.Print Num
End Sub

For further details, follow the instruction provided in my past answer: Generics and Com Visible .NET libraries 有关更多详细信息,请按照我以前的答案中提供的说明: Generics和Com Visible .NET库

You might be interested in this article too: Extend your VBA code with C#, VB.NET, or C++/CLI 您可能也对这篇文章感兴趣: 使用C#,VB.NET或C ++ / CLI扩展您的VBA代码

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM