BUG: You receive a COMException exception when you pass late-bound parameters to methods of Office objects in Visual Basic 2005 or in Visual Basic .NET (834063)
The information in this article applies to:
- Microsoft Visual Basic 2005
- Microsoft Visual Basic .NET (2003)
- Microsoft Visual Basic .NET (2002)
SYMPTOMSIn a Microsoft Visual Basic 2005 or Microsoft Visual Basic .NET application, when you pass
late-bound parameters to methods of Microsoft Office objects, you may receive COMException exceptions. For example, when you pass the Name property of a late-bound UserProperty object to the UserProperties.Items method, you may receive the following error message: An unhandled exception of type
'System.Runtime.InteropServices.COMException' occurred in
microsoft.visualbasic.dll
Additional information: Property is
read-only. Note Similar symptoms may also occur when you call methods of other
Microsoft Office objects. CAUSEIf you pass a property of an object to a method by reference
and the property has a set accessor, Visual Basic calls the set accessor to set the property to the value that the method
returns. At compile time, if you pass a property of a late-bound object, the
Visual Basic compiler cannot determine whether the property is passed by
reference. Also, the Visual Basic compiler cannot determine whether the
property has a set accessor. Therefore, the Visual Basic compiler permits the
late binder to determine these details at run time.
However, if the
relevant objects are Component Object Model (COM) objects at run time, the late
binder cannot obtain sufficient information about these details. The late
binder uses managed reflection to try to determine these details. The late
binder assumes that such method calls involve a ByRef parameter and that the property that you pass has a set accessor. If the property that you pass does not have a set accessor, the Microsoft .NET Framework generates a MissingMethodException exception. The late binder handles the MissingMethodException exception.
The .NET Framework generates a MissingMethodException exception when the call to a method of a Microsoft Office object
returns a HRESULT value of COR_E_MISSINGMETHOD. The Microsoft Office object is
a COM object. Visual Basic .NET incorrectly assumes that all the methods of
Microsoft Office objects return COR_E_MISSINGMETHOD if the property that you
pass does not have a set accessor. However, not all the methods of Microsoft Office
objects return COR_E_MISSINGMETHOD if the property that you pass does not have
a set accessor. Therefore, the behavior that is mentioned in the
"Symptoms" section of this article occurs if you pass a property that does not
have a set accessor, such as the UserProperty.Name property, to a method.
Note This behavior does not occur if you use early-bound Microsoft
Office objects.WORKAROUNDTo work around this behavior, use either of the following
methods. Use early-bound objectsTo use early-bound objects, use code that is similar to the
following code: Dim outApp As New Application()
Dim cti As ContactItem
Dim usps As UserProperties
Dim usp As UserProperty
cti = outApp.CreateItem(OlItemType.olContactItem)
cti.FullName = "Test Default property"
cti.Save()
usps = cti.UserProperties
usp = usps.Add("Test", OlUserPropertyType.olNumber)
For Each usp In usps
Console.WriteLine(usps.Item(usp.Name).Name) ' You do not receive the error message.
Next Pass a variable instead of a property of an objectTo initialize a variable with the value of the property that you
want to pass and then pass the variable to the method, use the following code: Dim outApp As New Application()
Dim cti As ContactItem
Dim uspsObj As Object
Dim uspObj As Object
cti = outApp.CreateItem(OlItemType.olContactItem)
cti.FullName = "Test Default property"
cti.Save()
uspsObj = cti.UserProperties
uspObj = uspsObj.Add("Test", OlUserPropertyType.olNumber)
Dim strName As String
strName = uspObj.Name
Console.WriteLine(uspsObj.Item(strName).Name) STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are
listed at the beginning of this article.
REFERENCESFor more information, visit the following Microsoft Web
sites:
Modification Type: | Minor | Last Reviewed: | 10/3/2006 |
---|
Keywords: | kbvs2005swept kbvs2005applies kbvs2002sp1sweep kbOutlookObj kbCOMInterop kbinterop kberrmsg kbcode kbbug KB834063 kbAudDeveloper |
---|
|