How to get the name of the command button that was clicked

M

Marc Hankin

Does anyone know how to get the name of the command
button that was clicked?

thanks, marc
 
P

Perry

Look at

ActiveControl property of forms

Note: ActiveControl won't intellisense Name as a property however
you can obtain the Name nevertheless.

Krgrds,
Perry
 
M

Marc Hankin

Sorry for being stupid. I spent almost all day trying to
figure out how to use ActiveControl (or some other
approach) to get vba to return the name of the control
that was just clicked, but I can't figure it out.
The following lines (are triggered by the Click event
of a textbox):

txBkMrkSplSrch01.SetFocus
strControlNam = frmBkMarksNavigtn.ActiveControl.Name

Instead of returning the name of the textbox that was
clicked, those lines return the name of the frame in
which the textbox is situated
As you can see, I cheated, i.e., I used the control's
name itself (which is what I'm trying to avoid) in
setting the focus on the textbox.

I also unsuccessfully tried using the following code
(that I found using google) to get vba to return the name
of the clicked textbox, but the code below gives me an
error message ("Compile error, variable not defined" and
highlights the word "Screen" below):

Dim ctlCurrentControl As Control
Dim ctlCurrentControl As Control
Dim strControlName As String
Set ctlCurrentControl = Screen.ActiveControl
strControlName = ctlCurrentControl.Name

Any help would be much appreciated.

Marc
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Marc Hankin > écrivait :
In this message, < Marc Hankin > wrote:

|| Sorry for being stupid. I spent almost all day trying to
|| figure out how to use ActiveControl (or some other
|| approach) to get vba to return the name of the control
|| that was just clicked, but I can't figure it out.

As Perry mentioned, this should work:

'_______________________________________
Dim CtrlName As String

CtrlName = Me.ActiveControl.Name
'_______________________________________

|| The following lines (are triggered by the Click event
|| of a textbox):

I do not see a Click event for textboxes.

||
|| txBkMrkSplSrch01.SetFocus
|| strControlNam = frmBkMarksNavigtn.ActiveControl.Name
||
|| Instead of returning the name of the textbox that was
|| clicked, those lines return the name of the frame in
|| which the textbox is situated

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
M

Marc Hankin

D'abord, Merci pour avoir repondu. C'est tres gentil
de votre part de donner votre temps comme ca.
Now, alas, the rest in English.
1. I tried using "CtrlName = Me.ActiveControl.Name", but
it returns the name of the frame in which the text box is
situated on the form.
2. You said: "I do not see [the] Click event for
textboxes [to which you, Marc, referred]."
I had refrained from including all the code, for fear
of bothering everyone with too much code. But, without
further ado (except to thank you for your efforts and
time), here's the entire subroutine for the text box:

Private Sub txBkMrkSplSrch01_DblClick(ByVal Cancel As
MSForms.ReturnBoolean)
'Set ActiveControl = txBkMrkSplSrch01
'txBkMrkSplSrch01.Enabled
txBkMrkSplSrch01.SetFocus
Dim strBookmark As String, strCtrlNam As String,
strCtrlNo As String, strBookmarkNam As String
Dim strTxBoxNam As String, strCtrlClkdNam As String,
CtrlName As String

Dim ctlCurrentControl As Control
Dim strControlName As String
' The next 2 lines of code are commented out,
' because the first of those 2 lines causes the
' following error message:
' "Compile error: Variable not defined", and
' the word "Screen" gets highlighted:
'
'Set ctlCurrentControl = Screen.ActiveControl
'strControlName = ctlCurrentControl.Name

'The next line is what you suggested trying.
' The msgbox returns the name of the frame in
' which the textbox is situated.
CtrlName = Me.ActiveControl.Name
MsgBox CtrlName '

'A Google search lead me to the "Application.Caller"
' approach, but I couldn't figure out how to use it.
'strCtrlNam = Application.Caller
'MsgBox strCtrlNam
strCtrlNo = Right(strCtrlNam, 1)
strBookmarkNam = "BkMrkSplSrch0" & strCtrlNo

'If the bookmark exists, delete it.
If ActiveDocument.Bookmarks.Exists("strBookmarkNam")
= True Then
ActiveDocument.Bookmarks("strBookmarkNam").Delete
End If

'Copy the selected text to this bookmark's name,
e.g., BkMrkSplSrch0?
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range,
Name:="strBookmarkNam"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With

'copy the bookmarked text to a stringVar named
strBookmark
strBookmark = ActiveDocument.Bookmarks
("strBookmarkNam").Range.Text

'create a string = the name of the textbox
strTxBoxNam = "txBkMrkSplSrch0" & strCtrlNo

MsgBox strTxBoxNam
'strTxBoxNam.Text = strBookmark
End Sub



Thanks again, Marc
 
P

Perry

Ah, the buttons are in a frame. The frame will indeed come out as
ActiveControl ...

There's no Application.Caller way of retrieving system callback information
in Word.
This is an Excel feature.
Similar to VB dotnet's: 'sender As System.Object' argument to events.
Again, in Word VBA these system callbacks are not available.

You'll have explicitely pass the caller/sender as an argument to
a single routine like in below Word VBA code sequence:

code sequence/
Private Sub HandleButton(ByVal MyButton As MSForms.CommandButton)
MsgBox MyButton.Name
End Sub

Private Sub CommandButton3_Click()
HandleButton Me.CommandButton3
End Sub

Private Sub CommandButton2_Click()
HandleButton Me.CommandButton2
End Sub
/code sequence

Krgrds,
Perry

"Marc Hankin" <[email protected]> schreef in bericht
D'abord, Merci pour avoir repondu. C'est tres gentil
de votre part de donner votre temps comme ca.
Now, alas, the rest in English.
1. I tried using "CtrlName = Me.ActiveControl.Name", but
it returns the name of the frame in which the text box is
situated on the form.
2. You said: "I do not see [the] Click event for
textboxes [to which you, Marc, referred]."
I had refrained from including all the code, for fear
of bothering everyone with too much code. But, without
further ado (except to thank you for your efforts and
time), here's the entire subroutine for the text box:

Private Sub txBkMrkSplSrch01_DblClick(ByVal Cancel As
MSForms.ReturnBoolean)
'Set ActiveControl = txBkMrkSplSrch01
'txBkMrkSplSrch01.Enabled
txBkMrkSplSrch01.SetFocus
Dim strBookmark As String, strCtrlNam As String,
strCtrlNo As String, strBookmarkNam As String
Dim strTxBoxNam As String, strCtrlClkdNam As String,
CtrlName As String

Dim ctlCurrentControl As Control
Dim strControlName As String
' The next 2 lines of code are commented out,
' because the first of those 2 lines causes the
' following error message:
' "Compile error: Variable not defined", and
' the word "Screen" gets highlighted:
'
'Set ctlCurrentControl = Screen.ActiveControl
'strControlName = ctlCurrentControl.Name

'The next line is what you suggested trying.
' The msgbox returns the name of the frame in
' which the textbox is situated.
CtrlName = Me.ActiveControl.Name
MsgBox CtrlName '

'A Google search lead me to the "Application.Caller"
' approach, but I couldn't figure out how to use it.
'strCtrlNam = Application.Caller
'MsgBox strCtrlNam
strCtrlNo = Right(strCtrlNam, 1)
strBookmarkNam = "BkMrkSplSrch0" & strCtrlNo

'If the bookmark exists, delete it.
If ActiveDocument.Bookmarks.Exists("strBookmarkNam")
= True Then
ActiveDocument.Bookmarks("strBookmarkNam").Delete
End If

'Copy the selected text to this bookmark's name,
e.g., BkMrkSplSrch0?
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range,
Name:="strBookmarkNam"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With

'copy the bookmarked text to a stringVar named
strBookmark
strBookmark = ActiveDocument.Bookmarks
("strBookmarkNam").Range.Text

'create a string = the name of the textbox
strTxBoxNam = "txBkMrkSplSrch0" & strCtrlNo

MsgBox strTxBoxNam
'strTxBoxNam.Text = strBookmark
End Sub



Thanks again, Marc
 
M

Marc Hankin

Thanks.
I keep hearing about features that are available in
VB.net, which regrettably are not available in vba.
So, I'm thinking wouldn't it be better for me to (learn
how to) make the forms and the related programming in
VB.net, and just have vba trigger the VB.net routines?
Whaddaya think?
Thanks again, Marc

-----Original Message-----
Ah, the buttons are in a frame. The frame will indeed come out as
ActiveControl ...

There's no Application.Caller way of retrieving system callback information
in Word.
This is an Excel feature.
Similar to VB dotnet's: 'sender As System.Object' argument to events.
Again, in Word VBA these system callbacks are not available.

You'll have explicitely pass the caller/sender as an argument to
a single routine like in below Word VBA code sequence:

code sequence/
Private Sub HandleButton(ByVal MyButton As MSForms.CommandButton)
MsgBox MyButton.Name
End Sub

Private Sub CommandButton3_Click()
HandleButton Me.CommandButton3
End Sub

Private Sub CommandButton2_Click()
HandleButton Me.CommandButton2
End Sub
/code sequence

Krgrds,
Perry

"Marc Hankin" <[email protected]> schreef in bericht
D'abord, Merci pour avoir repondu. C'est tres gentil
de votre part de donner votre temps comme ca.
Now, alas, the rest in English.
1. I tried using "CtrlName = Me.ActiveControl.Name", but
it returns the name of the frame in which the text box is
situated on the form.
2. You said: "I do not see [the] Click event for
textboxes [to which you, Marc, referred]."
I had refrained from including all the code, for fear
of bothering everyone with too much code. But, without
further ado (except to thank you for your efforts and
time), here's the entire subroutine for the text box:

Private Sub txBkMrkSplSrch01_DblClick(ByVal Cancel As
MSForms.ReturnBoolean)
'Set ActiveControl = txBkMrkSplSrch01
'txBkMrkSplSrch01.Enabled
txBkMrkSplSrch01.SetFocus
Dim strBookmark As String, strCtrlNam As String,
strCtrlNo As String, strBookmarkNam As String
Dim strTxBoxNam As String, strCtrlClkdNam As String,
CtrlName As String

Dim ctlCurrentControl As Control
Dim strControlName As String
' The next 2 lines of code are commented out,
' because the first of those 2 lines causes the
' following error message:
' "Compile error: Variable not defined", and
' the word "Screen" gets highlighted:
'
'Set ctlCurrentControl = Screen.ActiveControl
'strControlName = ctlCurrentControl.Name

'The next line is what you suggested trying.
' The msgbox returns the name of the frame in
' which the textbox is situated.
CtrlName = Me.ActiveControl.Name
MsgBox CtrlName '

'A Google search lead me to the "Application.Caller"
' approach, but I couldn't figure out how to use it.
'strCtrlNam = Application.Caller
'MsgBox strCtrlNam
strCtrlNo = Right(strCtrlNam, 1)
strBookmarkNam = "BkMrkSplSrch0" & strCtrlNo

'If the bookmark exists, delete it.
If ActiveDocument.Bookmarks.Exists("strBookmarkNam")
= True Then
ActiveDocument.Bookmarks("strBookmarkNam").Delete
End If

'Copy the selected text to this bookmark's name,
e.g., BkMrkSplSrch0?
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range,
Name:="strBookmarkNam"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With

'copy the bookmarked text to a stringVar named
strBookmark
strBookmark = ActiveDocument.Bookmarks
("strBookmarkNam").Range.Text

'create a string = the name of the textbox
strTxBoxNam = "txBkMrkSplSrch0" & strCtrlNo

MsgBox strTxBoxNam
'strTxBoxNam.Text = strBookmark
End Sub



Thanks again, Marc
-----Original Message-----
Bonjour,

Dans son message, < Marc Hankin > écrivait :
In this message, < Marc Hankin > wrote:

|| Sorry for being stupid. I spent almost all day trying to
|| figure out how to use ActiveControl (or some other
|| approach) to get vba to return the name of the control
|| that was just clicked, but I can't figure it out.

As Perry mentioned, this should work:

'_______________________________________
Dim CtrlName As String

CtrlName = Me.ActiveControl.Name
'_______________________________________

|| The following lines (are triggered by the Click event
|| of a textbox):

I do not see a Click event for textboxes.

||
|| txBkMrkSplSrch01.SetFocus
|| strControlNam = frmBkMarksNavigtn.ActiveControl.Name
||
|| Instead of returning the name of the textbox that was
|| clicked, those lines return the name of the frame in
|| which the textbox is situated

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org



.


.
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Marc Hankin > écrivait :
In this message, < Marc Hankin > wrote:

| D'abord, Merci pour avoir repondu. C'est tres gentil
| de votre part de donner votre temps comme ca.

Très bien!

| Now, alas, the rest in English.
| 1. I tried using "CtrlName = Me.ActiveControl.Name", but
| it returns the name of the frame in which the text box is
| situated on the form.

For a control in a Frame, you have to refer to the frame container, as in:

'_______________________________________
Dim CtrlName As String

CtrlName = Me.Frame1.ActiveControl.Name
'_______________________________________

| 2. You said: "I do not see [the] Click event for
| textboxes [to which you, Marc, referred]."

Ah! You posted a DoubleClick event... I was not sure If you had meant
DoubleClick for a textbox or Click for a CommandButon...

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
M

Marc Hankin

Whoopee! That works! Merci beaucoup. Mais j'ai
decovert un nouveau probleme: (The rest in English)
Using the code you gave me, I was able to get the name
of the control (in this case, a textbox) as follows:
"CtrlName =
Me.framBkMarkSpclDstinatns.ActiveControl.Name"
So, (being the adventurous type) I assigned the name
of the frame ("framBkMarkSpclDstinatns") to a string
variable ("strFramNam"). Then, I tried to use your code
using the string variable instead of hard coding the
frame's name.
Helas, this triggered the following error message:
"Compile error: method or data member not found."

My objective is to be able to use the same code for many
controls, i.e., to be able to avoid hard coding the
controls' respective names into each control's code.
Here's the code that I'm using:

Private Sub txBkMrkSplSrch01_DblClick(ByVal Cancel As
MSForms.ReturnBoolean)
'
Dim strBookmark As String, strCtrlNam As String,
strCtrlNo As String, strBookmarkNam As String
Dim strTxBoxNam As String, strCtrlClkdNam As String,
CtrlName As String
Dim strFramNam As String, strMeFramNam As String

Dim ctlCurrentControl As Control
Dim strControlName As String

'Set ctlCurrentControl = Screen.ActiveControl
'strControlName = ctlCurrentControl.Name

strFramNam = Me.ActiveControl.Name
CtrlName =
Me.framBkMarkSpclDstinatns.ActiveControl.Name

MsgBox "strFramNam = " & strFramNam
MsgBox "CtrlName = " & CtrlName

'On the next line, I am using the same code as 3
lines above this one,
' except that I've replaced the frame's name with a
string variable
' containing the name. Alas, the next line produces
the error
' message: Compile error: Method or data member
not found.
' Why the error message? Is there a way to use the
string variable
' on the next line, without producing the error
message?
CtrlName = Me.strFramNam.ActiveControl.Name
End sub

Thanks again,
Marc, Sieur de la Hankiniere


-----Original Message-----
Bonjour,

Dans son message, < Marc Hankin > écrivait :
In this message, < Marc Hankin > wrote:

| D'abord, Merci pour avoir repondu. C'est tres gentil
| de votre part de donner votre temps comme ca.

Très bien!

| Now, alas, the rest in English.
| 1. I tried using "CtrlName = Me.ActiveControl.Name", but
| it returns the name of the frame in which the text box is
| situated on the form.

For a control in a Frame, you have to refer to the frame container, as in:

'_______________________________________
Dim CtrlName As String

CtrlName = Me.Frame1.ActiveControl.Name
'_______________________________________

| 2. You said: "I do not see [the] Click event for
| textboxes [to which you, Marc, referred]."

Ah! You posted a DoubleClick event... I was not sure If you had meant
DoubleClick for a textbox or Click for a CommandButon...

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org



.
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Marc Hankin > écrivait :
In this message, < Marc Hankin > wrote:

| Whoopee! That works! Merci beaucoup. Mais j'ai
| decovert un nouveau probleme: (The rest in English)
| Using the code you gave me, I was able to get the name
| of the control (in this case, a textbox) as follows:
| "CtrlName =
| Me.framBkMarkSpclDstinatns.ActiveControl.Name"
| So, (being the adventurous type) I assigned the name
| of the frame ("framBkMarkSpclDstinatns") to a string
| variable ("strFramNam"). Then, I tried to use your code
| using the string variable instead of hard coding the
| frame's name.
| Helas, this triggered the following error message:
| "Compile error: method or data member not found."
|
|(...)
|
| Marc, Sieur de la Hankiniere

Monseigneur,
Peut-être trouverez-vous un certain plaisir à utiliser ce code:

'_______________________________________
Dim FrameName As String
Dim CtrlName As String

FrameName = Me.ActiveControl.Name

CtrlName = Me.Controls(FrameName).ActiveControl.Name
'_______________________________________

C'est tout un honneur de correspondre avec quelqu'un de si noble!


p.s. You cannot user a String Variable in an expression in lieu of an
object/property/method as in:

CtrlName = Me.FrameName.ActiveControl.Name

The compiler is interpreting FrameName as an object/property/method , but it
is a variable.
Hence the
"Compile error: method or data member not found."
message.
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top