BUG: CommandBar Control Reference Becomes Invalid Before COM Add-In's OnDisconnection Event Fires (230876)



The information in this article applies to:

  • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Office Word 2003
  • Microsoft Word 2002
  • Microsoft Word 2000

This article was previously published under Q230876

SYMPTOMS

Global object references to CommandBar controls are not valid in the OnDisconnection event of your COM Add-in when they are hosted in Microsoft Word. You might receive the following error message when you attempt to call a method or property on the invalidated object reference in the OnDisconnection event:
Run-time error '424':
Object Required
The problem may occur regardless of whether the object reference is to a custom or a built-in CommandBar control.

NOTE: You may also encounter the problem with the global CommandBar control object reference becoming invalid in the OnStartUpComplete and OnBeginShutdown events.

RESOLUTION

To work around the problem, use the FindControl method to obtain a new object reference to the CommandBar control. This solution is illustrated in the "More Information" section.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

One scenario where you might use a global CommandBar control reference is when you add your own custom control or controls in the OnConnection event for your COM Add-in, and then attempt to remove those controls with the global object variables in the OnDisconnection event. The following steps illustrate the error that might occur in this scenario and how you can resolve the error.

Steps to Reproduce Behavior

  1. Start a new AddIn project in Visual Basic 6.0.
  2. For Word 2000, Add a reference to the Microsoft Office 9.0 and Microsoft Word 9.0 Object Libraries. For Word 2002, add a reference to the Microsoft Office 10.0 and Microsoft Word 10.0 Object Libraries. For Microsoft Office Word 2003, add a reference to the Microsoft Office 11.0 and the Microsoft Word 11.0 Object Libraries.
  3. On the Project Explorer, open the Forms folder and remove frmAddin from the project.
  4. On the Project Explorer, open the Designers folder, and then double-click the Connect Addin Designer.
  5. On the General tab of the designer, change the Application to Microsoft Word, and then change the Initial Load Behavior to Startup.
  6. On the View menu, click Code.
  7. Replace all of the code in the Connect code module with the following:
    Option Explicit
    
    Dim oWord As Word.Application
    Dim oButton As Office.CommandBarButton
    
    Private Sub AddinInstance_OnConnection(ByVal Application As Object, _
       ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _
       ByVal AddinInst As Object, custom() As Variant)
       
       Set oWord = Application
       Set oButton = oWord.CommandBars("File").Controls.Add(msoControlButton)
       oButton.Caption = "Custom Command"
       oButton.Tag = "Custom_Command_From_MyAddin"
       MsgBox "Added custom Commandbar control to the File menu."
    
    End Sub
    
    Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode As _
       AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
    
       oButton.Delete
       MsgBox "Removed custom Commandbar control from the File menu."
       Set oButton = Nothing
       Set oWord = Nothing
    
    End Sub
    					
  8. On the File menu, select Make MyAddin.dll to build the add-in.
  9. Start Microsoft Word. A message box appears which indicates that the add-in loaded successfully and a custom CommandBar control was added to the File menu.
  10. on the File menu, click Exit to quit Word.

    Result: You receive the following error message because the global oButton object variable has become invalid:
    Run-time error '424':
    Object Required

Workaround

To work around this problem, use the FindControl method in the OnDisconnection event to obtain a new reference to the CommandBar control. In the previous sample, replace the following line
   oButton.Delete
				
with:
   Set oButton = _
      oWord.CommandBars.FindControl(Tag:="Custom_Command_From_MyAddin")
   oButton.Delete 
   oWord.NormalTemplate.Save
				

Modification Type:MajorLast Reviewed:3/23/2006
Keywords:kbAutomation kbbug kbpending KB230876