Search code examples
vbaoutlookms-officeoutlook-addinribbonx

Get customized togglebutton in outlook addin


Hi I develop an addin for outlook where I want to add a new customized toggle button in the appointment tab. When I save the appointment I want to get the current state of the toggle button. This is my code so far:

So the button is already created, but when I press save, I can't get the button control. 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

UPDATE: Fixed the problem, that my onAction function never get called, because the parameter wasn't set right: Ribbon1.vb:

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

But the main question is: how to get the state of the toogle button when the user press save?


Solution

  • As I understand your basic need, you need to be able to check the state of the ToggleButton "ImportToSalesforce" when an Appointment is saved. I am not sure whether you are fine with using the Ribbon ( created from the Visual Designer ) instead of Ribbon XML ( which is more flexible and needs more programming as compared to the Visual Designer created Ribbon )

    When you create the Ribbon using Visual Designer(with the needed Toggle Button) , you can easily access the ribbon object from anywhere inside the Addin, using the Ribbon collection.

    ThisRibbonCollection ribbons = Globals.Ribbons[Globals.ThisAddIn.Application.ActiveInspector()]; ribbons.SalesForceRibbon.toggleButton1.Checked <-- This is what you need !

    In case you really need to use the Ribbon XML, instead of Ribbon Visual Designer, please refer - Is there a way to access a Ribbon (XML) at run time?