Question summary
When I use VBA's VarType
function, passing it an instance of the Object
class available in a mscorlib.dll library reference (a .NET library reference), the value returned is 8
.
According to VBA documentation here, this means the object is a string. This seems ridiculous.
My question is why is the VarType
function returning a string-type value for instances of this Object
class from the .NET-library VBA reference? Is it a bug?
Background information
I suspect that the fact that VBA's VarType
function is saying a certain COM object is a string, may be why I'm having problems using the DispCallFunc
function on certain methods of certain COM objects. The COM objects are COM versions of .NET objects, made available through the .NET framework.
I'm using the mscorlib.dll VBA reference to get early-binding functionality for these objects. The reference refers to version 4.0.30319 of the .NET framework. On my computer, the type library for the reference is stored at:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.tlb.
It is looking like when the object method specifies that an argument or return value be of type System.Object
, DispCallFunc
fails to work for me. Anyway, that is a separate problem that is circumstantial to the problem this question is dedicated to.
At the time of writing, I'm running the latest version of Excel (Version 1903, Build 11425.20202). My operating system is Windows 8.1.
When I use the VarType
function with instances of other classes from the mscorlib.dll library, I sometimes get return values of 9
& 13
(VbVarType.vbObject
& VbVarType.vbDataObject
constants) which seems correct.
I researched on the internet to see whether anyone else had encountered the problem but couldn't find anything.
Code that can be used to reproduce problem
Dim o As mscorlib.Object
Set o = New mscorlib.Object
Debug.Print "TypeName(o) = " & TypeName(o) ' TypeName function seems to work correctly.
Debug.Print "o.Equals(o) = " & o.Equals(o) ' System.Object.Equals method is working.
Debug.Print "VarType(CVar(o)) = " & VarType(CVar(o)) ' IMPORTANT LINE
' VBA VarType function says o is string (type 8) but it isn't?!
Debug.Print "VbVarType.vbString = " & VbVarType.vbString
I expected VarType(CVar(o))
to return 9
, 13
or some other appropriate integer. Instead, it returned 8
which does not seem appropriate at all (8
represents strings.)