• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1130
  • Last Modified:

Visio VBA - Assigning 4 macros to 4 buttons throughout a visio file

I would like to have four macro buttons on every page of my 23 page visio file.

CommandButton1_click()
CommandButton2_click()
CommandButton3_click()
CommandButton4_click()

I attached code to each command button click event and they all work on one page but when I copy those same four buttons to another page.  I get command button5, 6, 7, 8.
When I try to rename the newly pasted button from command button5 back to commandbutton 1, I get "Ambiguous Name Detected Command Button 1"
I don't want to reprogram or copy code over and over again.
I want to invoke those same four code modules regardless of what page they're on.
0
brothertruffle880
Asked:
brothertruffle880
  • 2
  • 2
1 Solution
 
COACHMAN99Commented:
does it matter what the button is called? surely you will call the same code/function regardless?
e.g.
CommandButton1_click()
   functionA()

CommandButton2_click()
   functionA()

CommandButtonNN_click()
   functionA()
0
 
brothertruffle880Author Commented:
Doesn't matter what the buttons are called but I'm trying to avoid typing code over and over.
or pasting code over and over I have 28 pages.  multiply that by four buttons 28 x 4 = 112 event procedures!

Here is the code I want to attach to each button:
 
Dim pg As Visio.Page
  Set pg = Visio.ActivePage
  Call m_showAndHideLayers(pg, LayerName_Mgmt$)
  Set pg = Nothing

Open in new window


Of course MGMT$ will change because each button will invoke a different layer.
Dim pg As Visio.Page
  Set pg = Visio.ActivePage
  Call m_showAndHideLayers(pg, LayerName_Wireless$)
  Set pg = Nothing

Open in new window

etc.
0
 
COACHMAN99Commented:
got it. didn't know you were referencing the current page in your code.
0
 
CodeCruiserCommented:
See if this works for you

http://spreadsheetpage.com/index.php/tip/handle_multiple_userform_buttons_with_one_subroutine/

Copied here:

When you create a UserForm, every control on the form must have its own event handler procedure. For example, if a UserForm has 12 CommandButtons, you need 12 procedures to handle the click events for those buttons.

This tip describes a way around this limitation by using a Class Module to define a new class.

Procedure

Create your UserForm as usual, and add several CommandButtons.
Insert a Class Module and enter the following code. You will need to customize the ButtonGroup_Click subroutine.
Public WithEvents ButtonGroup As CommandButton

Private Sub ButtonGroup_Click()
    MsgBox "Hello from " & ButtonGroup.Name
End Sub
Insert a normal module and enter the following code. In this code, I exclude a button named OKButton from the "button group." Therefore, clicking the OK Button does not execute the ButtonGroup_Click subroutine.
Option Explicit
Dim Buttons() As New Class1

Sub ShowDialog()
    Dim ButtonCount As Integer
    Dim ctl As Control

'   Create the Button objects
    ButtonCount = 0
    For Each ctl In UserForm1.Controls
        If TypeName(ctl) = "CommandButton" Then
            If ctl.Name <> "OKButton" Then 'Skip the OKButton
                ButtonCount = ButtonCount + 1
                ReDim Preserve Buttons(1 To ButtonCount)
               Set Buttons(ButtonCount).ButtonGroup = ctl
            End If
        End If
    Next ctl
    UserForm1.Show
End Sub
Execute the ShowDialog subroutine to display the UserForm. Clicking any of the CommandButtons (except the OKButton) executes the ButtonGroup_Click subroutine.
Note: To use this technique with other types of controls, change the WithEvents statement. For example:

   Public WithEvents LabelGroup As MSForms.Label
Notice that you must qualify the Label object because Excel also has an object named Label. Also, you will need to make appropriate changes throughout the ShowDialog procedure.
0
 
brothertruffle880Author Commented:
Hi CodeCruiser
This is a useful solution and I will use it But my problem is different.
I have awarded you the points because it's a terrific piece of code.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now