fernandreu / office-ribbonx-editor

An overhauled fork of the original Custom UI Editor for Microsoft Office, built with WPF
MIT License
523 stars 103 forks source link

Make Custom Tab Active on Launch Instruction #64

Closed scienceguyroy closed 5 years ago

scienceguyroy commented 5 years ago

Hello,

I am new to github, and I'm not sure about the appropriateness of this question. If this is the wrong forum, I apologize. I was hoping to find instructions, how you could make the custom tab be the active tab when Excel is opened? In other words, when the Excel file with the custom tab is opened, the custom tab is what the user would be seeing (as opposed to the Home tab).

Thank you in advance for any help!

Sincerely, Roy

fernandreu commented 5 years ago

Hi Roy,

This site is for anything that is directly related to the functionality of the Office RibbonX Editor: a bug you have found, something that is not quite ideal and you would like changed, a new feature you thought it would be useful to add, etc. Basically, anything that would require me (or somebody else) look at the code of the tool itself and change it.

Your question is actually more related to Excel / VBA and not the tool itself, but it is a relatively straightforward one, so I can still help you. For other similar questions, I would usually try Excel-specific sites, such as MrExcel, the Excel subreddit or StackOverflow. That way, there can be more people looking at your problem and trying to help out, increasing your chances of having a successful response.

Basically, you need to have a callback to the onLoad method of the top-level customUI element, such as:

<customUI xmlns="..." onLoad="OnCustomUILoad">
    <ribbon>
        <tabs>
            <tab id="customTabId" label="Custom Tab">
                ...
            </tab>
        </tabs>
    </ribbon>
</customUI>

Then, on a VBA module, you add something like this:

Sub OnCustomUILoad(ribbon As IRibbonUI)
    ribbon.ActivateTab "customTabId"
End Sub

If you want to activate the custom tab at any other time later, you can just store that ribbon variable at a module level, e.g.:

Option Explicit

Private mRibbon As IRibbonUI

Sub OnCustomUILoad(ribbon As IRibbonUI)
    Set mRibbon = ribbon
End Sub

Sub ActivateTab()
    'mRibbon can be Nothing if the module is recompiled since the custom UI was loaded
    If mRibbon Is Nothing Then Exit Sub
    mRibbon.ActivateTab "customTabId"
End Sub

Regards, Fernando