Microsoft KB Archive/230876

= The CommandBar control reference becomes invalid before the COM add-in's OnDisconnection event fires =

Article ID: 230876

Article Last Modified on 5/31/2007

-

APPLIES TO


 * Microsoft Office Word 2007
 * Microsoft Office Word 2003
 * Microsoft Word 2002 Standard Edition
 * Microsoft Word 2000 Standard Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



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
 Start a new AddIn project in Visual Basic 6.0. Add a reference to the Office and Word object libraries. To do this, do one of the following:  For Microsoft Office Word 2007, add a reference to the Microsoft Office 12.0 object library and to the Microsoft Word 12.0 object library. For Microsoft Office Word 2003, add a reference to the Microsoft Office 11.0 object library and to the Microsoft Word 11.0 object library. For Microsoft Word 2002, add a reference to the Microsoft Office 10.0 object library and to the Microsoft Word 10.0 object library. For Microsoft Word 2000, add a reference to the Microsoft Office 9.0 object library and to the Microsoft Word 9.0 object library.</li></ul> </li> On the Project Explorer, open the Forms folder and remove frmAddin from the project.</li> On the Project Explorer, open the Designers folder, and then double-click the Connect Addin Designer.</li> On the General tab of the designer, change the Application to Microsoft Word, and then change the Initial Load Behavior to Startup.</li> On the View menu, click Code.</li>  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 </li> On the File menu, select Make MyAddin.dll to build the add-in.</li> 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.</li> 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

</li></ol>

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

Additional query words: comaddin addin command bar button msword 424 WD3007

Keywords: kbexpertiseinter kbautomation kbbug kbpending KB230876

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.