Solved

VBA map multiple buttons to the same click event

Posted on 2010-11-18
2
976 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
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‚Ķ

724 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