switch to HOME tab

Started by perry59, January 31, 2024, 09:11:42 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

perry59

I have a form which when activated I would like to switch to the "HOME" tab on the ribbon. sounds simple but I have not found any examples. Can someone give some pointers on this?
Thanks
what, me worry?

Yacine

Doesn't sound simple at all. You would probably need to check Microsofts doumentation and search whether they have implemented an activation command for ribbons. That is not a Visio issue but a general "Office" one.
If so, implementing it in Visio should be easy.
Yacine

Surrogate

Try use ActivateTabMso method, with syntax like:
ribbon.ActivateTabMSO "Home"

perry59

Quote from: Surrogate on February 02, 2024, 02:04:37 PM
Try use ActivateTabMso method, with syntax like:
ribbon.ActivateTabMSO "Home"

that looks easy enough, my only problem is it requires a control as input and I don't know how to get the control that represents the default ribbon
what, me worry?

perry59

Quote from: Yacine on February 02, 2024, 01:07:05 PM
Doesn't sound simple at all. You would probably need to check Microsofts doumentation and search whether they have implemented an activation command for ribbons. That is not a Visio issue but a general "Office" one.
If so, implementing it in Visio should be easy.
true, it is an office thing, not really visio
what, me worry?

perry59

well I may just give up on this, it wasn't real important just a nicety.
tried several things without success, the easiest was just using "sendkeys" such as:
My.Computer.Keyboard.SendKeys("%H")
but even that didn't work (funny though it DID activate "Help" in visual studio while debugging!)
what, me worry?

Nikolay

#6
I could suggest an approach that sounds crazy enough to work  ;D

1. You create a custom ribbon with XML from a string, with invisible control on the "Home" tab
2. You register that ribbon with Visio (using RegisterRibbonX).
3. You hook up to the "load" event to get hold of the ribbon reference
4. You call the "ActivateTab" for that thing.
5. You unregister and delete this ribbon.

Another crazy idea:

You cna use Windows Accessiblity API to access the ribbon. The tab is exposed as a control.

---

WHY is it so hard (IMHO). To prevent you (application developer) from messing up the UI.
We had enough of that with CommandBars when different extensions were fighting each other,
effectively messing up each other's user interface, and the customizations user has made.

Thinking more philosophically, why would you want to control something you don't own (and you don't own the "Home" tab), to start with? Isn't that a bad idea? :D

Nikolay

#7
Attached is a sample file that ALWAYS activates the "Home" tab when opend that uses that first approach.
It seems to actually work.

ThisDocument

Dim cls1 As Class1

Private Sub Document_DocumentOpened(ByVal doc As IVDocument)

Set cls1 = New Class1
Application.RegisterRibbonX cls1, ActiveDocument, visRXModeDrawing, "my"
Application.UnregisterRibbonX cls1, ActiveDocument

End Sub


Class1 (class module)

Implements IRibbonExtensibility

Private Function IRibbonExtensibility_GetCustomUI(ByVal RibbonID As String) As String
IRibbonExtensibility_GetCustomUI = "<customUI xmlns=""http://schemas.microsoft.com/office/2006/01/customui"" onLoad=""onRibbonLoaded""><ribbon><tabs><tab idMso=""TabHome"" ></tab></tabs></ribbon></customUI>"
End Function

Public Sub onRibbonLoaded(ribbon As IRibbonUI)
    ribbon.ActivateTabMso "TabHome"
End Sub


Hmm looks like it's the same death clock I have invented last year (c) prof. Farnsworth.
Started forgetting things, lol. But this time it seems to actually work

perry59

Quote from: Nikolay on February 02, 2024, 08:08:40 PM

WHY is it so hard (IMHO). To prevent you (application developer) from messing up the UI.
We had enough of that with CommandBars when different extensions were fighting each other,
effectively messing up each other's user interface, and the customizations user has made.

Thinking more philosophically, why would you want to control something you don't own (and you don't own the "Home" tab), to start with? Isn't that a bad idea? :D

Thanks Nikolay, you da visio Professor!
I don't want to own or interact with the home tab other than just activating it. The reason is in my application I have a form (modal) to allow the user to draw "cables" 
which are just two or more connectors that will be associated in a backend database as a single cable. the form is invoked from my custom ribbon (thanks again to your awesome visual studio template). So when the form is displayed I thought it would be nice to automatically switch to the "home" tab where the connector command is rather than making the user do that.
Not a "must have" thing at all, I just thought it would be a nice touch.
Have a great weekend!
what, me worry?

Nikolay

So did you try the approvach above? Would love to get some feedback if it worked for you.
Have a nice weekend too.

perry59

Quote from: Nikolay on February 02, 2024, 11:54:02 PM
So did you try the approvach above? Would love to get some feedback if it worked for you.
Have a nice weekend too.

Sorry!
Yes I did try it, interesting but it works!
I will incorporate that into my addin, thank you very much Nikolay!
what, me worry?

Nikolay

Hm If it is about add-in probably you already have some user interface, then you cna just call that method (ribbon.ActivateTabMso) on the ribbon.
I mean all dancing around was actually to call that.

perry59

I spoke too soon, this code worked fine as embedded vba, but I'm having issues getting it to work in my addin (nothing happens)
What I have so far:
I have a module named "commands" where I define my commands and any global variables, it looks like this (stripped out all other stuff)

Imports System.Windows.Forms
Imports System.Collections
Imports System.Data
Imports visio = Microsoft.Office.Interop.Visio
Imports System.Diagnostics

Module Commands
Public clsl As ribbonMod '= Nothing
Public Sub TabActivate()

        clsl = New ribbonMod

        Globals.ThisAddIn.Application.RegisterRibbonX(clsl, Globals.ThisAddIn.Application.ActiveDocument, visio.VisRibbonXModes.visRXModeDrawing, "my")
        Globals.ThisAddIn.Application.UnregisterRibbonX(clsl, Globals.ThisAddIn.Application.ActiveDocument)
    End Sub
End Module


then I have the class module:

Option Explicit On

Imports System.Runtime.InteropServices
Imports Microsoft.Office.Core


<ComVisible(True)>
Public Class ribbonMod
    Implements IRibbonExtensibility

    Dim ribbonUI As IRibbonUI
    Dim ribbonXML As String = String.Empty


    Public Sub New()
        MsgBox("new ribbonMod")
    End Sub

    Private Function IRibbonExtensibility_GetCustomUI(ByVal RibbonID As String) As String Implements IRibbonExtensibility.GetCustomUI

        ribbonXML = "<customUI xmlns=""http://schemas.microsoft.com/office/2006/01/customui"" onLoad=""onRibbonLoaded"">"
        ribbonXML = ribbonXML & "   <ribbon>"
        ribbonXML = ribbonXML & "      <tabs>"
        ribbonXML = ribbonXML & "        <tab idMso=""TabHome"" >"
        ribbonXML = ribbonXML & "        </tab>"
        ribbonXML = ribbonXML & "      </tabs>"
        ribbonXML = ribbonXML & "   </ribbon>"
        ribbonXML = ribbonXML & "</customUI>"

        IRibbonExtensibility_GetCustomUI = ribbonXML
    End Function

    Public Sub onRibbonLoaded(ribbon As IRibbonUI)
        ribbon.ActivateTabMso("TabHome")
    End Sub

End Class


finally, I call it from my form like such:

Commands.TabActivate()

there are no compile errors, but stepping through the code when it enters the class definition the only function/sub that executes is the "new" sub I put in for debugging. In my watch window both "clsl" (the class) and "ribbonXML" are empty. I do not even see how these functions/sub even get called (except for "new")
what, me worry?

Nikolay

Yes, with an add-in this could be different (most probably the "onLoad" is not called, because Visio thinks it's a VBA function or something)
I would suggest you go with easier approach for the add-in.

You cna just add an (empty) ribbon (with visual studio designer for example), and then use "convert to xml", unless you already have one in your project.
Then in that "stock" ribbon you call the "ActivateTab".

perry59

Quote from: Nikolay on February 06, 2024, 11:35:47 AM
Yes, with an add-in this could be different (most probably the "onLoad" is not called, because Visio thinks it's a VBA function or something)
I would suggest you go with easier approach for the add-in.

You cna just add an (empty) ribbon (with visual studio designer for example), and then use "convert to xml", unless you already have one in your project.
Then in that "stock" ribbon you call the "ActivateTab".

Thanks Nikolay!
got it working!

In the "AddinRibbon.vb", which was part of the Addin (from your template) I just added this simple sub:

Public Sub GoHome()
        _ribbon.ActivateTabMso("TabHome")
    End Sub


and in my form I call it like this:

Globals.ThisAddIn.AddinUI.GoHome()

and it works a charm
Thanks again!
what, me worry?