Solved

VBA map multiple buttons to the same click event

Posted on 2010-11-18
2
908 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 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