Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Periodically someone asks me whether there’s a way to automatically convert all of the pages in a Visio drawing to PowerPoint slides. There have even been a few times when I’ve wanted to do that myself but I never really had enough incentive to figu…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

772 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