在 Outlook 插件中获取自定义切换按钮

Get customized togglebutton in outlook addin

您好,我为 outlook 开发了一个插件,我想在约会选项卡中添加一个新的自定义切换按钮。当我保存约会时,我想获得切换按钮的当前状态。 到目前为止,这是我的代码:

所以按钮已经创建了,但是当我按下保存时,我无法获取按钮控件。 Ribbon1.xml:

<tab idMso="TabAppointment">
        <group id="SalesforceGroup" label="Salesforce">
          <toggleButton id="ImportToSalesforce" size="large"
            label="Import to Salesforce" imageMso="DatabaseInsert"
            getPressed="GetPressed"
            onAction="Salesforce_Click"  />
        </group>
      </tab>

Ribbon1.vb:

Public Sub GetPressed(ByVal control As Office.IRibbonControl)
        MsgBox("test")   ' This alert only pops up when the appointment window opens
    End Sub

Public Sub Salesforce_Click(ByVal control As Office.IRibbonControl)
    MsgBox("test")    ' This alert never pops up
End Sub

ThisAddIn.vb:

Private Sub inspectors_NewInspector(ByVal Inspector As Microsoft.Office.Interop.Outlook.Inspector) Handles inspectors.NewInspector
    If TypeName(Inspector.CurrentItem) = "AppointmentItem" Then
        MsgBox("event")
        oAppointmentItem = TryCast(Inspector.CurrentItem, Outlook.AppointmentItem)
        AddHandler oAppointmentItem.Write, AddressOf Item_Save
    End If
End Sub

Private Sub Item_Save(ByRef Cancel As Boolean)
  'get IRibbonControl
End Sub

更新: 解决了我的 onAction 函数永远不会被调用的问题,因为参数设置不正确: Ribbon1.vb:

Public Sub Salesforce_Click(ByVal control As Office.IRibbonControl, _
    ByVal isPressed As Boolean)
    MsgBox("test2")
End Sub

但主要问题是:当用户按下保存时,如何获取toogle按钮的状态?

您需要调用自定义 UI 标记中声明的 GetPressed 函数作为 toggleButton 的回调。它应该如下所示:

C#: bool GetPressed(IRibbonControl control)
VBA: Sub GetPressed(control As IRibbonControl, ByRef returnValue)
C++: HRESULT GetPressed([in] IRibbonControl *pControl, [out, retval]VARIANT_BOOL *pvarfPressed)
Visual Basic: Function GetPressed(control As IRibbonControl) As Boolean

如您所见,returns 您感兴趣的布尔值。如果你不使用回调中的参数,只需传递 Nothing(C# 中为 null)。

看来你不明白功能区回调是如何设计和使用的。我建议阅读 MSDN 中的以下系列文章:

您可能还会发现 Globals.Ribbons 属性 有帮助,请参阅 Accessing the Ribbon at Run Time 了解更多信息。

据我了解您的基本需求,您需要能够在保存约会时检查 ToggleButton "ImportToSalesforce" 的状态。我不确定您是否可以使用 Ribbon(从 Visual Designer 创建)而不是 Ribbon XML(与 Visual Designer 创建的 Ribbon 相比,它更灵活并且需要更多编程)

当您使用可视化设计器(带有所需的切换按钮)创建功能区时,您可以使用功能区集合从插件内的任何位置轻松访问功能区对象。

ThisRibbonCollection 色带 = Globals.Ribbons[Globals.ThisAddIn.Application.ActiveInspector()]; ribbons.SalesForceRibbon.toggleButton1.Checked <-- 这就是您所需要的!

如果您真的需要使用 Ribbon XML,而不是 Ribbon Visual Designer,请参考 - Is there a way to access a Ribbon (XML) at run time?