Dim toolsMenuName As String
Try
Dim resourceManager As System.Resources.ResourceManager = _
New System.Resources.ResourceManager("MyAddin.CommandBar", _
System.Reflection.Assembly.GetExecutingAssembly())
Dim cultureInfo As System.Globalization.CultureInfo = _
New System.Globalization.CultureInfo(_applicationObject.LocaleID)
toolsMenuName = resourceManager.GetString(String.Concat(cultureInfo.TwoLetterISOLanguageName, "Tools"))
Catch e As Exception
toolsMenuName = "Tools"
End Try
Dim commandBars As CommandBars = CType(_applicationObject.CommandBars, CommandBars)
Dim menuBarCommandBar As CommandBar = commandBars.Item("MenuBar")
Dim toolsControl As CommandBarControl = menuBarCommandBar.Controls.Item(toolsMenuName)
Dim toolsPopup As CommandBarPopup = CType(toolsControl, CommandBarPopup)
- Many commandbar popups are indexed in the DTE.CommandBars collection,
so it would be much more simple to locate the "Tools" commandbar popup in
the DTE.CommandBars collection using the English name, as we did for the "MenuBar"
commandbar.
- For commandbar popups that are not indexed in the DTE.CommandBars
collection (such as the "View" commandbar), for which you really need to
locate its CommandBarControl inside a Controls collection and
then cast it to CommandBarPopup, the approach used by the wizard requires a
CommandBar.resx resource file with some localized names. A more simple and
robust approach is shown here, which gets the commandbar associated to a
CommandBarControl using only English names:
Dim objMenuBarCommandBar As CommandBar
Dim objViewCommandBar As CommandBar
Dim objCommandBarControl As CommandBarControl
Dim objCommandBarPopup As CommandBarPopup
objMenuBarCommandBar = _applicationObject.CommandBars.Item("MenuBar")
For Each objCommandBarControl In objMenuBarCommandBar.Controls
If objCommandBarControl.Type = MsoControlType.msoControlPopup Then
objCommandBarPopup = DirectCast(objCommandBarControl, CommandBarPopup)
If objCommandBarPopup.CommandBar.Name = "View" Then ' English name
objViewCommandBar = objCommandBarPopup.CommandBar
Exit For
End If
End If
Next