Mike Smith-Lonergan

I've been puzzled by something for a while...every time I go looking for good source to double-check that the CommandBar and buttons I'm adding to one of the Office apps, I seem to come across one or more variants of the two main approaches here.

Approach 1 (VB):

Private WithEvents uiButton As Microsoft.Office.Core.CommandBarButton

Approach 2 (C#):

uiButton.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(uiButton_Click);

I can't tell if one of these is for application add-ins and the other for document-level add-ins, or if one is VSTO 2005 and the other is new to VSTO 2005 SE, or if one is for VSTO add-ins and the other for "Shared" Add-ins> Or something else.

Q: Are these types of Event Handler adding-code entirely equivalent, or are there unique dependencies, constraints or pre-requisites for using one over another

Q: When should I use one vs. the other (or can I interchange them)

I have a current CommandBarButton that no longer works, and I have no idea why. At some point it was working in this app's code (probably previous to this version):

http://www.codeplex.com/word2mediawikipp/SourceControl/FileView.aspx itemId=130408&changeSetId=13025

Now, it no longer fires - even despite the fact that (assuming Visual Studio isn't lying to me and this Solution hasn't magically converted from VSTO for Word to a Shared Add-in) I could swear I have all the right pieces in place (at least according to these forum archives). However, for whatever reason Visual Studio won't break on the Event, and it doesn't even trap a breakpoint that I've set on all pieces of the subroutine in which the developer has involved the buttons:

Public Class ThisAddIn

Private W2MWPPBar As Office.CommandBar

Private WithEvents uiConvert As Office.CommandBarButton

Structure CommandBarButtonSettings

Friend BeginGroupProperty As Boolean

Friend ButtonVariable As Office.CommandBarButton

Friend CaptionProperty As String

Friend DescriptionTextProperty As String

Friend FaceIDProperty As Integer

Friend StyleProperty As Office.MsoButtonStyle

Friend Tag As String

Friend TooltipTextProperty As String

End Structure

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

Dim _Template As Word.Template

For Each _Template In Me.Application.Templates

If _Template.Name = "W2MWPPTemplate.dot" Then

Me.Application.CustomizationContext = _Template

End If

If Me.Application.CustomizationContext Is Nothing Then

_Template = Me.Application.AddIns.Add("W2MWPPTemplate.dot")

Me.Application.CustomizationContext = _Template

End If

Next

AddCommandBar(True)

AddCommandBarButtons(True)

W2MWPPBar.Visible = True

End Sub

Private Sub AddCommandBar(ByVal replaceExisting As Boolean)

Dim _CommandBarsCollection As Office.CommandBars = DirectCast(Application.CommandBars, Office.CommandBars)

Try

If replaceExisting = True Then

W2MWPPBar = _CommandBarsCollection.FindControl(Tag:=TOOLBAR_NAME)

If W2MWPPBar IsNot Nothing Then

W2MWPPBar.Delete()

End If

End If

If _CommandBarsCollection.FindControl(Tag:=TOOLBAR_NAME) Is Nothing Then

W2MWPPBar = _CommandBarsCollection.Add(TOOLBAR_NAME, _

Office.MsoBarPosition.msoBarTop, _

False, _

False)

Else

W2MWPPBar = Application.CommandBars(TOOLBAR_NAME)

End If

Catch ex As System.ArgumentException

MessageBox.Show(String.Format("{0} add-in's toolbar could not be found - you won't be able to upload to the Wiki until you restart Word and/or reinstall the Add-in.", TOOLBAR_NAME) _

+ vbCrLf + vbCrLf + String.Format("Error: {0}", ex.Message), _

TOOLBAR_NAME, _

MessageBoxButtons.OK, _

MessageBoxIcon.Error)

Exit Sub

End Try

End Sub

Private Sub AddCommandBarButtons(ByVal replaceExistingButtons As Boolean)

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 and upload formatted text"

buttonsList.Add(buttonSettings)

buttonSettings.ButtonVariable = uiUpload

buttonSettings.CaptionProperty = "Upload Images"

buttonSettings.DescriptionTextProperty = String.Empty

buttonSettings.FaceIDProperty = 38

buttonSettings.StyleProperty = Office.MsoButtonStyle.msoButtonIconAndCaption

buttonSettings.Tag = "W2MWPP Upload"

buttonSettings.TooltipTextProperty = "Word2MediaWiki++ - Upload Images to wiki"

buttonsList.Add(buttonSettings)

buttonSettings.ButtonVariable = uiConfig

buttonSettings.CaptionProperty = "Configure Word2MediaWiki++"

buttonSettings.DescriptionTextProperty = String.Empty

buttonSettings.FaceIDProperty = 277

buttonSettings.StyleProperty = Office.MsoButtonStyle.msoButtonIconAndCaption

buttonSettings.Tag = "W2MWPP Config"

buttonSettings.TooltipTextProperty = "Word2MediaWiki++ - Add-in Configuration"

buttonsList.Add(buttonSettings)

If replaceExistingButtons = True Then

For Each _buttonSettings As CommandBarButtonSettings In buttonsList

_buttonSettings.ButtonVariable = W2MWPPBar.FindControl(Tag:=_buttonSettings.Tag)

If _buttonSettings.ButtonVariable IsNot Nothing Then

_buttonSettings.ButtonVariable.Delete()

End If

Next

End If

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

Try

With buttonSettings.ButtonVariable

.Caption = _ButtonSettings.CaptionProperty

.DescriptionText = _ButtonSettings.DescriptionTextProperty

If .FaceId < 2 Then

.FaceId = _ButtonSettings.FaceIDProperty

End If

.Style = _ButtonSettings.StyleProperty

.Tag = _ButtonSettings.Tag

.TooltipText = _ButtonSettings.TooltipTextProperty

End With

Catch ex As System.Runtime.InteropServices.COMException

If ex.ErrorCode = -2147467259 Then

MessageBox.Show(String.Format("The {0} toolbar has misconfigured one of its buttons. Please restart Word, and if this error recurs, please file a bug at {1}.", TOOLBAR_NAME, CODEPLEX_SITE) _

+ vbCrLf + vbCrLf + String.Format("Error: {0}", ex.Message), _

TOOLBAR_NAME, _

MessageBoxButtons.OK, _

MessageBoxIcon.Error)

End If

End Try

Next

End Sub

Q: Any ideas, anyone And rumours, inferred details or pointers to good docs that you know of would be most appreciated.

Cheers,




Re: Visual Studio Tools for Office Which "Event Handler-adding" code is more appropriate for my application add-in?

Cindy Meister

Hi Mike

Mmmm. As to why your code isn't working, the object variable you declare for your button - uiConvert - isn't used anywhere in your code. If nothing is assigned to the object variable, then any buttons you're creating won't pick up any event handlers.

The VB equivalent of Approach 2 is to use the AddHandler (and RemoveHandler) methods. They're described in this KB article

HOW TO: Handle Events for Excel by Using Visual Basic .NET

http://support.microsoft.com/kb/302814/en-us

C# has no equivalent to Approach 1. Approach 1 is "simpler", but does not allow as fine a control.






Re: Visual Studio Tools for Office Which "Event Handler-adding" code is more appropriate for my application add-in?

Mike Smith-Lonergan

Hi Cindy, thanks for catching my oversight - somehow that use of uiConvert didn't make my first cut-and-paste attempt. [I've added it back, and tabbed the code appropriately to make it slightly more readable.]

It was a little too obvious, though, that I hadn't used the variable in my actual code Smile - I just double-checked that it was still in the compiled project, and yet I'm still not seeing the Click event causing my click-enclosed code to fire.

I'll try the AddHandler approach - perhaps that'll work better than what I've got right now.

Thanks again!