I'm developing a COM add-in for Microsoft Word using VB.Net and VS 2003. My
Connect class adds a menu item, and has a shared event handler that should
respond to the menu item's click events. My (simplified) code is below.
My problem is that the menu item fires, but only for the first document that
opens. When I start Word and get a blank document, the menu item works
fine. However, if I open a new document window, either by opening a
document from disk or by creating a new document, the menu item no longer
responds. If I switch back to the original document, the menu will start
working correctly again.
Word isn't raising any errors -- it just appears that the menu event isn't
firing. The same problem occurs in both Word 2000 (using the Word 9 object
library) and Word XP (using the Office XP PIA). This issue was discussed in
an earlier thread, but apparently was never resolved:
Also, for what it's worth, this behavior sounds similar to menu events when
multiple instances of Outlook are opened, as discussed in this article (in
the Code Patterns section):
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnoxpta/html/odc_dnscof.asp
The article recommends adding a new event delegate each time a new Outlook
Explorer is opened. However, this doesn't seem directly applicable to my
problem -- as far as I can tell, Word doesn't have separate explorers for
different documents.
Any suggestions? Is this a bug, or have I overlooked something? Thanks.
--Robert Jacobson
Imports Microsoft.Office.Core
Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop
Imports System.Windows.Forms
<GuidAttribute("AE434928-4B29-4555-9C24-09A6EDE843AC"),
ProgIdAttribute("MyAddin1.Connect")> _
Public Class Connect
Implements Extensibility.IDTExtensibility2
Private Shared mWordApp As Word.Application
Private Shared addInInstance As Object
Private Shared mMenu As CommandBarPopup
Private Shared WithEvents mMenuItem As CommandBarButton
Public Sub OnBeginShutdown(ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnBeginShutdown
Call RemoveMenu()
End Sub
Public Sub OnAddInsUpdate(ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnAddInsUpdate
End Sub
Public Sub OnStartupComplete(ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnStartupComplete
End Sub
Public Sub OnDisconnection(ByVal RemoveMode As
Extensibility.ext_DisconnectMode, ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnDisconnection
End Sub
Public Sub OnConnection(ByVal application As Object, ByVal connectMode
As Extensibility.ext_ConnectMode, ByVal addInInst As Object, ByRef custom As
System.Array) Implements Extensibility.IDTExtensibility2.OnConnection
mWordApp = DirectCast(application, Word.Application)
Call AddMenus()
End Sub
'-- Add the root menu ("New Menu") to the menu bar, and add a menu item
("Click Me") to it.
Private Sub AddMenus()
Try
mMenu =
DirectCast(mWordApp.CommandBars.ActiveMenuBar.Controls.Add(MsoControlType.ms
oControlPopup), CommandBarPopup)
mMenu.Caption = "New Menu"
mMenuItem =
DirectCast(mMenu.Controls.Add(MsoControlType.msoControlButton),
CommandBarButton)
mMenuItem.Caption = "Click Me"
Catch ex As Exception
Debug.Write(ex.ToString)
End Try
End Sub
'-- Remove the newly-added menu. See KB article 230876.
Private Sub RemoveMenu()
Try
mWordApp.CommandBars.ActiveMenuBar.Controls("New Menu").Delete()
mWordApp.NormalTemplate.Save()
Catch ex As Exception
Debug.Write(ex.ToString)
End Try
End Sub
'-- Click event handler for the "Click Me" menu item.
'-- Only responds to click events from within the first document in
Word.
Private Shared Sub mMenuItem_Click(ByVal Ctrl As CommandBarButton, ByRef
CancelDefault As Boolean) Handles mMenuItem.Click
MessageBox.Show("You clicked me.")
End Sub
End Class
Custom menu items in Word require a Tag property to properly handle Click
events for multiple windows. Try adding a unique tag to your menu item and
see if that resolves the problem:
mMenuItem.Tag = "Click_Me"
Hope that helps!
Lori Turner
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
>> From: "Robert Jacobson" <rjacobson_at...@nospam.com>
>> Subject: Bug? Menu click event in Word stops responding
>> Date: Wed, 6 Aug 2003 12:56:18 -0700
>> Lines: 117
>> X-Priority: 3
>> X-MSMail-Priority: Normal
>> X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
>> X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
>> Message-ID: <OGg3PTFX...@TK2MSFTNGP12.phx.gbl>
>> Newsgroups:
microsoft.public.dotnet.framework.interop,microsoft.public.vsnet.vstools.off
ice
>> NNTP-Posting-Host: adsl-64-161-31-95.dsl.sntc01.pacbell.net 64.161.31.95
>> Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
>> Xref: cpmsftngxa06.phx.gbl microsoft.public.vsnet.vstools.office:622
microsoft.public.dotnet.framework.interop:17838
>> X-Tomcat-NG: microsoft.public.vsnet.vstools.office
Jason
>.
>
Could someone develop a KB article on this, though? It's a very esoteric
problem, and not a very intuitive solution. (Dare a say... a bug?) I had
the impression from the docs that tags are supposed to be optional.
--Robert Jacobson
""Lori Turner [MSFT]"" <Lor...@online.microsoft.com> wrote in message
news:JJLfp4eX...@cpmsftngxa06.phx.gbl...
Actually we do have a KB in the works for this.
It's not a bug really -- Word, because of the SDI structure for its
windows, requires this tag for menu items so that it knows what control you
click when you're switching between windows. The issue with the Tag
property has been around since Word 2000 and was actually introduced in
2000 as a fix to a problem with events not firing.
Regards,
Lori Turner
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
>> From: "Robert Jacobson" <rjacobson_at...@nospam.com>
>> References: <OGg3PTFX...@TK2MSFTNGP12.phx.gbl>
<JJLfp4eX...@cpmsftngxa06.phx.gbl>
>> Subject: Re: Bug? Menu click event in Word stops responding
>> Date: Fri, 8 Aug 2003 14:20:51 -0700
>> Lines: 203
>> X-Priority: 3
>> X-MSMail-Priority: Normal
>> X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
>> X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
>> Message-ID: <u6$q0LfXD...@TK2MSFTNGP10.phx.gbl>
>> Newsgroups:
microsoft.public.vsnet.vstools.office,microsoft.public.dotnet.framework.inte
rop
>> NNTP-Posting-Host: adsl-64-161-31-95.dsl.sntc01.pacbell.net 64.161.31.95
>> Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
>> Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.interop:17899
microsoft.public.vsnet.vstools.office:633
>> X-Tomcat-NG: microsoft.public.vsnet.vstools.office