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 StructurePrivate 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
Me.Application.AddIns.Add("W2MWPPTemplate.dot") Me.Application.CustomizationContext = _Template End If Next_Template =
AddCommandBar(
True)AddCommandBarButtons(
True)W2MWPPBar.Visible =
True End SubPrivate Sub AddCommandBar(ByVal replaceExisting As Boolean)
Dim _CommandBarsCollection As Office.CommandBars = DirectCast(Application.CommandBars, Office.CommandBars) Try
If replaceExisting = True Then
If W2MWPPBar IsNot Nothing ThenW2MWPPBar = _CommandBarsCollection.FindControl(Tag:=TOOLBAR_NAME)
End If End If If _CommandBarsCollection.FindControl(Tag:=TOOLBAR_NAME) Is Nothing ThenW2MWPPBar.Delete()
False, _ False) ElseW2MWPPBar = _CommandBarsCollection.Add(TOOLBAR_NAME, _
Office.MsoBarPosition.msoBarTop, _
End If Catch ex As System.ArgumentExceptionW2MWPPBar = Application.CommandBars(TOOLBAR_NAME)
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) _MessageBox.Show(
String.Format("Error: {0}", ex.Message), _+ vbCrLf + vbCrLf +
TOOLBAR_NAME, _
MessageBoxButtons.OK, _
MessageBoxIcon.Error)
Exit Sub End Try End SubPrivate 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)
"Upload Images"buttonSettings.ButtonVariable = uiUpload
buttonSettings.CaptionProperty =
buttonSettings.DescriptionTextProperty =
String.EmptybuttonSettings.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.EmptybuttonSettings.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
If _buttonSettings.ButtonVariable IsNot Nothing Then_buttonSettings.ButtonVariable = W2MWPPBar.FindControl(Tag:=_buttonSettings.Tag)
End If Next End If_buttonSettings.ButtonVariable.Delete()
For Each _ButtonSettings As CommandBarButtonSettings In buttonsList
If W2MWPPBar.FindControl(Tag:=_ButtonSettings.Tag) Is Nothing ThenCType(W2MWPPBar.Controls.Add(1), _buttonSettings.ButtonVariable =
Office.CommandBarButton)
Else
End If TrybuttonSettings.ButtonVariable = W2MWPPBar.FindControl(Tag:=_ButtonSettings.Tag)
With buttonSettings.ButtonVariable
If .FaceId < 2 Then.Caption = _ButtonSettings.CaptionProperty
.DescriptionText = _ButtonSettings.DescriptionTextProperty
End If.FaceId = _ButtonSettings.FaceIDProperty
.Style = _ButtonSettings.StyleProperty
.Tag = _ButtonSettings.Tag
.TooltipText = _ButtonSettings.TooltipTextProperty
End With Catch ex As System.Runtime.InteropServices.COMExceptionIf ex.ErrorCode = -2147467259 Then
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) _MessageBox.Show(
String.Format("Error: {0}", ex.Message), _+ vbCrLf + vbCrLf +
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,