VBA Multiple Toggle Buttons picture-switching (on MS Visio)

Gingegna

New Member
Joined
Jun 8, 2021
Messages
2
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
Hi, I don't know if that's the right place where asking this, but let's try.

I'm actually working on a Visio Draw wich have tons of icons to be changed manually from users (switching from an "active" state to a "passive" one).
I have been asked for automatize this process with vba, and i started out using Toggle Buttons, as ActiveX Image control's can't trigger click events in visio.
Basically I have Toggle Buttons with a picture loaded in, and change the state of the buttons switch the active and passive state of icons.
As I said, I have hundreds of toggle buttons in this draw and for each of them i would have to write the same macros, except for the Toggle Buttons property Names.
I am using this code for switching the picture in a single button:

VBA Code:
Private Sub TogglePicture1()

    If ToggleButton1.Value = True Then
    
        ToggleButton1.Picture = LoadPicture("C:\path\Active.jpg")
    Else
        ToggleButton1.Picture = LoadPicture("C:\path\Passive.jpg")
End Sub

and this code for call the one above clicking on the button:

VBA Code:
Private Sub ToggleButton1_Click()

    Call TogglePicture1
    
End Sub

I would have to copy this for every button, so I was looking for a way to "learn" the macro who switch the picture to look at wich button was pressed, so that I would have to copy only the click event macros that calls it.

I have tried different ways but I just can't look out how to refer to the toggle button name property on the code, nor how to derivate it from the last button clicked... I'm not so advanced on VBA and I usally use it for Excel and for basic function. I've read something about creating classes but I never went so deep on VBA programming, and I didn't untersand how do them work.

There is a way for give to a variable the property Name of the last clicked button? Or a way to copy a Toggle Button with all of his code, automatically changing the property Name inside of it?

Thank you very much, every tip is really appreciated.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
You could try something like this. You would have to copy each Click function for each button and change the name in quotes as well as the procedure name, but at least there is just one TogglePicture function that all of them call.
VBA Code:
Private Sub ToggleButton1_Click()
    TogglePicture "ToggleButton1"
End Sub

Private Sub ToggleButton2_Click()
    TogglePicture "ToggleButton2"
End Sub

Sub TogglePicture(TogBtn As String)
    Dim sh As Shape
    
    For Each sh In ActiveWindow.Page.Shapes
        If sh.Name = TogBtn Then
            If sh.Object.Value = True Then
                sh.Object.Picture = LoadPicture("C:\path\Active.jpg")
            Else
                sh.Object.Picture = LoadPicture("C:\path\Passive.jpg")
            End If
        End If
    Next sh
End Sub
 
Upvote 0
I have found the solution I was looking for. I'll post it here, if anyone would need it.

I had to use class modules for operating on all the toggles on the page, so here is the code (to put in a class module):

VBA Code:
Private WithEvents mToggle As MSForms.ToggleButton
Property Set ToggleButtn(oButton As MSForms.ToggleButton)
 
    Set mToggle = oButton
    
End Property
Property Get ToggleButtn() As MSForms.ToggleButton
    
    Set ToggleButtn = mToggle
    
End Property
Private Sub mToggle_Click()
If mToggle.Caption = "FACCIA" Then
    If mToggle.Value = True Then
        mToggle.Picture = LoadPicture("C:\Users\GinoGatti\Desktop\FacciaOn.jpg")
    Else
        mToggle.Picture = LoadPicture("C:\Users\GinoGatti\Desktop\FacciaOff.jpg")
    End If

ElseIf mToggle.Caption = "SVEGLI" Then
    If mToggle.Value = True Then
        mToggle.Picture = LoadPicture("C:\Users\GinoGatti\Desktop\SvegliaOn.jpg")
    Else
        mToggle.Picture = LoadPicture("C:\Users\GinoGatti\Desktop\SvegliaOff.jpg")
    End If

End If

End Sub

This is the code that trigger the condition necessary to the toggles for switch the picture, it operate on a class who's filled with the following code:

In a regular module:

VBA Code:
Public gToggles() As CToggles
Sub AddTogglesToClass()
    
    Dim oleToggle As OLEObject
    Dim i As Long
    
    i = 1
    
    For Each oleToggle In ThisDocument.OLEObjects
         If TypeName(oleToggle.Object) = "ToggleButton" Then
             ReDim Preserve gToggles(1 To i)
             Set gToggles(i) = New CToggles
             Set gToggles(i).ToggleButtn = oleToggle.Object
             i = i + 1
         End If
     Next oleToggle
    
End Sub

With this every togglebutton in the document is added to the class, and as written on the class module, any togglebutton in the document will now trigger the "switchpicture" macro.

I've also written on the document code some string for making the toggles part of the class upon opening the document:

VBA Code:
Private Sub Document_DocumentOpened(ByVal doc As IVDocument)
Call AddTogglesToClass
End Sub

I'm sorry if this isn't the better explanation about what I did, i Have to say that I naver worked with class modules and I don't even know the right terminology to use when explaining this kind of things. Hope it would be clear what I did (to me, it isn't, I reached this solution with a lot of voodoo stuff only).
 
Upvote 0
Solution
I have found the solution I was looking for. I'll post it here, if anyone would need it.

I had to use class modules for operating on all the toggles on the page, so here is the code (to put in a class module):

VBA Code:
Private WithEvents mToggle As MSForms.ToggleButton
Property Set ToggleButtn(oButton As MSForms.ToggleButton)
 
    Set mToggle = oButton
   
End Property
Property Get ToggleButtn() As MSForms.ToggleButton
   
    Set ToggleButtn = mToggle
   
End Property
Private Sub mToggle_Click()
If mToggle.Caption = "FACCIA" Then
    If mToggle.Value = True Then
        mToggle.Picture = LoadPicture("C:\Users\GinoGatti\Desktop\FacciaOn.jpg")
    Else
        mToggle.Picture = LoadPicture("C:\Users\GinoGatti\Desktop\FacciaOff.jpg")
    End If

ElseIf mToggle.Caption = "SVEGLI" Then
    If mToggle.Value = True Then
        mToggle.Picture = LoadPicture("C:\Users\GinoGatti\Desktop\SvegliaOn.jpg")
    Else
        mToggle.Picture = LoadPicture("C:\Users\GinoGatti\Desktop\SvegliaOff.jpg")
    End If

End If

End Sub
I read this older thread and I found it helpful and interesting. (I've not quoted the entire message I'm responding to.) But it gave me an idea. Here is my solution, which is quite different: use two toggle buttons, each with a different picture. Overlay them. Make them visible or invisible as the click event.

VBA Code:
Private Sub ToggleButtonUSD_Click()

    With Me.ToggleButtonUSD
        If .Visible = True Then
            .Visible = False
            .Value = False
            Me.ToggleButtonEUR.Visible = True
        End If
    End With
End Sub

Private Sub ToggleButtonEUR_Click()

    With Me.ToggleButtonEUR
        If .Visible = True Then
            .Visible = False
            .Value = False
            Me.ToggleButtonUSD.Visible = True
        End If
    End With
End Sub

I put a small, ca. 15-second video of what the user form with the toggle buttons looks like here:

(I hope it's okay to do that. I'm not trying to draw anyone away from this forum, which I think is great.)

The values I show in that tiny clip are just dummy values. I'm not done coding the user form. It's been a great learing experience so far, in any case.

/dr
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,465
Members
448,965
Latest member
grijken

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top