1

I have written a simple dll in VB.NET 2017, and I am trying to call it from VBA (Excel 2016). I have searched through Stack Overflow and MSDN, but despite many hits for this VBA error, I cannot find information related to a user defined dll that solves this particular problem.

I checked the box for the Register for COM Interop option when compiling the dll. I also verified that the Make assembly COM-Visible box was also checked.

Compilation was successful, and I was able to find my library (called TestLibrary2) in References inside the VBA editor. The class I created in it is called TestVBNET. I checked TestLibrary2 to include it in VBA, and then attempted to run the following code in VBA:

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

When I ran the sub, I got the pop-up dialog with message: Run-time error '429': ActiveX can't create object

Based on what I have read on MSDN and other sites, the expected result is that the TestLibrary2.TestVBNet object should be instantiated successfully.

The code below is the VB.NET code that I compiled to the dll. The GUID's were generated using guidgen at the Windows 10 command prompt.

    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

Any help and or references would be greatly appreciated. Thanks in advance.

EDIT (per comments below): I compiled the dll as admin, which in fact was required for the Register for COM Interop option to run. I also tried using regasm -- also as admin -- as follows, in the same directory as the compiled dll:

regasm /codebase /tlb TestLibrary2.dll 

The following was returned:

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))

I apologize for being a newb here with respect to registering COM libraries, but what this seemed to indicate was the dll was registered successfully, but could not be transferred to the GAC. It was my intention not to transfer it. When I tried loading it in VBA, the same error as above occurred.

4
  • Depending on the current user's priviledges it might be a permission issue. Please try to run the code as admin so you can rule out that reason. Commented Dec 3, 2017 at 9:47
  • 1
    Make sure the bitness of the dll matches the bitness of Excel. Try re-registering the assembly with regasm.exe Commented Dec 3, 2017 at 11:54
  • Did you try to load the dll in a simple VB.NET client? This may give you a hint Commented Dec 3, 2017 at 12:38
  • I'll try to load it in a simple VB.NET app -- good point. Thanks. Commented Dec 3, 2017 at 22:26

1 Answer 1

0

It turns out that the above code compiled to a dll that was indeed callable from a C# client. The problem with VBA, however, was that I neglected to put Implements ITestVBNet following the derived class declaration. In VB.NET, though, in addition to this, one must also explicitly indicate function overriding in the derived function declaration as well; viz (scroll all the way to the right to see the entire line),

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

By replacing the derived class above with the following, I was able to create an instance of the TestVBNet object in VBA and call the addTrig(.) method successfully:

<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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.