Article ID: 316478 - Last Review: May 18, 2007 - Revision: 4.3 BUG: "Object variable or With block variable not set" error message when you access a public object variableThis article was previously published under Q316478 On This PageSYMPTOMS 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:
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 2005Use 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. Define a public property procedure in the COM componentModify 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. STATUSMicrosoft has confirmed that this is a bug in the Microsoft
products that are listed in the "Applies to"
section. MORE INFORMATIONSteps to reproduce the problemCreate an ActiveX DLL project in Visual Basic 6.0
Create the Visual Basic .NET or Visual Basic 2005 application
| Article Translations
|
Back to the top
