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.
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.
Microsoft has confirmed that this is a bug in the Microsoft
products that are listed at the beginning of this article.
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.
- On the Project Explorer, open the Forms folder and remove frmAddin from the project.
- On the Project Explorer, open the Designers folder, and then double-click the Connect Addin Designer.
- On the General tab of the designer, change the Application to Microsoft Word, and then change the Initial Load Behavior to Startup.
- On the View menu, click Code.
- 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
- On the File menu, select Make MyAddin.dll to build the add-in.
- 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.
- 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
with:
Set oButton = _
oWord.CommandBars.FindControl(Tag:="Custom_Command_From_MyAddin")
oButton.Delete
oWord.NormalTemplate.Save