简体   繁体   中英

C# Outlook Acces CommandBar Programmatically

I'm trying to access to some buttons on my Outlook Ribbon Programmatically. So i am using:

Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
CommandBar command = app.ActiveExplorer().CommandBars.OfType<CommandBar>().First();
CommandBarControl button = command.Controls.OfType<CommandBarControl>().Where(x => x.Caption == "label of my button").First();
button.Execute();

The problem is every CommandBars only return 1 Control... How can i access to all Controls inside a Ribbon ?

Thanks

Command bars were deprecated and not used any longer (only for executing buttons programmatically). You need to use the Fluent UI instead.

But the Fluent UI doesn't provide any way for iterating through the existing controls programmatically. As a workaround you may use Accessibility API (Windows API) functions to get the job done.

You can read more about the Ribbon UI in the following articles in MSDN:

As Eugene mentioned, you can use the Accessibility API. If using Redemption is an option, it exposes SafeExplorer and SafeInspector objects that provide access to the ribbon controls and allow to execute their default actions. The example below (VB script) executes the "OneNote" button on the "Home" ribbon:

 set sExplorer = CreateObject("Redemption.SafeExplorer")
 sExplorer.Item = Application.ActiveExplorer
 set Ribbon = sExplorer.Ribbon
 oldActiveTab = Ribbon.ActiveTab
 Ribbon.ActiveTab = "Home"
 set Control = Ribbon.Controls("OneNote")
 Control.Execute
 Ribbon.ActiveTab = oldActiveTab 'restore the active tab

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM