1

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

所以按钮已经创建,但是当我按下保存时,我无法获得按钮控件。功能区1.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>

功能区1.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

这个插件.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

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

4

2 回答 2

2

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

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

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

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

此外,您可能会发现 Globals.Ribbons 属性很有帮助,请参阅在运行时访问功能区以获取更多信息。

于 2015-03-26T13:26:38.620 回答
1

据我了解您的基本需求,您需要能够在保存约会时检查切换按钮“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,请参考 - 有没有办法在运行时访问 Ribbon (XML)?

于 2015-03-28T14:32:15.487 回答