Solved

Word VBA Self-Reference

Posted on 2009-03-30
4
1,460 Views
Last Modified: 2013-12-20
Hi,

I have a document with a number of Control Checkboxes.
Im trying to get a checkbox to refer to itself when clicked.
Say I want to obtain the name or the value of the checkbox whose status I just changed...

For example something like

Private Sub CheckBoxB_Change()
    MsgBox <THIS CHECKBOX>.Name
    MsgBox <THIS CHECKBOX>.Value
End Sub


I'm using Ms Word 2003...
0
Comment
Question by:Strobo
  • 2
  • 2
4 Comments
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 24026103
You can access the current control using Me.ActiveControl.....

    Private Sub CheckBoxB_Change()
        MsgBox Me.ActiveControl.Name
        MsgBox Me.ActiveControl.Value
    End Sub

Wayne
0
 
LVL 2

Author Comment

by:Strobo
ID: 24027791
Hi Wayne...

I tried that already. I get an error message "Method or data member not found".

I think that would work in Excel, but not in Word. Yeah, I keep running into simple ways to do things in excel and can only wonder why they dont work in Word. :-(

Any other ideas?
0
 
LVL 47

Assisted Solution

by:Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs) earned 160 total points
ID: 24027877
Sorry, I thought you were using a UserForm.

On the document there doesn't appear to be a way to get the active control. But why do you need it? If "CheckBoxB_Change" is called, obviously the controls name is "CheckBoxB". If you are after a method by which you can copy the same routine to numerous controls, consider using a separate routine....
Private Sub CheckBox1_Change()
     CheckBox_Change CheckBox1
End Sub
 
Private Sub CheckBox2_Change()
     CheckBox_Change CheckBox2
End Sub
 
Sub CheckBox_Change(ByRef cb As Object)
     MsgBox cb.Value
End Sub

Open in new window

0
 
LVL 2

Accepted Solution

by:
Strobo earned 0 total points
ID: 24034308
Yes, thats how Im currently doing it... but, to be honest, I have about a million checkboxes and was hoping there would be an easier way then writing this out for each one.

Maybe I should just try switching to UserForm... although that gives me other issues.

Anyway... thanks..
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

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…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

803 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