How can I add a button into the context menu when I right click the mail item in Outlook’s Explorer?
Locked
- How can I add a button into the context menu when I right click the mail item in Outlook’s Explorer?
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Edited byJi.ZhouMSFT, ModeratorSaturday, February 14, 2009 12:55 AM
- Edited byJi.ZhouMSFT, ModeratorWednesday, February 11, 2009 12:45 PM
Answers
The approach to this problem depends on which version of Outlook we are working on.
(1) If we are working on Outlook 2007, we can easily achieve the objective by registering the Application.ItemContextMenuDisplay event. Then we have two parameters with a function to handle an event, one is the CommandBar object and another is the selected mail items. To add a button into the context menu, just call CommandBar.Controls.Add()
See the discussion in this link:
http://social.msdn.microsoft.com/forums/en-US/vsto/thread/74e3fbaf-5806-4684-b410-65a0720386e8/
(2) The Application.ItemContextMenuDisplay event was first introduced in Outlook 2007 object model. If we are working on Outlook 2003, we have to listen to CommandBars.OnUpdate event for this objective. But in the event handle, we do not have the parameter representing the corresponding command bar. We need to get that command bar by the following statement: Application.CommandBars[“Context Menu”].
We can get the sample codes from this link:
http://www.outlookcode.com/codedetail.aspx?id=314
Please notice that: the “context menu” customization is not supported in Office 2003 by Microsoft. Using this code will be at your own risk. The workaround solution may break at some point due to a change in the Outlook 2003 product implementation. Officially it is not possible to customize the context menus for Outlook 2003.
(Related forum thread http://social.msdn.microsoft.com/forums/en-US/vsto/thread/74e3fbaf-5806-4684-b410-65a0720386e8/ )
For more FAQ about Visual Studio Tools for Office, please see Visual Studio Tools for Office FAQ
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Marked As Answer byJi.ZhouMSFT, ModeratorWednesday, February 11, 2009 12:46 PM
All Replies
The approach to this problem depends on which version of Outlook we are working on.
(1) If we are working on Outlook 2007, we can easily achieve the objective by registering the Application.ItemContextMenuDisplay event. Then we have two parameters with a function to handle an event, one is the CommandBar object and another is the selected mail items. To add a button into the context menu, just call CommandBar.Controls.Add()
See the discussion in this link:
http://social.msdn.microsoft.com/forums/en-US/vsto/thread/74e3fbaf-5806-4684-b410-65a0720386e8/
(2) The Application.ItemContextMenuDisplay event was first introduced in Outlook 2007 object model. If we are working on Outlook 2003, we have to listen to CommandBars.OnUpdate event for this objective. But in the event handle, we do not have the parameter representing the corresponding command bar. We need to get that command bar by the following statement: Application.CommandBars[“Context Menu”].
We can get the sample codes from this link:
http://www.outlookcode.com/codedetail.aspx?id=314
Please notice that: the “context menu” customization is not supported in Office 2003 by Microsoft. Using this code will be at your own risk. The workaround solution may break at some point due to a change in the Outlook 2003 product implementation. Officially it is not possible to customize the context menus for Outlook 2003.
(Related forum thread http://social.msdn.microsoft.com/forums/en-US/vsto/thread/74e3fbaf-5806-4684-b410-65a0720386e8/ )
For more FAQ about Visual Studio Tools for Office, please see Visual Studio Tools for Office FAQ
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Marked As Answer byJi.ZhouMSFT, ModeratorWednesday, February 11, 2009 12:46 PM
Add VB version codes,
Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
AddHandler Application.ItemContextMenuDisplay, AddressOf Application_ItemContextMenuDisplay
End SubSub Application_ItemContextMenuDisplay(ByVal CommandBar As Microsoft.Office.Core.CommandBar, ByVal Selection As Microsoft.Office.Interop.Outlook.Selection)
Dim cb As Office.CommandBarButton
cb = CommandBar.Controls.Add(Office.MsoControlType.msoControlButton, , , , True)
cb.Visible = True
cb.Caption = "NewButton"
End Sub
We have published a VSTO FAQ recently, you can view them from the entry thread http://social.msdn.microsoft.com/Forums/en/vsto/thread/31b1ffbf-117b-4e8f-ad38-71614437df59. If you have any feedbacks or suggestions on this FAQ, please feel free to write us emails to colbertz@microsoft.com.


