Solved

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

Posted on 2014-01-21
7
1,070 Views
Last Modified: 2014-01-24
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
Comment
Question by:brothertruffle880
  • 2
  • 2
7 Comments
 
LVL 7

Expert Comment

by:COACHMAN99
ID: 39797925
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
 

Author Comment

by:brothertruffle880
ID: 39798026
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
 
LVL 7

Expert Comment

by:COACHMAN99
ID: 39798758
got it. didn't know you were referencing the current page in your code.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39806233
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
 

Author Comment

by:brothertruffle880
ID: 39806498
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question