What has happend to the event handler?

H

hayrob

See the code below - a Windows form instantiates a Word Controller class
which in turn launches Word. When Word is launched a Menu Item is added. If
the button on the Windows form is clicked, a Word document is opened and the
new menu item is enabled. If the user opens any other document in Word, the
Menu item is not made visible.

The problem is - if one document is open, it works fine. If both documents
are open (click both buttons on the Windows form), the menu item works for
the first document to be opened, but not for the second. The click event is
not captured by the event handler!

Help!!

Code follows:

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Private mWordController As New WordController

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
mWordController.OpenWordDoc(FILE_NAME1)
Catch ex As Exception
If Not mWordController Is Nothing Then
mWordController.Dispose()
mWordController = Nothing
End If
End Try
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Try
mWordController.OpenWordDoc(FILE_NAME2)
Catch ex As Exception
If Not mWordController Is Nothing Then
mWordController.Dispose()
mWordController = Nothing
End If
End Try
End Sub

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If Not mWordController Is Nothing Then
mWordController.Dispose()
End If
End Sub

End Class


Imports Word = Microsoft.Office.Interop.Word
Imports Office = Microsoft.Office.Core

Public Class WordController
Implements IDisposable


Private mWordApp As Word.Application
Private mMainMenuBar As Office.CommandBar
Private mMyMenuItem As Office.CommandBarControl
Private WithEvents mMySubMenuItem As Office.CommandBarButton

Public Sub New()

End Sub

Public Sub OpenWordDoc(ByVal fileName As String)

If mWordApp Is Nothing Then
StartWord()
End If

Dim wordDoc As Word.Document
RemoveHandler mWordApp.WindowActivate, AddressOf WindowActivate
wordDoc = mWordApp.Documents.Open(CObj(fileName))
AddHandler mWordApp.WindowActivate, AddressOf WindowActivate

wordDoc.Variables.Add("IsTest", "true")

mWordApp.Visible = True

End Sub

Private Sub StartWord()

mWordApp = New Word.Application
AddWordAppEventHandlers()
InitMenuBarItems()

mWordApp.NormalTemplate.Saved = True
End Sub

Private Sub AddWordAppEventHandlers()

AddHandler mWordApp.Quit, AddressOf WordAppQuit
AddHandler mWordApp.WindowActivate, AddressOf WindowActivate

End Sub

Private Sub InitMenuBarItems()

mMainMenuBar = mWordApp.CommandBars("Menu Bar")
mMyMenuItem =
mMainMenuBar.Controls.Add(Office.MsoControlType.msoControlPopup,
Temporary:=True)

Dim cbc As Office.CommandBarControl = DirectCast(mMyMenuItem,
Office.CommandBarControl)
cbc.Caption = "My Menu Item"
cbc.Visible = True

mMySubMenuItem = CreateButton(DirectCast(mMyMenuItem,
Office.CommandBarPopup), "My Sub Menu Item")
mMySubMenuItem.Enabled = False
AddHandler mMySubMenuItem.Click, AddressOf MySubMenuItem

End Sub

Private Shared Function CreateButton( _
ByVal parent As Office.CommandBarPopup, _
ByVal caption As String) _
As Office.CommandBarButton

Dim cbc As Office.CommandBarControl
' The menu item is again loaded temporarily.
cbc = parent.Controls.Add(Office.MsoControlType.msoControlButton,
Temporary:=True)
cbc.Caption = caption
cbc.Visible = True

Return DirectCast(cbc, Office.CommandBarButton)

End Function

Private Sub SetUpDocumentMenuItems()

If mWordApp Is Nothing Then
Exit Sub
End If
If mWordApp.Documents.Count = 0 Then
Exit Sub
End If
' There must be an active document.

Dim doc As Word.Document = mWordApp.ActiveDocument

If IsMyDoc(doc) Then
mMySubMenuItem.Enabled = True
mMySubMenuItem.Visible = True
Dim cbc As Office.CommandBarControl = mWordApp.CommandBars("Menu
Bar").Controls("My Menu Item")
cbc.Visible = True
mWordApp.NormalTemplate.Saved = True
Else
mMySubMenuItem.Visible = False
Dim cbc As Office.CommandBarControl = mWordApp.CommandBars("Menu
Bar").Controls("My Menu Item")
cbc.Visible = False
mWordApp.NormalTemplate.Saved = True
End If

End Sub

Private Function IsMyDoc(ByVal wordDoc As Word.Document) As Boolean

For Each var As Word.Variable In wordDoc.Variables
If var.Name = "IsTest" Then
Return True
End If
Next
Return False

End Function

Private Sub MySubMenuItem(ByVal ctrl As Office.CommandBarButton, ByRef
CancelDefault As Boolean)

MsgBox("Handler works")

End Sub

Private Sub WindowActivate(ByVal Doc As Word.Document, ByVal Wn As
Word.Window)

SetUpDocumentMenuItems()

mWordApp.NormalTemplate.Saved = True

End Sub

Private Sub WordAppQuit()

mWordApp = Nothing

End Sub

Public Sub Dispose() Implements System.IDisposable.Dispose
Try
If Not (mWordApp Is Nothing) Then
mWordApp.Quit(Word.WdSaveOptions.wdDoNotSaveChanges)
mWordApp = Nothing
End If
Catch ex As Exception

End Try
End Sub

End Class
 
W

Wei-Dong XU [MSFT]

Hi,

Currently we are finding one support engineer for you on this issue. If any
result, we will update you at the first time.

Best Regards,
Wei-Dong XU
Microsoft Product Support Services
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Huang [MSFT]

Hi

You may try to set the commandbarbutton's Tag property for the Word used to
identify the different document.
e.g.
CommandBarButton.Tag = "Test Tag";

Here is link for your reference.
http://groups-beta.google.com/group/microsoft.public.office.developer.com.ad
d_ins/browse_thread/thread/674deb030a90e16/e31ccc1c6cba18ab?q=%22v-phuang%22
+tag+office&rnum=10&hl=en#e31ccc1c6cba18ab

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
H

hayrob

Peter

That is absolute magic! It works!

But why does the magic work?

Thanks for your help, but I would like to understand why simply setting the
CommandBarButton' Tag works!
 
P

Peter Huang [MSFT]

Hi

I think this is by design. Word need the tag to separator different
document, because we just add event to a commandbarbutton, word need the
information to distribute the click event to the right document.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
H

hayrob

Peter

Isn't the commandbarbutton associated with the application, not the
document? Once a tag is set for the commandbarbutton, why does the
application then fire the event? Seems very strange - I'm sure it is by
design, but I can't see why. Nonetheleass, I am very, very happy I have a
solution to my problem!

Robin Hay
 
P

Peter Huang [MSFT]

Hi

For the detailed implement of Word is not public.
I think the commandbar object is associated with application, that is to
say there is one commandbarbutton instance in the memory, to associated
with multiple document, it need the tag.

Also I am glad that works for you.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top