Solved

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

Posted on 2014-01-21
7
1,032 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Object to array 7 28
Running Visio Macro from VBS File 3 33
Round up to 100% in .NET 10 43
How to make an ADE file by code? 11 35
The ability to add structure to Visio diagrams using containers, lists and callouts is one of my favorite features in Visio 2010. In this article I’ll describe a mashup of containers and lists that meet a real-world need. Prior to reading this ar…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now