简体   繁体   English

C#Outlook以编程方式访问CommandBar

[英]C# Outlook Acces CommandBar Programmatically

I'm trying to access to some buttons on my Outlook Ribbon Programmatically. 我试图以编程方式访问Outlook功能区上的某些按钮。 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 ? 问题是每个CommandBars仅返回1个控件...如何访问功能区中的所有控件?

Thanks 谢谢

Command bars were deprecated and not used any longer (only for executing buttons programmatically). 命令栏已弃用,不再使用(仅用于以编程方式执行按钮)。 You need to use the Fluent UI instead. 您需要改用Fluent UI。

But the Fluent UI doesn't provide any way for iterating through the existing controls programmatically. 但是Fluent UI并没有提供任何以编程方式遍历现有控件的方法。 As a workaround you may use Accessibility API (Windows API) functions to get the job done. 解决方法是,您可以使用可访问性API(Windows API)函数来完成工作。

You can read more about the Ribbon UI in the following articles in MSDN: 您可以在MSDN的以下文章中阅读有关Ribbon UI的更多信息:

As Eugene mentioned, you can use the Accessibility API. 正如Eugene所说,您可以使用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. 如果使用“ 赎回”是一个选项,它将公开SafeExplorerSafeInspector对象,这些对象提供对功能区控件的访问并允许执行其默认操作。 The example below (VB script) executes the "OneNote" button on the "Home" ribbon: 下面的示例(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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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