vba 运行时错误"429":ActiveX 无法创建对象,用户定义 vb.net dll (VB.NET 2017、Excel 2016)



我在vb.net 2017中写了一个简单的dll,我试图从vba称呼它(Excel 2016)。我已经搜索了堆栈Overflow和MSDN,但是尽管此VBA错误有很多命中,但我找不到与用户定义的DLL有关的信息。

我在编译DLL时检查了寄存器的寄存器选项。我还验证了还检查了制作组件com-visible 框。

汇编成功,我能够在VBA编辑器内的参考文献中找到我的库(称为 testLibrary2 )。我创建的类称为 testvbnet 。我检查了 testLibrary2 将其包括在VBA中,然后尝试将以下代码运行在VBA中:

Public Sub mySub()
    Dim myTest2 As TestLibrary2.TestVBNet
    Set myTest2 = New TestLibrary2.TestVBNet
End Sub

当我运行子时,我获得了带有消息的弹出对话框:运行时错误'429':ActiveX无法创建对象

根据我在MSDN和其他站点上阅读的内容,预期的结果是 testLibrary2.testvbnet 对象应成功实例化。

下面的代码是我编译到DLL的VB.NET代码。GUID是在Windows 10命令提示下使用GUIDGEN生成的。

    Imports System.Runtime.InteropServices 
    <Guid("2CEBF80D-D5FC-4C52-BCF1-E2F076318240")>
    Public Interface ITestVBNet
        Function addTrig(x As Double, y As Double) As Double
    End Interface
    <Guid("60983277-A724-4CDD-8ACE-B94CF9546F43")>
    <ClassInterface(ClassInterfaceType.None)>
    Public Class TestVBNet
        Function addTrig(x As Double, y As Double) As Double
            Return Math.Sin(x) + Math.Cos(x)
        End Function
    End Class

任何帮助和或参考将不胜感激。预先感谢。

编辑(下面的注释):我将DLL编译为Admin,实际上com Interop 选项的寄存器需要运行。我还尝试使用regasm(也作为管理员),如下所示,与编译DLL相同的目录:

regasm /codebase /tlb TestLibrary2.dll 

返回以下内容:

RegAsm : warning RA0000 : Registering an unsigned assembly with /codebase can cause your assembly to interfere with other applications that may be installed on the same computer. The /codebase switch is intended to be used only with signed assemblies. Please give your assembly a strong name and re-register it.
Types registered successfully
RegAsm : error RA0000 : An error occurred while saving the exported type library: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

我对在此处注册的COM库中成为新手表示歉意,但这似乎表明是DLL已成功注册,但无法转移到GAC。我打算不转移它。当我尝试将其加载到VBA中时,与上述相同的错误。

事实证明,上述代码已将其编译到C#客户端确实可调用的DLL。但是,VBA的问题在于,我忽略了在派生的类声明之后放置工具Itestvbnet。但是,在VB.NET中,除此之外,还必须明确指示在派生功能声明中覆盖的功能;viz(一直向右滚动以查看整行),

Public Function addTrig(x As Double, y As Double) As Double Implements ITestVBNet.addTrig

通过以下替换上面的派生类,我能够在VBA中创建一个testvbnet对象的实例,并成功调用AddTrig(。)方法:

<Guid("60983277-A724-4CDD-8ACE-B94CF9546F43")>
<ClassInterface(ClassInterfaceType.None)>
Public Class TestVBNet
    Implements ITestVBNet  
    Public Function addTrig(x As Double, y As Double) As Double Implements ITestVBNet.addTrig
        Return Math.Sin(x) + Math.Cos(x)
    End Function
End Class

最新更新