Solved

VBA map multiple buttons to the same click event

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 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