0

I have a c# dll that I created in Visual Studio, and I want to import a method from it in Excel VBA.

So, I use the Declare statement approach since I cannot register the library (company policies)

Private Declare PtrSafe Sub ExcelTest Lib "c:\CorrectPath\MyDll.dll" ()

Sub TestSub()
    Call ExcelTest
End Sub

While my DLL has the following:

namespace Default_Namespace_of_the_Project
{
    public static class ExcelTestClass //among many other classes, but this is the target one
    {
        public static void ExcelTest()
        {
            MessageBox.Show("Test Ok");
        }
    }
}

And yet, Excel keeps telling me I got a runtime error 453:

Specified DLL function not found

What is going on here? Why doesn't excel find the function? Is there a special attribute I need to add to the function? Does it need to be somewhere special in the DLL? How can I solve this?

Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
  • `I use the Declare statement approach since I cannot register the library (company policies)` - you are not to choose here. Either the library is COM, in which case you must register it, or it's native, in which case you must use Declare. The company policy has no role here. A C# library is neither COM nor native. If you want it to export a native function from it, use [`DllExport`](https://github.com/3F/DllExport) or a similar solution. – GSerg Mar 28 '22 at 14:15
  • @GSerg, thank you. Isn't there any alternative to use a C# DLL as source in VBA? (Wihtout the extra tools) – Daniel Möller Mar 28 '22 at 14:36
  • [Reg-free COM](https://stackoverflow.com/q/15503117/11683)? – GSerg Mar 28 '22 at 16:14

0 Answers0