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

问题描述:

我已经写了一个简单的DLL在VB.NET 2017中,我是试图从VBA调用它(Excel 2016)。我已经通过堆栈溢出和MSDN进行了搜索,但尽管此VBA错误点击很多,我无法找到与用户定义的dll相关的信息来解决此特定问题。vba运行时错误'429':ActiveX不能创建对象,用户定义vb.net dll(VB.NET 2017,Excel 2016)

我在编译dll时勾选了注册COM Interop选项。我也验证了使装配COM-Visible框也被检查。

编译成功,我能够在VBA编辑器内的References中找到我的库(名为TestLibrary2)。我在其中创建的班级名为TestVBNET。我检查TestLibrary2将其包括在VBA,然后再尝试运行VBA下面的代码:

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

当我跑分,我得到的消息弹出对话框:运行时错误“429”:基于什么我已经阅读MSDN和其他网站

的ActiveX不能创建对象,预期的结果是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作为管理员,这实际上是需要为注册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中加载它时,发生了与上面相同的错误。

+0

根据当前用户的权限,这可能是一个权限问题。请尝试以管理员身份运行代码,以便排除该原因。 – peakpeak

+1

确保dll的位数与Excel的位数相匹配。尝试使用regasm.exe重新注册程序集 –

+0

您是否尝试在简单的VB.NET客户端中加载dll?这可能会给你一个提示 –

事实证明,上面的代码编译为一个dll,确实可以从C#客户端调用。然而,VBA的问题是我忽略了在派生类声明之后放置了Implements ITestVBNet。但在VB.NET中,除此之外,还必须在派生函数声明中明确指示函数重写;即(滚动一路看到整个线的右边),

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