Why aren't my Toolbar buttons doing anything? [was CommandBarButton Event Handlers won't fire in VST

P

ParanoidMike

I haven't had any response so far - perhaps a more attention-getting
Subject will help? If anyone has any suggestions on what I should try
to modify in my VSTO code, to get these toolbar buttons to actually
run the code I've tried to connect to them, I'd sure appreciate the
help.

e.g. even though I've declared each CommandBarButton using the
"WithEvents" keyword *and* I've included an Event Handler subroutine
for the button's Click event, is it necessary to actually Add the
button each time the Add-in runs?

I notice that I've coded this Add-in in a way that it only Adds the
button if it doesn't currently exist (marked with *** below), so that
after the first time I launch Word, it should never add the button
again. However, I had assumed that once the button (with its Event
Handler) was created the first time, the Event Handlers would fire on
their own without any additional Startup activity for the Add-in. Is
this a false assumption?

Thanks, Mike

I *cannot* figure out what I'm missing to get my Event Handlers to
fire when I click the buttons I've added to a VSTO toolbar
(application add-in) in Word. The code approach I've used is
patterned after a working CommandBar & CommandBarButtons I have in a
different project, and it mirrors the approach documented in "VSTO for
Mere Mortals".

Here's the setup:
- Word 2003 SP3
- Visual Studio 2005 with the VSTO and VSTO SE add-ons
- Office PIAs and other dependencies installed (since other VSTO
projects work correctly in this copy of Word)
- I've even double-checked that the CAS policy that Visual Studio
automatically configures is "correctly" allowing FullTrust to my
assemblies (via the default URL policy that VS generates)

Here's the relevant code snipped from the current project (note that
the Structure is used to simplify the setup for three similar
CommandBarButtons, but I've only shown one button here):

Private W2MWPPBar As Office.CommandBar
Private WithEvents uiConvert As Office.CommandBarButton
Structure CommandBarButtonSettings
Public BeginGroupProperty As Boolean
Public ButtonVariable As Office.CommandBarButton
Public CaptionProperty As String
Public DescriptionTextProperty As String
Public FaceIDProperty As Integer
Public StyleProperty As Office.MsoButtonStyle
Public Tag As String
Public TooltipTextProperty As String
End Structure
Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Startup
Dim commandBarsCollection As Office.CommandBars =
DirectCast(Application.CommandBars, Office.CommandBars)
If commandBarsCollection.FindControl(Tag:=TOOLBAR_NAME) Is
Nothing Then
W2MWPPBar = commandBarsCollection.Add(TOOLBAR_NAME, _

Office.MsoBarPosition.msoBarTop, _
False, _
True)
Else
W2MWPPBar = Application.CommandBars(TOOLBAR_NAME)
End If

Dim buttonSettings As New CommandBarButtonSettings()
Dim buttonsList As New ArrayList()

buttonSettings.ButtonVariable = uiConvert
buttonSettings.CaptionProperty = "Convert to Wiki"
buttonSettings.DescriptionTextProperty = "Converts Word
Documents to MediaWiki format."
buttonSettings.FaceIDProperty = 2144
buttonSettings.StyleProperty =
Office.MsoButtonStyle.msoButtonIconAndCaption
buttonSettings.Tag = "W2MWPP Convert"
buttonSettings.TooltipTextProperty = "Word2MediaWiki++ -
Convert to MediaWiki syntax"

buttonsList.Add(buttonSettings)
For Each _buttonSettings As CommandBarButtonSettings In
buttonsList

***
If W2MWPPBar.FindControl(Tag:=_buttonSettings.Tag) Is
Nothing Then
buttonSettings.ButtonVariable =
CType(W2MWPPBar.Controls.Add(1), _

Office.CommandBarButton) ***

Else
buttonSettings.ButtonVariable =
W2MWPPBar.FindControl(Tag:=_buttonSettings.Tag)
End If

With buttonSettings.ButtonVariable
.Caption = _buttonSettings.CaptionProperty
.DescriptionText =
_buttonSettings.DescriptionTextProperty
.FaceId = _buttonSettings.FaceIDProperty
.Style = _buttonSettings.StyleProperty
.Tag = _buttonSettings.Tag
.TooltipText = _buttonSettings.TooltipTextProperty
End With
End Sub

Private Sub uiConvert_Click(ByVal Ctrl As
Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As
Boolean) Handles uiConvert.Click
' Event Handler code goes here...
End Sub

If you're interested in reviewing the source code in its entirety,
you're welcome to take a look at it here:http://www.codeplex.com/word2mediawikipp/SourceControl/DirectoryView....

So: with all this code in place, does anyone have any idea why the
CommandBar buttons do *nothing* when I click on them? [I've set
breakpoints in the code at the beginning of the Event Handlers, and VS
never breaks, so I'm 99% sure the handlers never get called. However,
when I had .OnAction properties being set for each CommandBarButton, I
was getting the VBA macro errors that are called out in Cindy
Meister's responses here:http://groups.google.com/group/microsoft.public.office.developer.com....]

Thanks for any assistance anyone can provide,
Mike
 
S

Swapnil Shejul

Hi Mike,

Well ! I am not expert in this field and writing some of my early Plugins
using VSTO.
But I came across exactly same problem as yours !

I guess u will have to make the declaration of commandbar variable at class
level.To simplyfy the process I will paste my code (this was written for a
OUTLOOK plugin) but i guess it is not a big deal to convert it for WORD.

'-------------------------CODE SNIPPET STARTS----------------------
Public Partial Class ThisAddIn
Private Const MENU_BEFORE As String = "Help"
Private menuBar As Office.CommandBar
Private helpMenuIndex As Object
Private topMenu As Office.CommandBarPopup
Private MenuItem1 As Office.CommandBarButton
Private BankMenuItem1 As Office.CommandBarButton
Private BankMenuItem2 As Office.CommandBarButton

Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As
System.EventArgs)

If MessageBox.Show("Do u want to proceed for plugin?",
"Plugin-startup message", MessageBoxButtons.YesNo, MessageBoxIcon.Question) =
DialogResult.Yes Then
Me.InitializeMenu()
Me.AttachEventHandler()
End If
End Sub
Private Sub InitializeMenu()
'Get the Active menu bar
menuBar = Me.Application.ActiveExplorer().CommandBars.ActiveMenuBar

' Get the index of the Help menu on the Outlook menu bar.
Try
helpMenuIndex = menuBar.Controls(MENU_BEFORE).Index
Catch ex As Exception
helpMenuIndex = menuBar.Controls.Count
End Try
' Add the top-level menu right before the Help menu.
topMenu =
DirectCast(menuBar.Controls.Add(Office.MsoControlType.msoControlPopup,
Type.Missing, Type.Missing, helpMenuIndex, True), Office.CommandBarPopup)
topMenu.Caption = "Test Menu"
topMenu.Visible = True
'--------Add FIRST sub-item to "Bank
Menu"-------------------------------
BankMenuItem1 =
DirectCast(topMenu.Controls.Add(Office.MsoControlType.msoControlButton,
Type.Missing, Type.Missing, 1, True), Office.CommandBarButton)
BankMenuItem1.Caption = "TestButton1"
BankMenuItem1.Visible = True
'--------Add SECOND sub-item to "Bank
Menu"-------------------------------
BankMenuItem2 =
DirectCast(topMenu.Controls.Add(Office.MsoControlType.msoControlButton,
Type.Missing, Type.Missing, 2, True), Office.CommandBarButton)
BankMenuItem2.Caption = "TestButton2"
BankMenuItem2.Visible = True
'------Add button DIRECTLY to the topmost level in the outlook
menubar----------------/
MenuItem1 =
DirectCast(menuBar.Controls.Add(Office.MsoControlType.msoControlButton,
Type.Missing, Type.Missing, 1, True), Office.CommandBarButton)
MenuItem1.Caption = "ATM-List"
MenuItem1.Visible = True
End Sub
Private Sub MenuItem_Click(ByVal cmdBarbutton As
Office.CommandBarButton, ByRef cancel As Boolean)
If MessageBox.Show("Do u want to send Email?", "Send An E-Mail",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
SendEmail()
End If
End Sub
Private Sub BankMenuItem2_Click(ByVal cmdBarbutton As
Office.CommandBarButton, ByRef cancel As Boolean)
MessageBox.Show("BankMenuItem2 button was clicked",
"Bank-Button2-ClickEvent")
End Sub
Private Sub SendEmail()
' Create a new e-mail item.
Dim mailItem As Outlook.MailItem =
DirectCast(Me.Application.CreateItem(Outlook.OlItemType.olMailItem),
Outlook.MailItem)

' Set the Subject property of the new MailItem.
mailItem.Subject = "Hello World!"

' Set the To address of the new MailItem to the username for the
current session.
'mailItem.To = this.Session.CurrentUser.Name.ToString();
mailItem.[To] = "(e-mail address removed)"
' Set the body of the new MailItem.
mailItem.Body = "This is the first Outlook application I " +
"created by using Visual Studio Tools for Office."

' Display the new MailItem.
mailItem.Display(False)

' Show a message box with options to send or not send the e-mail
message.
If MessageBox.Show("Would you like to send your Hello World! e-mail
message?", "HandsOnLab.Lab", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) = DialogResult.Yes Then
' Yes, send this message.
DirectCast(mailItem, Outlook._MailItem).Send()
Else
' Do not send this message. Close the MailItem and discard any
changes.
DirectCast(mailItem,
Outlook._MailItem).Close(Outlook.OlInspectorClose.olDiscard)
End If
End Sub
Private Sub ThisAddIn_Shutdown(ByVal sender As Object, ByVal e As
System.EventArgs)
End Sub


'--------------------------------------------------------------------------------------------------
Private Sub AttachEventHandler()
AddHandler MenuItem1.Click, AddressOf MenuItem_Click
AddHandler BankMenuItem2.Click, AddressOf BankMenuItem2_Click

'--------------------------------------------------------------------------------------------------------------
End Sub

End Class

'-------------------------CODE SNIPPET ENDS----------------------

Hope this helps you a bit :)

N'Joy Life N Try Hard,
Swaps


ParanoidMike said:
I haven't had any response so far - perhaps a more attention-getting
Subject will help? If anyone has any suggestions on what I should try
to modify in my VSTO code, to get these toolbar buttons to actually
run the code I've tried to connect to them, I'd sure appreciate the
help.

e.g. even though I've declared each CommandBarButton using the
"WithEvents" keyword *and* I've included an Event Handler subroutine
for the button's Click event, is it necessary to actually Add the
button each time the Add-in runs?

I notice that I've coded this Add-in in a way that it only Adds the
button if it doesn't currently exist (marked with *** below), so that
after the first time I launch Word, it should never add the button
again. However, I had assumed that once the button (with its Event
Handler) was created the first time, the Event Handlers would fire on
their own without any additional Startup activity for the Add-in. Is
this a false assumption?

Thanks, Mike

I *cannot* figure out what I'm missing to get my Event Handlers to
fire when I click the buttons I've added to a VSTO toolbar
(application add-in) in Word. The code approach I've used is
patterned after a working CommandBar & CommandBarButtons I have in a
different project, and it mirrors the approach documented in "VSTO for
Mere Mortals".

Here's the setup:
- Word 2003 SP3
- Visual Studio 2005 with the VSTO and VSTO SE add-ons
- Office PIAs and other dependencies installed (since other VSTO
projects work correctly in this copy of Word)
- I've even double-checked that the CAS policy that Visual Studio
automatically configures is "correctly" allowing FullTrust to my
assemblies (via the default URL policy that VS generates)

Here's the relevant code snipped from the current project (note that
the Structure is used to simplify the setup for three similar
CommandBarButtons, but I've only shown one button here):

Private W2MWPPBar As Office.CommandBar
Private WithEvents uiConvert As Office.CommandBarButton
Structure CommandBarButtonSettings
Public BeginGroupProperty As Boolean
Public ButtonVariable As Office.CommandBarButton
Public CaptionProperty As String
Public DescriptionTextProperty As String
Public FaceIDProperty As Integer
Public StyleProperty As Office.MsoButtonStyle
Public Tag As String
Public TooltipTextProperty As String
End Structure
Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Startup
Dim commandBarsCollection As Office.CommandBars =
DirectCast(Application.CommandBars, Office.CommandBars)
If commandBarsCollection.FindControl(Tag:=TOOLBAR_NAME) Is
Nothing Then
W2MWPPBar = commandBarsCollection.Add(TOOLBAR_NAME, _

Office.MsoBarPosition.msoBarTop, _
False, _
True)
Else
W2MWPPBar = Application.CommandBars(TOOLBAR_NAME)
End If

Dim buttonSettings As New CommandBarButtonSettings()
Dim buttonsList As New ArrayList()

buttonSettings.ButtonVariable = uiConvert
buttonSettings.CaptionProperty = "Convert to Wiki"
buttonSettings.DescriptionTextProperty = "Converts Word
Documents to MediaWiki format."
buttonSettings.FaceIDProperty = 2144
buttonSettings.StyleProperty =
Office.MsoButtonStyle.msoButtonIconAndCaption
buttonSettings.Tag = "W2MWPP Convert"
buttonSettings.TooltipTextProperty = "Word2MediaWiki++ -
Convert to MediaWiki syntax"

buttonsList.Add(buttonSettings)
For Each _buttonSettings As CommandBarButtonSettings In
buttonsList

***
If W2MWPPBar.FindControl(Tag:=_buttonSettings.Tag) Is
Nothing Then
buttonSettings.ButtonVariable =
CType(W2MWPPBar.Controls.Add(1), _

Office.CommandBarButton) ***

Else
buttonSettings.ButtonVariable =
W2MWPPBar.FindControl(Tag:=_buttonSettings.Tag)
End If

With buttonSettings.ButtonVariable
.Caption = _buttonSettings.CaptionProperty
.DescriptionText =
_buttonSettings.DescriptionTextProperty
.FaceId = _buttonSettings.FaceIDProperty
.Style = _buttonSettings.StyleProperty
.Tag = _buttonSettings.Tag
.TooltipText = _buttonSettings.TooltipTextProperty
End With
End Sub

Private Sub uiConvert_Click(ByVal Ctrl As
Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As
Boolean) Handles uiConvert.Click
' Event Handler code goes here...
End Sub

If you're interested in reviewing the source code in its entirety,
you're welcome to take a look at it here:http://www.codeplex.com/word2mediawikipp/SourceControl/DirectoryView....

So: with all this code in place, does anyone have any idea why the
CommandBar buttons do *nothing* when I click on them? [I've set
breakpoints in the code at the beginning of the Event Handlers, and VS
never breaks, so I'm 99% sure the handlers never get called. However,
when I had .OnAction properties being set for each CommandBarButton, I
was getting the VBA macro errors that are called out in Cindy
Meister's responses here:http://groups.google.com/group/microsoft.public.office.developer.com....]

Thanks for any assistance anyone can provide,
Mike
 

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