SYMPTOMS
When you set a public object variable of a Microsoft
Component Object Model (COM) component in Microsoft Visual Basic .NET or in Microsoft Visual Basic 2005, you may
receive the following error message:
An unhandled
exception of type 'System.Runtime.InteropServices.COMException' occurred in
microsoft.visualbasic.dll
Additional information: Object variable or
With block variable not set
CAUSE
This behavior can occur if all the following conditions are
true:
- You define a public variable in the COM
component.
- The variable is of the Object type.
- You reference this COM component by using late binding in
Visual Basic .NET or in Visual Basic 2005.
When you access the public object by using late binding, Visual
Basic .NET or Visual Basic 2005 does not correctly set the
BindingFlags enumeration.
The
BindingFlags enumeration is used to specify the flags that control binding and
the way in which the search for members and types is conducted by reflection.
WORKAROUND
To work around this problem, use one of the following
methods.
Use early binding in Visual Basic .NET or in Visual Basic 2005
Use early binding in the Visual Basic .NET or Visual Basic 2005 client application.
This is the easiest solution because you do not have to rebuild the COM
component. To use early binding in Visual Basic .NET or in Visual Basic 2005, define the object
variable by using the type name from the interop assembly that is generated
when you set the reference to the component. Do not define it by using the
Object type.
When you use the Visual Basic .NET or Visual Basic 2005 code example
that is in the "Steps to reproduce the problem" section, use the following code
example to define the object variable.
Dim obj1 As Project1.Class1
Set obj1 = New Project1.Class1
Do not use the following code example:
Dim obj1 As Object
Set obj1 = New Project1.Class1
Define a public property procedure in the COM component
Modify the Microsoft Visual Basic 6.0 COM component to use a
public property procedure to set and to return the object. Do this instead of
using a global
Object variable. This solution is more difficult to implement because
you must modify the original Visual Basic 6.0 component. If you do this, you
may have to break the binary compatibility of the component.
When you
use the Visual Basic 6.0 code example that is in the "Steps to reproduce the
problem" section, use the following code example to define a public property
procedure within the
Class1 class to set and to retrieve the object variable.
Option Explicit
Private obj1 As Object
Public Property Get Obj() As Object
Set Obj = obj1
End Property
Public Property Let Obj(Object As Object)
Set obj1 = Object
End Property