C# Outlook 以编程方式访问 CommandBar

C# Outlook Acces CommandBar Programmatically

我正在尝试以编程方式访问 Outlook 功能区上的某些按钮。 所以我正在使用:

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();

问题是每个 CommandBars 只有 return 1 个控件...我如何访问功能区内的所有控件?

谢谢

命令栏已弃用,不再使用(仅用于以编程方式执行按钮)。您需要改用 Fluent UI。

但是 Fluent UI 没有提供任何以编程方式迭代现有控件的方法。作为解决方法,您可以使用辅助功能 API (Windows API) 函数来完成工作。

您可以在 MSDN 中的以下文章中阅读有关功能区 UI 的更多信息:

正如 Eugene 提到的,您可以使用辅助功能 API。 如果使用提供对功能区控件的访问并允许执行其默认操作的 Redemption is an option (I am its author), it exposes SafeExplorer and SafeInspector 对象。下面的示例(VB 脚本)执行“主页”功能区上的“OneNote”按钮:

 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