How to get the value supplied in the OnRibbonLoaded Event?

Add-in Express™ Support Service
That's what is more important than anything else

How to get the value supplied in the OnRibbonLoaded Event?
 
Leon Lai Kan




Posts: 200
Joined: 2018-12-20
Hi, Andrei

I have several Tabs on my Excel Menu.
These Tabs (SR1, SR2, SR3) represent my Excel add-in.

Suppose a user Clicks on SR2 tab. He will be shown a number of buttons on the Ribbon.
Suppose he clicks on any button to run some Sub.

I want SR2 to remain the active Tab after the user has clicked any button.

How to do this?

I learn there are 2 ways of doing this:

(a) Using Sendkeys to activate the SR2 tab.

 ExcelApp.SendKeys("%Y2%")

This works well!


(b) Using ActivateTab()
I wish to try this method also, but I am having some problems


My Code

Dim myRibbon As IRibbonUI
myRibbon.ActivateTab("SR2")


myRibbon is highlighted as an error:
Variable 'myRibbon' is used before it has been assigned a value.
A null reference exception could result at runtime


What value should we assign to myRibbon?


I found an answer here:

We must assign the value supplied in the OnRibbonLoaded Event

I have no idea how to get this value. Could you help?

Thanks
Leon
Posted 29 Apr, 2019 03:01:00 Top
Andrei Smolin


Add-in Express team


Posts: 16739
Joined: 2006-05-11
Hello Leon,

Leon Lai Kan writes:
I want SR2 to remain the active Tab after the user has clicked any button.


I'm stupmed. You ask this question for the second time. My understanding is: you must do something to get some other tab activated; if you do not do this, your tab remains active.

Say, if the code behind your button selects a shape, table, etc. Excel will show a context tab specific to the selection. In this specific scenario, re-activating your tab is *not* user-friendly as the user expects this behavior from Excel.

To activateyour tab, you should call {my ribbon tab variable such as Dim myTab1 as AddinExpress.MSO.ADXRibbonTab}.Activate().

Leon Lai Kan writes:
Variable 'myRibbon' is used before it has been assigned a value.


I regard this error message as an indication that you use that variable *before* you have a right to do so; you can do such calls *after* OnRibbonLoaded event is processed.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 29 Apr, 2019 06:39:44 Top
Leon Lai Kan




Posts: 200
Joined: 2018-12-20
Hi, Andrei

I am not really asking the SAME question a second time, although it relates to the SAME problem.

I understand perfectly what you explained to me in my previous post.

So, what I did was as follows:

At the end of the Sub, the Ribbon tab may have shifted.
I am not going to worry about what my Sub may be doing.

What I did was to add this line just before End Sub:
ExcelApp.SendKeys("%Y2%") 


So, whatever my Sub does, this line of code returns the tab to SR2 - which is exactly what I wanted.

----

My question is:
Is there another way of doing the same thing instead of using SendKeys?

In one of the threads you yourself answered, you suggested to use ActivateTab.

Hi Karthick,

You need to create a variable of the IRibbonUI type on the class level and assaign it the value supplied in the OnRibbonLoaded event. Then you call IRibbonUI.ActivateTab(ADXRibbonTab.Id) in the AddinStartupComplete event.

Andrei Smolin




My question is:
How to find the value supplied in the OnRibbonLoaded event



Andrei, kindly reread my previous thread. It is completely different from my present question.
None of the technical terms I am using here appear in my previous thread. The title itself is completely different.


Best Regards,
Leon
Posted 29 Apr, 2019 08:11:09 Top
Andrei Smolin


Add-in Express team


Posts: 16739
Joined: 2006-05-11
Leon,

Okay.

Instead of calling ExcelApp.SendKeys("%Y2%") or IRibbonUI.ActivateTab(ADXRibbonTab.Id), I suggest that you call {a variable of the ADXRibbonTab type}.Activate() e.g. Me.adxRibbonTab1.Activate() where Me is a reference to the add-in module.

Is this what you are looking for? If not, please explain.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 29 Apr, 2019 09:21:29 Top
Leon Lai Kan




Posts: 200
Joined: 2018-12-20
Andrei,

Your suggestion works fine!

I looked at AddinModule.Designer.vb.

In Private Sub InitializeComponents, I see:

Me.SR2 = New AddinExpress.MSO.ADXRibbonTab


So, following your suggestion, I simply added this line just before End Sub:

Me.SR2.Activate


So, whatever may happen during processing, at the end my add-in always returns me to the Tab I started with (SR2).

This is exactly what I wanted!


Best Regards,
Leon
Posted 30 Apr, 2019 01:57:01 Top
Andrei Smolin


Add-in Express team


Posts: 16739
Joined: 2006-05-11
Hello Leon,

Leon Lai Kan writes:
I simply added this line just before End Sub


If the Sub is the InitializeComponents method, then I believe Me.SR2.Activate() shouldn't do anything. To indicate this, it returns False. As explained, such calls make sense only after your add-in module receives the OnRibbonLoaded event.

Actually, a tab defined by an ADXRibbonTab component gets activated if all of these conditions are met:
- The ADXRibbonTab component is listed in the components collection of the add-in module; to achieve this, you create such a component via Me.adxRibbonTab1 = New AddinExpress.MSO.ADXRibbonTab(Me.components).
- The version of Office is 2010 or greater.
- No exception occurs when ribbonUI.ActivateTabMso(controlID) or ribbonUI.ActivateTab(controlID) is called. Naturally, if you call that method too early, the ribbonUI variable is not set and your attempt to activate the tab throws an exception which is "swallowed" by Add-in Express.

So, if the ADXRibbonTab is properly created, if the Office version is correct, and if the OnRibbonLoaded event has been raised, Me.SR2.Activate() sends Office a signal to activate the tab and returns True. Otherwise, it returns False.

As to the actual tab activation, this is the responsibility of Office who may or may not activate the tab depending on its inner logic. In particular, if you activate a tab in an inactive window, Office will activate the tab only when the window becomes active.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 30 Apr, 2019 02:36:21 Top
Leon Lai Kan




Posts: 200
Joined: 2018-12-20
Andrea,

Thanks a lot for your great explanation and for pointing out the limitations of my code. Though it will take me some time to understand it all. I am still much a beginner in ADX.

For the moment, it seems to work for me, and it seems the conditions you have listed do apply in my situation:

(a) The ADXRibbonTab component is auto-generated by Add-In Express. I do not use the code to activate an inbuilt Excel tab.

(b) I use Excel 2016.

© For the moment, it does not appear an exception is created.

I take good note of all your suggestions, and may revise my strategy if problems happen.

Best Regards,
Leon
Posted 30 Apr, 2019 03:00:38 Top
Andrei Smolin


Add-in Express team


Posts: 16739
Joined: 2006-05-11
Leon Lai Kan writes:
(3) For the moment, it does not appear an exception is created.


You'll never receive that exception. Instead, Me.SR2.Activate() will return False.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 30 Apr, 2019 03:06:59 Top