VBA中使用的C#/。net DLL

时间:2018-10-18 12:19:09

标签: c# .net excel-vba dll com

我尝试使用可以在Visual Basic(Excel)中运行的.net Framework创建C#库(.dll)。我在Visual Studio上开发。

经过一些研究,我发现我们可以使用COM接口在DLL和VBA之间进行通信。 我指的是Calling a .net library method from vba

我无法从Visual Studio中检查“注册COM Interop”构建选项。此选项被禁用。 option_disable

然后,当我使用“ InterfaceType(ComInterfaceType.InterfaceIsDual)”时,Visual Studio告诉我该参数已被弃用。 argument obsolete

最后,我无法从VBA代码中调用“ HelloWorld”函数。

精度:

  • 我的C#/。net程序构建成功

  • 我使用此命令注册DLL(无错误注册)

    regasm /s /codebase /tlb ClassLibrary3.dll
    

我的完整C#/。net程序:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace ClassLibrary3
{
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    [ComVisible(true)]
    public interface _Test
    {
        string HelloWorld();
    }

    [ClassInterface(ClassInterfaceType.None)]
    [ComVisible(true)]
    public class Test : _Test
    {
        public string HelloWorld() {
           return "Hello, World! ";
        }
    }
}

我可以使用COM以外的其他选项吗?

或者我的程序出了什么问题?

谢谢您的帮助:)

巴斯蒂安

1 个答案:

答案 0 :(得分:0)

不确定您需要什么,但是在我的访问项目中,我能够从互操作服务使用DLLImport,正如我所知道的,将.dll引用添加到您的项目后,您可以像这样使用它。 https://www.barcoderesource.com/dllimportvb.shtml您可以检查此链接吗?

网站示例:

<DllImport("connectcodefont.dll")> _
Public Shared Function Encode_Code39(ByVal instr As String, ByVal checkdigit As Integer, ByVal outstr As System.Text.StringBuilder, ByVal outstrmax As Integer) As Integer
End Function