0
votes

I have a COM DLL which has been written in C#. I am using the DLL from VBA (MS Access 2010).

Some functions in the DLL contain out parameters which are not working correctly when used from VBA: VBA gives error "Object reference not set to an instance of an object" when the function is called. All other functions in the DLL class are working correctly.

VBA code:

Dim par0 As String
par0 = "test"

Dim outPar1 As Boolean
Dim outPar2 As myDllNamespace.Foo
Set outPar2 = New myDllNamespace.Foo
Dim outPar3 As String

dllClassInstance.Bar par0 outPar1 outPar2 outPar3

C# COM DLL code (Class for dLLClassInstance):

[ProgId("myDllNamespace.DllTestClass")]
[Guid( ... ),
 ClassInterface(ClassInterfaceType.None),
 ComDefaultInterface(typeof(IDllTestClass))]
public class DllTestClass : IDllTestClass
{
    public void Bar(string filter, out bool success, out Foo entries, out string html) { ... }
}

C# COM DLL interface code (Interface for the class):

[Guid( ... ), InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IDllTestClass
{
    void Bar(string filter, [Out] out bool success, [Out] out Foo entries, [Out] out string html);
}

Why does VBA throw the "Object reference not set to an instance of an object" error even all the variables have a value and the three last parameters are out parameters anyway?

2
That is a .NET exception, NullReferenceException, not a VBA error. It is surely generated in the { ... } code. Use the debugger to step through the code or stop when the exception is thrown. Project > Properties > Debug tab > Start external program.Hans Passant
Although it should not be the cause of your specific issue, be aware that neither VBA nor VB6 actually support [out] parameters. They fake it best they can by treating them as [in, out]. As you can imagine, this handicapped treatment ranges from "ok", through "resource-leaking' all the way into "crashing", depending on exact usage. If your COM client is VB6 or VBA, use [in, out] instead, to make sure that .NET/COM Interop is in the joke.Euro Micelli

2 Answers

0
votes

I'm not a VB.NET guy, but all I can tell that VB.NET doesn't have out equivalent. The possible approach is to use < Out > attribute when you import the Bar method. It then depends how compiler sets it up.

0
votes

As Hans Passant said, this was not a VBA error but an error in the .NET COM library instead.

By fixing the COM library I was able to get the out parameters to work as they should.

Thank you for your support!