Solved

VBA map multiple buttons to the same click event

Posted on 2010-11-18
2
864 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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

743 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

12 Experts available now in Live!

Get 1:1 Help Now