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
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