ActiveX component can't create object when usi

2019-09-19 14:55发布

问题:

I am trying to use a custom class exported as a .tlb in vba. I have done the regasm stuff but I keep getting this error when I try to call a subroutine within the class:

Run-time error '429': ActiveX component can't create object

I've referenced the class in vba, I've built the class for 32bit and 64bit CPUs and nothing worked. Anyways, vba code:

    Sub test()
        Dim test As New Mail.Class1
        test.test
    End Sub

And the vb.net code:

    Imports System.Runtime.InteropServices
    Public Class Class1
        Public Sub test()
            MsgBox("hello")
        End Sub
    End Class

回答1:

That class won't be exposed to COM. Simplest way to do this is to Add New Item and select COM Class. This generates a Class skeleton that looks like this:

<ComClass(ComClass1.ClassId, ComClass1.InterfaceId, ComClass1.EventsId)> _
Public Class ComClass1

#Region "COM GUIDs"
    ' These  GUIDs provide the COM identity for this class 
    ' and its COM interfaces. If you change them, existing 
    ' clients will no longer be able to access the class.
    Public Const ClassId As String = "e19c541f-8eda-4fdd-b030-abed31518344"
    Public Const InterfaceId As String = "e2122f92-5752-4135-a416-4d499d022295"
    Public Const EventsId As String = "6b03de7e-90d7-4227-90ec-9121c4ce1288"
#End Region

    ' A creatable COM class must have a Public Sub New() 
    ' with no parameters, otherwise, the class will not be 
    ' registered in the COM registry and cannot be created 
    ' via CreateObject.
    Public Sub New()
        MyBase.New()
    End Sub

End Class

Also remember to check the "Make assembly COM Visible" in the Assembly Information dialog (Project properties>Application tab>Assembly Information)

Now when you compile this and call RegAsm, it should have an entry point for this class