Add menu item to existing sub menu
-
Wednesday, April 29, 2009 10:49 PM
In outlook 2007 I click the "actions" menu from the main screen and then click "Junk E-Mail" and get some menu's on the side. I would like to add my own sub menu to the "Junk E-Mail" sub menu's. Could someone point me in the right direction?
Best Regards
All Replies
-
Thursday, April 30, 2009 7:52 PMAnswerer
Hello,
Here is a C# example of how to accomplish that. To make this possible, I modified the example found at the following MSDN topic - http://msdn.microsoft.com/en-us/library/ms269110.aspx. The tricky part of adding submenus to existing Outlook menus is knowing the control ID of the menu to which you want to add a submenu. I just posted a blog that shows how to get the control IDs of built-in menus such as this one. You can find that post here - http://blogs.msdn.com/vsto/archive/2009/04/30/here-is-a-way-to-get-the-id-of-a-built-in-outlook-command-bar-menu-norm-estabrook.aspx.
public partial class ThisAddIn
{
// private Office.CommandBar menuBar;
private Office.CommandBarPopup newMenuBar;
private Office.CommandBarButton buttonOne;
private string menuTag = "A unique tag";private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
RemoveMenubar();
AddMenuBar();
}private void AddMenuBar()
{
try
{
// menuBar = this.Application.ActiveExplorer().CommandBars.ActiveMenuBar;
Office.CommandBarPopup cbc = (Office.CommandBarPopup)
this.Application.ActiveExplorer().CommandBars.FindControl
(Office.MsoControlType.msoControlPopup, 31353, missing, missing);
newMenuBar = (Office.CommandBarPopup)cbc.Controls.Add(
Office.MsoControlType.msoControlPopup, missing,
missing, missing, false);
if (newMenuBar != null)
{
newMenuBar.Caption = "New Menu";
newMenuBar.Tag = menuTag;
buttonOne = (Office.CommandBarButton)newMenuBar.Controls.
Add(Office.MsoControlType.msoControlButton, missing,
missing, 1, true);
buttonOne.Style = Office.MsoButtonStyle.
msoButtonIconAndCaption;
buttonOne.Caption = "Button One";
buttonOne.FaceId = 65;
buttonOne.Tag = "c123";
buttonOne.Click += new
Office._CommandBarButtonEvents_ClickEventHandler(
buttonOne_Click);
newMenuBar.Visible = true;
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}private void buttonOne_Click(Office.CommandBarButton ctrl,
ref bool cancel)
{
System.Windows.Forms.MessageBox.Show("You clicked: " + ctrl.Caption,
"Custom Menu", System.Windows.Forms.MessageBoxButtons.OK);
}
private void RemoveMenubar()
{
// If the menu already exists, remove it.
try
{
Office.CommandBarPopup foundMenu = (Office.CommandBarPopup)
this.Application.ActiveExplorer().CommandBars.ActiveMenuBar.
FindControl(Office.MsoControlType.msoControlPopup,
missing, menuTag, true, true);
if (foundMenu != null)
{
foundMenu.Delete(true);
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
Norm E.- Marked As Answer by Norm Estabrook - MSFTMicrosoft Employee, Editor Thursday, April 30, 2009 7:52 PM
- Edited by Norm Estabrook - MSFTMicrosoft Employee, Editor Thursday, April 30, 2009 9:18 PM This was a duplicate, so I used this space to add an additional comment
- Edited by Norm Estabrook - MSFTMicrosoft Employee, Editor Thursday, April 30, 2009 9:19 PM
- Edited by Norm Estabrook - MSFTMicrosoft Employee, Editor Thursday, April 30, 2009 9:22 PM
-
Friday, May 01, 2009 1:11 PMThanks for the update Norm, Below is a VB.NET version of how to get the control IDs of built-in menus:
Imports Microsoft.Office.Interop.Outlook
Imports
System.Collections
Imports
System.Windows.Forms
Imports
System.Reflection
Public
Class ThisAddIn
Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
' AddHandler Application.ItemContextMenuDisplay, AddressOf Application_ItemContextMenuDisplay
Dim app As Outlook.Application = Me.Application
'Create a new txt file to record controls' list
Dim sw As System.IO.StreamWriter = System.IO.File.CreateText("C:\Outlook Menus.txt")
'loop through Outlook ActiveExplorer CommandBars to get all CommandBars
For Each cb As Office.CommandBarControl In app.ActiveExplorer().CommandBars.ActiveMenuBar.Controls
PrintMenuItems(cb, sw)
Next
sw.Close()
End Sub
' Recursive method for printing menus and nested menus.
' Either the menu is a commandbarpopup (contains submenus)
' Or it is a commandbarbutton (contains no submenus)
Private Sub PrintMenuItems(ByVal menuItem As Object, ByVal sw As System.IO.StreamWriter)
If TryCast(menuItem, Office.CommandBarButton) Is Nothing Then
' This is a menu bar popup control.
sw.WriteLine((TryCast(menuItem, Office.CommandBarPopup).Caption & vbTab) + TryCast(menuItem, Office.CommandBarPopup).Id.ToString())
If TryCast(menuItem, Office.CommandBarPopup).accChildCount > 0 Then
For j As Integer = 1 To TryCast(menuItem, Office.CommandBarPopup).accChildCount
Dim x As Office.CommandBarPopup = TryCast(menuItem, Office.CommandBarPopup)
PrintMenuItems(TryCast(menuItem, Office.CommandBarPopup).accChild(j), sw)
Next
End If
Else
' Must be a command
sw.WriteLine((vbTab & TryCast(menuItem, Office.CommandBarControl).Caption & vbTab) + TryCast(menuItem, Office.CommandBarControl).Id.ToString())
End If
End Sub

