Function ActivateRibbonItem(ByVal sItem As String) As Boolean
Dim oParent As C_AccEx, oChild As IAccElement
Set oParent = New C_AccEx
Call oParent.GetAccessibleChildrenFromHwnd(Application.hwnd, TreeScope_Subtree)
If oParent.ElementsCount Then
For Each oChild In oParent.Items
With oChild
If LCase(.Name) = LCase(sItem) Then
If .IsEnabled = False Then .IsEnabled = True
.DoDefaultAction
ActivateRibbonItem = True
End If
End With
Next oChild
End If
' Stop
End Function
No. It won't work for enabling ribbon items that are currently disabled.I see that it does not work on disabled buttons, can this method enable the button before clicking on it?
@abc_xyzI opted for the first method as I was getting some error with the second approach. Below is the final sub procedure which I want to make it perfect. However, ended up with more errors on line:
Any idea on how do I make it work on other workbooks?
VBA Code:If Element.accRole(CHILDID_SELF) = RoleWanted _ And Element.accName(CHILDID_SELF) = NameWanted Then
With the second approach, I was able to make it work. However, it was very slow and caused my Excel application to crash (tried on other system and the results were the same). Would really appreciate if you could help me debug the first approach or guide me on how to do that.@abc_xyz
I too get the same error with that code. I am afraid, I won't have time to debug it.
You say you were getting some error with the second (Class) approach but you didn't say which error and where in the code the error is happening.
As I said, earlier in post#16, using the C_AccEx Class is much easier, doesn't require adding any external library references and shouldn't raise any errors.
I explained in post#18, the steps you will need to follow to use the Class and then it is just a simple case of calling the ActivateRibbonItem function.
That's probably caused by a timing issue. I think the opened workbooks should be fully loaded before trying to click on the addin ribbon button.With the second approach, I was able to make it work. However, it was very slow and caused my Excel application to crash (tried on other system and the results were the same). Would really appreciate if you could help me debug the first approach or guide me on how to do that.