I have spent the last 2 days trying to get a basic VB.NET dll that created to work in VBA. I have read every single post on this sight but i keep getting the same error.
- I create a VB.NET class (I run Visual Studio’s as administrator).
My class
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Runtime.Interposervices
Imports System.Linq
Public Class TestClass
Function TestMethod(ByVal input as String)
Return "Hello" & input
End Function
End Class
- I set “Make assembly comes visible”
- I set “Register for COM interop”
-
I build and release the dll
-
I open Excel and add a reference to the dll.
Everything works great so far
-
In a sheet I add the below code
Public Sub test() Dim a As TestClass 'Note: Auto fills in once i start typing it in so i know that the DLL there Set a = New TestClass MsgBox (a.TestMethod("World") End Sub
When i try running the code I get the following error
“ActiveX component can’t create object”
I have tried this using 2 computers: Win7 64 bit, Excel 2010 64 bit ,VS 2010 and Win7 64 bit, Excel 2013 , VS 2010 64 bit with know luck. I have read people receiving the same error but I nothing seems to work.
Does anyone see any mistakes in my method?
-add a namespace to your code.
-make the function as public
.
-make it com visible and set ‘Register for COM interop’ as you mentioned.
-your code should be like:
Namespace X
Public Class TestClass
Public Function TestMethod(ByVal input As String)
Return "Hello" & input
End Function
End Class
End Namespace
-build your project, you will find a yourProjName.tlb file in \bin\debug or \bin\release.
-open Excel and add a reference to the yourProjName.tlb not to dll.
-modify your code to be like:
Sub test()
Dim testObj As New TestClass
Dim myStr As String
myStr = testObj.TestMethod("ssssss")
MsgBox myStr
End Sub
This worked for me.
EDIT #1
-I am working on Windows 7 32 bit, Office 2010 professional 32 bit with SP2, Visual Studio 2010 with framework 4.
-configure your project for x64 : from Visual Studio ->Go to the Build Menu ->click Configuration Manager -> Under Active solution platform, click New, then add one with x64 as the platform. Use this platform to compile for x64 , link .
-It is preferred to sign your assembly (very easy) : Project Properties > Signing > Sign the assembly > New -> Enter the name of the file as myKey.snk , no need for password.
-build your project.
– if you are working on the same machine that visual studio in running, then no need to register your assembly, because VS will do since we set ‘Register for COM interop’.
-for other client machines where VS in not running, you must register your assembly, take the DLL file only, run cmd as administrator, run the following command:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm /codebase /tlb "D:\out\VB_DLL_001.dll"
Note that this command will generate the *.tlb file for you in the same folder.
-add your dll to Global Assembly cache by running the following command in cmd (that is running as administrator) :
"c:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\gacutil.exe" /i "d:\out\VB_DLL_001.dll"
-from Excel add reference to the generated tlb file : VBA Editor -> Tools -> References -> Browse -> Select the tlb file -> OK . Then run the previous code, I hope it will run successfully .
EDIT #2
Based on the comment, It works by building the DLL for x64 architecture, So, no need for other steps in EDIT #1.