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)

SYMPTOMS

In 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.

CAUSE

If 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.

WORKAROUND

To work around this behavior, use either of the following methods.

Use early-bound objects

To 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 object

To 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.

MORE INFORMATION

Steps to reproduce the behavior

Note Before you follow these steps, verify that the Microsoft.Office.Interop.Outlook.dll Microsoft Office XP primary interop assembly (PIA) is installed in the global assembly cache (GAC) on your computer.

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

328912 INFO: Microsoft Office XP PIAs Are Available for Download

  1. Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET.
  2. Use Visual Basic 2005 or Visual Basic .NET to create a Console Application project.

    By default, the Module1.vb file is created.
  3. On the Project menu, click Add Reference.

    The Add Reference dialog box appears.
  4. Click the COM tab, and then click Microsoft Outlook 10.0 Object Library.
  5. Click Select, and then click OK.
  6. Add the following code at the top of the Module1.vb file:
    Imports Microsoft.Office.Interop.Outlook
  7. In the Module1.vb file, locate the following code:
    Sub Main()
  8. Add the following code after the code that you located in step 7:
    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)
    
    For Each uspObj In uspsObj
       Console.WriteLine(uspsObj.Item(uspObj.Name).Name) ' You receive the error message at this line.
    Next
  9. On the Build menu, click Build ConsoleApplication1.
  10. On the Debug menu, click Start to run the application.

    A console window appears. You receive the error message that is mentioned in the "Symptoms" section of this article.

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbvs2002sp1sweep kbOutlookObj kbCOMInterop kbinterop kberrmsg kbcode kbbug KB834063 kbAudDeveloper