在Microsoft Access VBA中使用.Net DLL

时间:2012-01-13 10:22:54

标签: c# vba dll vb6 typelib

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

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

using System.Runtime.InteropServices;

namespace TestDLL
{
    public class Class1
    {
        [ComVisible(true)]
        public string TestMethod()
        {
            return "Result";
        }
    }
}

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

Visual Studio Output Window

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

VBA Reference Window

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

Private Sub btnMain_Click()

    Dim tm As TestDLL
    Dim foo As String

    foo = tm.testMethod

    lblBarr.Caption = foo

End Sub

三江源

1 个答案:

答案 0 :(得分: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
相关问题