marcoborn
Goto Top

Ribbon-Button deaktivieren

Hallo Forum,
ich habe meine erste RibbonBar in VB.NET erzeugt. Von meinem COM-Addin aus greife ich auf die Ribbons von Excel zu.

Ich möchte, dass ein Button dynamisch disabled/enabled wird. Nach Klick auf den Button1 soll dieser so lange deaktiviert sein, bis man den Button2 angeklickt hat. Erst dann soll Button1 wieder anklickbar sein.

Mein XML-Code sieht wie folgt aus:
<button id="t50" label="Prüfung an" imageMso="DataValidationCircleInvalid" onAction = "ButtonOnAction" getEnabled = "getEnabled_Button1" size="normal" />  
<button id="t51" label="Prüfung aus" imageMso="DataValidationClearValidationCircles" onAction = "ButtonOnAction" getEnabled = "getEnabled_Button2" size="normal" />  

Hier die zugehörigen Routinen:
Public Sub getEnabled_Button1(control As IRibbonControl, ByRef enabled As Boolean)
  If Not bolButton1 Then enabled = True
End Sub

Public Sub getEnabled_Button2(control As IRibbonControl, ByRef enabled As Boolean)
  enabled = True
End Sub

Wie bekomme ich es hin, dass die beiden Buttons beim Start von Excel aktiviert sind? Im Moment werden sie immer auf disabled gesetzt und ich kann sie nicht aktivieren.

Vielen Dank im voraus,
M. Born

Content-Key: 284075

Url: https://administrator.de/contentid/284075

Printed on: April 19, 2024 at 21:04 o'clock

Member: colinardo
Solution colinardo Sep 28, 2015, updated at Sep 29, 2015 at 11:22:11 (UTC)
Goto Top
Hallo Marco,
deine Callback-Handler haben nicht die richtige Signatur. Hier ein Beispiel wie dies aussehen kann :
   'save state of Ribbonbutton 1  
   Private btn1State As Boolean = True

    'GetEnabled of Button1  
    Function getEnabled_Button1(control As IRibbonControl) As Boolean
        return btn1State
    End Function

    'GetEnabled of Button2  
    Public Function getEnabled_Button2(control As IRibbonControl) As Boolean
        Return True
    End Function

    'onAction Handler of Button1  
    Public Sub ButtonOnAction1(control As IRibbonControl)
        btn1State = False
        ribbon.InvalidateControl("t50")  
    End Sub

    'onAction Handler of Button2  
    Public Sub ButtonOnAction2(control As IRibbonControl)
        btn1State = True
        ribbon.InvalidateControl("t50")  
    End Sub

Funktions-Demo:

19d1d250b271364ca641a769537b3c64

Eine Liste der Callback Signaturen findest du hier:
Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)

Grüße Uwe
Member: MarcoBorn
MarcoBorn Sep 29, 2015 at 09:48:53 (UTC)
Goto Top
Hallo Uwe,
ich habe meinen Code entsprechend angepasst. Allerdings bekomme ich bei den beiden Invalidates eine Fehlermeldung, dass der Objektverweis für ribbon nicht auf eine Objektinstanz festgelegt wurde. Ich habe es wie folgt deklariert:
Dim m_rib As IRibbonUI
Public Property Rib() As NetOffice.OfficeApi.IRibbonUI
  Get
    Return m_rib
  End Get
  Set
    m_rib = value
  End Set
End Property
Zusätzlich habe ich (gemäß einem Tutorial) noch folgende Funktion implementiert:
Sub Ribbon_Load(ByVal ribbon As IRibbonUI)
  rib = ribbon
End Sub

Wie kann ich das ribbon-Objekt korrekt anlegen?

Vielen Dank,
Marco
Member: colinardo
colinardo Sep 29, 2015 updated at 10:47:25 (UTC)
Goto Top
Allerdings bekomme ich bei den beiden Invalidates eine Fehlermeldung, dass der Objektverweis für ribbon nicht auf eine Objektinstanz festgelegt wurde
Ist ja auch normal das musst du an deine Umgebung anpassen...
Das globale "ribbon" Objekt müsstest du eigentlich schon haben, aber ich arbeite nicht mit deiner genutzten Umgebung.

In VisualStudio wird das so mit einer Ribbon-Klasse gemacht von der dann im Workbook-Code durch die Override-Function eine Instanz erzeugt wird:
'TODO:  Follow these steps to enable the Ribbon (XML) item:  

'1: Copy the following code block into the ThisAddin, ThisWorkbook, or ThisDocument class.  

'Protected Overrides Function CreateRibbonExtensibilityObject() As Microsoft.Office.Core.IRibbonExtensibility  
'    Return New Ribbon()  
'End Function  

'2. Create callback methods in the "Ribbon Callbacks" region of this class to handle user  
'   actions, such as clicking a button. Note: if you have exported this Ribbon from the  
'   Ribbon designer, move your code from the event handlers to the callback methods and  
'   modify the code to work with the Ribbon extensibility (RibbonX) programming model.  

'3. Assign attributes to the control tags in the Ribbon XML file to identify the appropriate callback methods in your code.  

'For more information, see the Ribbon XML documentation in the Visual Studio Tools for Office Help.  

<Runtime.InteropServices.ComVisible(True)> _
    Public Class Ribbon
    Implements Office.IRibbonExtensibility

    Private ribbon As Office.IRibbonUI

    Public Sub New()
    End Sub

    Public Function GetCustomUI(ByVal ribbonID As String) As String Implements Office.IRibbonExtensibility.GetCustomUI
        Return GetResourceText("ExcelWorkbook1.Ribbon.xml")  
    End Function

#Region "Ribbon Callbacks" 
    'Create callback methods here. For more information about adding callback methods, select the Ribbon XML item in Solution Explorer and then press F1.  
    Public Sub Ribbon_Load(ByVal ribbonUI As Office.IRibbonUI)
        Me.ribbon = ribbonUI
    End Sub



#End Region

#Region "Helpers" 

    Private Shared Function GetResourceText(ByVal resourceName As String) As String
        Dim asm As Reflection.Assembly = Reflection.Assembly.GetExecutingAssembly()
        Dim resourceNames() As String = asm.GetManifestResourceNames()
        For i As Integer = 0 To resourceNames.Length - 1
            If String.Compare(resourceName, resourceNames(i), StringComparison.OrdinalIgnoreCase) = 0 Then
                Using resourceReader As IO.StreamReader = New IO.StreamReader(asm.GetManifestResourceStream(resourceNames(i)))
                    If resourceReader IsNot Nothing Then
                        Return resourceReader.ReadToEnd()
                    End If
                End Using
            End If
        Next
        Return Nothing
    End Function

#End Region

End Class
Grüße Uwe
Member: MarcoBorn
MarcoBorn Sep 29, 2015 at 11:22:02 (UTC)
Goto Top
Hallo Uwe,
ich habe den Bug jetzt gefunden. Die Prozedur "Ribbon_Load(ByVal ribbon As IRibbonUI)" war im XML-Code als "Ribbon_OnLoad" gekennzeichnet. Jetzt funktioniert das Ganze so wie es sein soll.

Vielen Dank für Deine Hilfe, die mich auf den richtigen Weg gebracht hat.

Marco