Switch to Custom Tab in Ribbon with VBA

Started by nmxtch, July 28, 2023, 11:56:29 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

nmxtch

Hello,

I'm pulling my hair out trying to figure this one out. My goal is to switch to my custom tab in the ribbon when I import an image. Here's my code:

Public myRibbon As IRibbonUI

Private Sub Document_ShapeAdded(ByVal vsoShape As Visio.IVShape)

    'Check whther a shape is selected
    If ActiveWindow.selection.Count > 0 Then
       
        'Define objects
        Dim shape As shape
        Dim selection As selection
       
        'Assign the current selection of shapes to multi-variable
        Set selection = ActiveWindow.selection
       
        'Assign shapes listed in multi-variable to thier own variables
        Set shape = selection(1)
       
        'Check whether the shape is a picture
        If shape.CellExists("ClippingPath", 1) Then
       
            myRibbon.ActivateTab "mso_c1.178AE05F"
       
        End If
   
    End If

End Sub


But when I add an image, I get the following error:

QuoteRun-time error '91':

Object variable or With block variable not set

This code comes from the Microsoft website:

https://learn.microsoft.com/en-us/office/vba/api/office.iribbonui.activatetabq

I've tried numerous other iterations of this code but nothing seems to work. I've seen some snippets where they inlclude:

Set MyRibbon = ribbon

But that doesn't fix the error either, and I'm not sure what it's supposed to do anyway because I don't see "ribbon" called anywhere else.

Any ideas?

Nikolay

Correct, you need to set myRibon to your ribbon; otherwise it is set to nothing (not set).
How do you create your ribbon? The "ribbon" that you have seen is supposed to be the ribbon you created.

nmxtch

I'm sorry, I don't follow what you said. This sounds like a chicken and egg problem.

QuoteThe "ribbon" that you have seen is supposed to be the ribbon you created.

Can you elaborate? Is there a proper name for the ribbon? When I try to run the code and it stops in debug mode, if I hover over MyRibbon, the tooltip says "MyRibbon = none". Why is it none if it is set to "ribbon"? What is supposed to go there?

Surrogate

#3
Quote from: nmxtch on July 28, 2023, 11:56:29 PMThis code comes from the Microsoft website:

https://learn.microsoft.com/en-us/office/vba/api/office.iribbonui.activatetabq

I've tried numerous other iterations of this code but nothing seems to work.
This hyperlink for method ActivateTabQ, not for ActivateTab
Quote from: nmxtch on July 28, 2023, 11:56:29 PM
            myRibbon.ActivateTab "mso_c1.178AE05F"
In our Company's corporative MS Word template for activate custom tab used syntax with parentheses... It looks like
FormRibb.ActivateTab ("ct1")

Nikolay

#4
Quote from: nmxtch on July 29, 2023, 04:33:26 AM
I'm sorry, I don't follow what you said. This sounds like a chicken and egg problem.

QuoteThe "ribbon" that you have seen is supposed to be the ribbon you created.

Can you elaborate? Is there a proper name for the ribbon? When I try to run the code and it stops in debug mode, if I hover over MyRibbon, the tooltip says "MyRibbon = none". Why is it none if it is set to "ribbon"? What is supposed to go there?

What is a "ribbon" here? It is not defined by Visio, it is supposed to be defined by you.  There is no built-in keyword to access "current ribbon" or anything like this.
I.e. you should set this variable to the ribbon (fragment) you created. Have you created the tab you are trying to activate?
If yes, how did you create your custom tab?

nmxtch

Quote from: Surrogate on July 29, 2023, 08:06:33 AM
This hyperlink for method ActivateTabQ, not for ActivateTab

I've tried ActivateTab, ActivateTabQ, and ActivateTabMso. The one I pasted here was just whichever one I was trying last.

Quote from: Surrogate
In our Company's corporative MS Word template for activate custom tab used syntax with parentheses... It looks like
FormRibb.ActivateTab ("ct1")

What is "ct1?" Is that the name or ID of your tab? I've tried parantheses, quotes, and equals (because sometimes the error says it expected an = sign).

nmxtch

Quote from: Nikolay on July 29, 2023, 09:19:25 AM
What is a "ribbon" here? It is not defined by Visio, it is supposed to be defined by you.  There is no built-in keyword to access "current ribbon" or anything like this.
I.e. you should set this variable to the ribbon (fragment) you created. Have you created the tab you are trying to activate?
If yes, how did you create your custom tab?

I think I might understand now. I didn't realize "ribbon" was something that didn't already exist as an object because the ribbon at the top of the window *does* exist (It has the Home, Insert, Data, etc tabs). That's where I was getting confused -- I didn't create that ribbon, I only customized it. If I understand you correctly, what I think of as my custom tab inside the ribbon is actually a ribbon itself.

Okay, so now that I understand that (I think), I created my custom tab by right clicking on the ribbon, selecting "Customize Ribbon..." and dropping the collection of buttons I use most on it. After that, I exported it, edited the XML directly to add some macros, and then re-imported it.

The human-readable name of my tab (or ribbon, I guess?) is "Home Custom". Here's the XML for it, if this helps:

<mso:cmd app="Visio" dt="1" />
<mso:customUI xmlns:mso="http://schemas.microsoft.com/office/2009/07/customui">
    <mso:ribbon>
        <mso:qat>
            <mso:sharedControls>
                <mso:control idQ="mso:FileNewDefault" visible="false"/>
                <mso:control idQ="mso:FileOpenUsingBackstage" visible="false"/>
                <mso:control idQ="mso:FileSendAsAttachment" visible="false" insertBeforeQ="mso:FileSave"/>
                <mso:control idQ="mso:FilePrintQuick" visible="false" insertBeforeQ="mso:FileSave"/>
                <mso:control idQ="mso:PointerModeOptions" visible="false" insertBeforeQ="mso:FileSave"/>
                <mso:control idQ="mso:Undo" visible="true" insertBeforeQ="mso:FileSave"/>
                <mso:control idQ="mso:RedoOrRepeat" visible="true" insertBeforeQ="mso:FileSave"/>
                <mso:control idQ="mso:ShapesObjectCombine" visible="true" insertBeforeQ="mso:FileSave"/>
                <mso:control idQ="mso:FileSave" visible="true"/>
                <mso:control idQ="mso:Spelling" visible="true"/>
                <mso:control idQ="mso:PictureInsertFromFile" visible="true"/>
            </mso:sharedControls>
        </mso:qat>
        <mso:tabs>
            <mso:tab id="mso_c1.178AE05F" label="Home Custom" insertBeforeQ="mso:TabInsert">
                <mso:group idQ="mso:GroupFont"/>
                <mso:group idQ="mso:GroupParagraph"/>
                <mso:group idQ="mso:GroupTools"/>
                <mso:group id="mso_c3.178BC3C9" label="Styles" autoScale="true">
                    <mso:gallery idQ="mso:FillColorGallery" showInRibbon="false" visible="true"/>
                    <mso:gallery idQ="mso:LineColorGallery" showInRibbon="false" visible="true"/>
                </mso:group>
                <mso:group id="mso_c5.178CB5F9" label="Arrange" autoScale="true">
                    <mso:gallery idQ="mso:AlignGallery" showInRibbon="false" visible="true"/>
                    <mso:gallery idQ="mso:PositionMenu" showInRibbon="false" visible="true"/>
                    <mso:gallery idQ="mso:ShapesRotateGallery" imageMso="GreenBall" showInRibbon="false" visible="true"/>
                    <mso:control idQ="mso:ObjectBringToFrontMenu" visible="true"/>
                    <mso:control idQ="mso:ObjectSendToBackMenu" visible="true"/>
                </mso:group>
                <mso:group id="mso_c6.178DBCAB" label="Layers" autoScale="true">
                    <mso:control idQ="mso:LayerDialog" visible="true"/>
                    <mso:control idQ="mso:LayerPropertiesDialog" visible="true"/>
                    <mso:dropDown id="MyDropDown"
                              sizeString="AAAAAAAAAAAA"
                              onAction="DropDown_OnAction"
                              getEnabled="DropDown_OnGetEnabled"
                              getLabel="DropDown_OnGetLabel"
                              getSelectedItemIndex="DropDown_OnGetSelectedItemIndex"
                              getShowLabel="DropDown_OnGetShowLabel"
                              getVisible="DropDown_OnGetVisible"
                              imageMso="Lock">
                        <mso:button id="archLock" label="Architectural Drawing" imageMso="Lock" onAction="ArchitecturalLock"/>
                        <mso:button id="bldgLock" label="Structure" imageMso="Lock" onAction="BuildingLayerLock"/>
                        <mso:button id="bldgFillLock" label="Building Fill" imageMso="Lock" onAction="BuildingFillLayerLock"/>
                        <mso:button id="cctvLock" label="Camera Layer" imageMso="Lock" onAction="CameraLayerLock"/>
                        <mso:button id="archUnlock" label="Architectural Drawing" imageMso="IconPencilTool" onAction="ArchitecturalUnlock"/>
                        <mso:button id="bldgUnlock" label="Structure" imageMso="IconPencilTool" onAction="BuildingLayerUnlock"/>
                        <mso:button id="cctvUnlock" label="Camera Layer" imageMso="IconPencilTool" onAction="CameraLayerUnlock"/>
                    </mso:dropDown>
                </mso:group>
                <mso:group id="mso_c7.178DE968" label="Operations" autoScale="false">
                    <mso:control idQ="mso:ShapesObjectUnion" visible="true"/>
                    <mso:control idQ="mso:ShapesObjectSubtract" visible="true"/>
                    <mso:control idQ="mso:ShapesObjectIntersect" visible="true"/>
                    <mso:control idQ="mso:ShapesObjectCombine" visible="true"/>
                    <mso:control idQ="mso:ShapesObjectFragment" visible="true"/>
                    <mso:control idQ="mso:ShapesObjectJoin" visible="true"/>
                    <mso:control idQ="mso:ShapesObjectTrim" visible="true"/>
                    <mso:control idQ="mso:ShapesObjectOffset" visible="true"/>
                    <mso:control idQ="mso:ObjectsGroupMenu" visible="true"/>
                    <mso:control idQ="mso:ObjectsAddToGroup" imageMso="GreenBall" visible="true"/>
                </mso:group>
                <mso:group id="mso_c8.178FD162" label="Images" autoScale="true">
                    <mso:control idQ="mso:PictureInsertFromFile" visible="true"/>
                    <mso:control idQ="mso:CropTool" visible="true"/>
                    <mso:control idQ="mso:PictureFormatDialogVisio" visible="true"/>
                    <mso:button id="cropFade" label="Crop Image" imageMso="MacroPlay" onAction="CropFade"/>
                    <mso:button id="scaleAdjust" label="Adjust Drawing Scale" imageMso="DrawingCanvasScale" onAction="ScaleAdjust"/>
                </mso:group>
            </mso:tab>
        </mso:tabs>
    </mso:ribbon>
</mso:customUI>


That came directly out of Visio. The other thing that threw me off was that everything started with "mso" whereas none of the examples I found online did. Given the code above, what should I be setting Set MyRibbon = ribbon to?

Nikolay

#7
I see. I don't think it is possible to activate a ribbon tab you created by customizing the ribbon manually.
But maybe it is. I have never done it, at least. But usually, you use "onLoad" event on the ribbon, where you get the ribbon object (as a parameter).
That is the ribbon object on which you can use "ActivateTab" method. I'm not sure, if this will work with exported/imported ribbon, though.

I have seen that for a VBA solutions, some people are using RegisterRibbonX() API. Maybe this could be the way to go?

Surrogate

Quote from: nmxtch on July 29, 2023, 02:49:25 PMWhat is "ct1?" Is that the name or ID of your tab?
Yes, ct1 (Customise Table) is custom tab.
Our template have 3 custom tabs:
- 1st contain buttons for text operations;
- 2nd for pictures;
- 3rd for tables.
When user selected table or some cells automatically activated "ct1" tab. If selected picture then activated tab for operations with pictures. And when pointer in text then activated tab for text operations...