在Microsoft Access VBA中使用.Net DLL

JMK*_*JMK 7 c# vb6 dll vba typelib

好吧,我有一个用C#编写的程序集,使用Visual Studio 2010.

这个程序集包含一个类,其中包含一个返回单词Result的方法,代码如下:

using System.Runtime.InteropServices;

namespace TestDLL
{
    public class Class1
    {
        [ComVisible(true)]
        public string TestMethod()
        {
            return "Result";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

属性窗口的"构建"选项卡中的输出部分如下所示:

Visual Studio输出窗口

当我单击Build时,我得到一个DLL文件和一个TLB文件.我只需浏览它就可以将此TLB文件添加到Microsoft Access.

VBA参考窗口

现在,在Access中我有一个按钮和一个标签.我想让我的标签的Caption属性等于testMethod的结果.我想我需要做类似下面的事情,但我不确定,任何帮助都会非常感激:

Private Sub btnMain_Click()

    Dim tm As TestDLL
    Dim foo As String

    foo = tm.testMethod

    lblBarr.Caption = foo

End Sub
Run Code Online (Sandbox Code Playgroud)

谢谢

Arv*_*rvo 8

也许接下来会有效:

Private Sub btnMain_Click()

    Dim tm As TestDLL.Class1
    Dim foo As String

    Set tm = New TestDLL.Class1
    foo = tm.testMethod

    lblBarr.Caption = foo

End Sub
Run Code Online (Sandbox Code Playgroud)