button Click event triggerd twice in com add-in word 2003

W

wendy

I created com add-in for word2003, excel2003 and powerpoint2003,
basically, i added 4 button in toobar and on_click, i apply some
bussiness logic. all works in excel2003 and powerpoint 2003, but in
word2003, the button event has been tiggered twice. After i click, it
run the code inside the button click, then run again. so annoying.

code attached below,Any help and suggestion will be great appriciated


Wendy


Imports Office = Microsoft.Office.Core
Imports Extensibility
Imports System.Runtime.InteropServices

Imports Word = Microsoft.Office.Interop.Word

<GuidAttribute("BF60D3CC-02A1-42f5-B17D-1965D13E7BB7"),
ProgIdAttribute("Office_AddIn.ConnectWord")> _
Public Class ConnectWord
Implements Extensibility.IDTExtensibility2

Dim applicationObject As Object
Dim addInInstance As Object

Private Shared WithEvents FEWordApplication As Word.Application

Dim FEToolbar As Microsoft.Office.Core.CommandBar = Nothing

Private WithEvents BtnEventsOptions As Office.CommandBarButton
Private WithEvents BtnEventsClassify As Office.CommandBarButton


Public Sub OnBeginShutdown(ByRef custom As System.Array)
Implements Extensibility.IDTExtensibility2.OnBeginShutdown
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
applicationObject = application
addInInstance = addInInst
Setup(application)

If Not application Is Nothing Then
FEToolbar = AddToolBar(application, "RM Office 2003
Activator.")
End If

BtnEventsClassify = MakeANewButton(FEToolbar, "RM Classify",
0, _
AddressOf BtnEventsClassify_Click)
BtnEventsClassify.Tag = "word_classity"

BtnEventsOptions = MakeANewButton(FEToolbar, "RM Options", 0, _
AddressOf BtnEventsOptions_Click)
BtnEventsOptions.Tag = "word_options"

End Sub

Public Shared Function Setup(ByVal oApp As Word.Application) As
Boolean
FEWordApplication = oApp
End Function


Private Sub p_ctlBtnEventsOptions_Click(ByVal Ctrl As
Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As
Boolean) Handles p_ctlBtnEventsOptions.Click
MsgBox("I am in Option_click")

End Sub

Private Sub p_ctlBtnEventsClassify_Click(ByVal Ctrl As
Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As
Boolean) Handles p_ctlBtnEventsClassify.Click

MsgBox("I am in Classity_click")

End Sub

End Class

Public Function MakeANewButton(ByVal commandBar As _
Microsoft.Office.Core.CommandBar, ByVal caption As String, _
ByVal faceID As Integer, ByVal clickHandler As _
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler) _
As Microsoft.Office.Core.CommandBarButton
Try
Dim newButton As Microsoft.Office.Core.CommandBarButton
newButton = CType(commandBar.Controls.Add( _
Microsoft.Office.Core.MsoControlType.msoControlButton),
_
Microsoft.Office.Core.CommandBarButton)
newButton.Caption = caption
newButton.FaceId = faceID
newButton.DescriptionText = "My button"
newButton.Style =
Office.MsoButtonStyle.msoButtonCaption
AddHandler newButton.Click, clickHandler
Return newButton
Catch ex As System.Exception
Return Nothing
End Try
End Function
 
T

Tom Winter

wendy said:
I created com add-in for word2003, excel2003 and powerpoint2003,
basically, i added 4 button in toobar and on_click, i apply some
bussiness logic. all works in excel2003 and powerpoint 2003, but in
word2003, the button event has been tiggered twice. After i click, it
run the code inside the button click, then run again. so annoying.

code attached below,Any help and suggestion will be great appriciated


Wendy


Imports Office = Microsoft.Office.Core
Imports Extensibility
Imports System.Runtime.InteropServices

Imports Word = Microsoft.Office.Interop.Word

<GuidAttribute("BF60D3CC-02A1-42f5-B17D-1965D13E7BB7"),
ProgIdAttribute("Office_AddIn.ConnectWord")> _
Public Class ConnectWord
Implements Extensibility.IDTExtensibility2

Dim applicationObject As Object
Dim addInInstance As Object

Private Shared WithEvents FEWordApplication As Word.Application

Dim FEToolbar As Microsoft.Office.Core.CommandBar = Nothing

Private WithEvents BtnEventsOptions As Office.CommandBarButton
Private WithEvents BtnEventsClassify As Office.CommandBarButton


Public Sub OnBeginShutdown(ByRef custom As System.Array)
Implements Extensibility.IDTExtensibility2.OnBeginShutdown
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
applicationObject = application
addInInstance = addInInst
Setup(application)

If Not application Is Nothing Then
FEToolbar = AddToolBar(application, "RM Office 2003
Activator.")
End If

BtnEventsClassify = MakeANewButton(FEToolbar, "RM Classify",
0, _
AddressOf BtnEventsClassify_Click)
BtnEventsClassify.Tag = "word_classity"

BtnEventsOptions = MakeANewButton(FEToolbar, "RM Options", 0, _
AddressOf BtnEventsOptions_Click)
BtnEventsOptions.Tag = "word_options"

End Sub

Public Shared Function Setup(ByVal oApp As Word.Application) As
Boolean
FEWordApplication = oApp
End Function


Private Sub p_ctlBtnEventsOptions_Click(ByVal Ctrl As
Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As
Boolean) Handles p_ctlBtnEventsOptions.Click
MsgBox("I am in Option_click")

End Sub

Private Sub p_ctlBtnEventsClassify_Click(ByVal Ctrl As
Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As
Boolean) Handles p_ctlBtnEventsClassify.Click

MsgBox("I am in Classity_click")

End Sub

End Class

Public Function MakeANewButton(ByVal commandBar As _
Microsoft.Office.Core.CommandBar, ByVal caption As String, _
ByVal faceID As Integer, ByVal clickHandler As _
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler) _
As Microsoft.Office.Core.CommandBarButton
Try
Dim newButton As Microsoft.Office.Core.CommandBarButton
newButton = CType(commandBar.Controls.Add( _
Microsoft.Office.Core.MsoControlType.msoControlButton),
_
Microsoft.Office.Core.CommandBarButton)
newButton.Caption = caption
newButton.FaceId = faceID
newButton.DescriptionText = "My button"
newButton.Style =
Office.MsoButtonStyle.msoButtonCaption
AddHandler newButton.Click, clickHandler
Return newButton
Catch ex As System.Exception
Return Nothing
End Try
End Function

Event sinking in Word is based on the TAG property of
CommandBarControls/Buttons/etc. This can make things a little screwy
sometimes. Try setting the TAG property of the buttons BEFORE you do the
AddHandler. That might help.

-Tom
 

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