Solved

VBA map multiple buttons to the same click event

Posted on 2010-11-18
2
881 Views
Last Modified: 2012-05-10
Let's say I have one hundred label controls in a Word document and I want a message box or something to happen when I click on them. The message box should display the caption of the label.

I don't want to add code to one hundred labels one at a time. I don't really want to add code for 100 _Click event programmatically either.
If it helps the labels are in a table. No I'm not going to put it in Excel.
ThisDocument doesn't seem to have a SelectionChange or OnClick event or that could work.
Is there a way to do this?
0
Comment
Question by:TommySzalapski
2 Comments
 
LVL 37

Author Comment

by:TommySzalapski
ID: 34170724
Oh. There is a SelectionChange event. It's just more complicated than the Excel one.
You add a class module with code like this.
Public WithEvents App As Word.Application

Private Sub App_WindowSelectionChange(ByVal Sel As Selection)
MsgBox "Found it"
End Sub

Open in new window


And you put this in the ThisWorkbook module
Dim eventHandler As New Class1
Private Sub Document_Open()
  Set eventHandler.App = Word.Application
End Sub

Open in new window


If someone manages to answer the first actual question, I'll give you the points anyway because it would be useful to know (linking all the objects to the same code somehow). Otherwise, I'm accepting this post as the solution.
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 34170996
You would use a class (simple demo file attached) - code below:

' IN CLASS MODULE CALLED CLabelHandler

Public WithEvents lbl As MSForms.Label

Private Sub lbl_Click()
    MsgBox "You clicked " & lbl.Name
End Sub


' In ThisDocument module

Dim colLabels As Collection
Private Sub Document_Open()
    Dim objHandler As CLabelHandler
    Dim shp As InlineShape
    Set colLabels = New Collection
    For Each shp In ThisDocument.InlineShapes
        If TypeName(shp.OLEFormat.Object) = "Label" Then
            Set objHandler = New CLabelHandler
            Set objHandler.lbl = shp.OLEFormat.Object
            colLabels.Add objHandler
        End If
    Next shp
End Sub

Open in new window

LabelHandler.doc
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

929 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

15 Experts available now in Live!

Get 1:1 Help Now