PDA

View Full Version : Change caption of command button using code



msblane
06-30-2012, 01:23 AM
Hi, I am trying to add code to the click event of an activex command button that can read the name or index of the command button and then change its caption. Can anyone help? Thanks!

Jay Freedman
06-30-2012, 02:19 PM
There's not much point in reading the name or index of an ActiveX button in code, because each button must have a separate click handler in the ThisDocument module of the template or document. So the event handler always knows exactly which button was clicked to raise the event.

To change the button's caption, you just assign a string to the button's .Caption property. For example:

Private Sub btnOne_Click()
btnOne.Caption = "ouch!"
End Sub

Paul_Hossler
07-01-2012, 08:09 AM
There are probably cleverer ways to do this, but the most straight-forward way is



Option Explicit
Private Sub CommandButton1_Click()

With CommandButton1

If .Caption = "On" Then
.Caption = "Dim"
ElseIf .Caption = "Dim" Then
.Caption = "Off"
ElseIf .Caption = "Off" Then
.Caption = "On"
Else
.Caption = "On"
End If

End With

End Sub



Paul

msblane
07-02-2012, 12:55 AM
Thanks Jay and Paul. I see exactly what you are saying which will make it impossible to do what I originally wanted. I am thinking of putting the code to change the caption in a separate module - but I would still need to know the button's name so that I could manipulate it in the separate code. Any ideas?

fumei
07-02-2012, 01:59 AM
"I see exactly what you are saying which will make it impossible to do what I originally wanted"

No, the code offered does not make it impossible, it does EXACTLY what you originally wanted. What you said you wanted was:

"I am trying to add code to the click event of an activex command button"

The code does precisely that. It is code added to the click event that changes the Caption.

OK, so you posted incorrectly - you do not want code added to the click event. You want code that has nothing to do with the click event. Yes?

"I am thinking of putting the code to change the caption in a separate module" Ok. Why? What exactly do you want anyway? Or perhaps more significantly, WHEN do you want to change the caption? If it is NOT the click event then you do not need the code to find out the control name. Just look it up, and then use it. Then determine WHEN you want the action to happen. And how it is going to happen. What action will cause the code to fire (the Caption to change)? A keyboard shortcut? What?
ActiveDocument.InlineShapes(3).OLEFormat.Object.Caption = "yadda"
The third InlineShape (ActiveX controls are InlineShapes) Caption is changed.

Paul_Hossler
07-02-2012, 09:30 AM
The 'seperate module' part is a little ambigious, but the button events could call a sub in a seperate module or modules

Button events


Option Explicit
Private Sub CommandButton1_Click()
Call ButtonCaptions(CommandButton1)
End Sub
Private Sub CommandButton2_Click()
Call ButtonCaptions(CommandButton2)
End Sub



Seperate standalone regular module


Option Explicit
Sub ButtonCaptions(oButton As Object)

With oButton

If .Name = "CommandButton1" Then
If .Caption = "1-On" Then
.Caption = "1-Dim"
ElseIf .Caption = "1-Dim" Then
.Caption = "1-Off"
ElseIf .Caption = "1-Off" Then
.Caption = "1-On"
Else
.Caption = "1-On"
End If

ElseIf .Name = "CommandButton2" Then
If .Caption = "2-On" Then
.Caption = "2-Dim"
ElseIf .Caption = "2-Dim" Then
.Caption = "2-Off"
ElseIf .Caption = "2-Off" Then
.Caption = "2-On"
Else
.Caption = "2-On"
End If
End If

End With


End Sub



Paul

fumei
07-02-2012, 04:19 PM
That is not different - in any way - from having the action fire from the click event. Well, that is not exactly true...it adds an completely extraneous step. Bottom line though, it is an action caused by the click event. So, ummm, what is the point??

In fact, we need to know the point of this.

msblane
07-03-2012, 01:04 AM
My apologies for the confusion - and thanks for the help. Let me try and be clearer. What I am trying to do is create a command button in a template that users can add to their documents. They may add multiple instances of the command button which I am hoping to put into AutoText. When the user clicks on any one of the buttons in their document, the caption needs to change. Because the users may add multiple versions of the button, I will not know which button is being clicked unless I can retrieve the button's name or ID so that I can manipulate it in a separate procedure - or in the click event. I hope this makes what I am trying to do clearer.

fumei
07-03-2012, 09:10 PM
If I may say so, this is a VERY odd thing to want to do. Having users ADD multiple instances of a command button into documents??? Whoa.

Bottom line: every commandbutton has its own click event, so if they did put multiple commandbuttons in, each one still has its OWN event.

You still have not explained, in any way, WHY "When the user clicks on any one of the buttons in their document, the caption needs to change". Why does it "need" to change? What is its caption going to be to start with? If they click it again, does it change back? Change into something different? Not change?

Besides, not matter what the Caption is, clicking the button only does whatever is in the click event.

"Because the users may add multiple versions of the button, I will not know which button is being clicked unless I can retrieve the button's name or ID so that I can manipulate it in a separate procedure - or in the click event."

First off, you have been given code (multiple times) how to manipulate things in the click event itself.

You know - in the click event - which button is being clicked. Paul Hossler has shown you how you can pass the commandbutton (as an object) to another procedure.

So, in fact, you have already everything you are asking for.



I still do not think (generally) users should put commandbuttons into documents.

msblane
07-04-2012, 03:01 AM
Thanks everyone for trying to help. I appreciate your input. I have given up trying to explain what I need as I don't know what else to add. (I am not sure why you need to know why I want to do something!) Just FYI, for a relatively new user trying to accomplish something, it is very intimidating to be told that what you want to do is "VERY odd" and that I already have EXACTLY what I asked for when it is not at all what I needed. A little patience with confused users would be appreciated.

Paul_Hossler
07-04-2012, 01:19 PM
It's not uncommon for someone to find that what they asked for is not what they needed

Your #10



and that I already have EXACTLY what I asked for when it is not at all what I needed




Your #1



Hi, I am trying to add code to the click event of an activex command button that can read the name or index of the command button and then change its caption. Can anyone help? Thanks!


Posts #2 and #3 answered that specific question


Your Post #4 ...



I am thinking of putting the code to change the caption in a separate module - but I would still need to know the button's name so that I could manipulate it in the separate code. Any ideas?


Post #6 answered that specific question


Your #8 started to get closer to what you were trying to accomplish, but since there was no 'why', the technique that you had already decided that you were going to use was 'uncommon' and probably not as compatible with MS Word as an alternative approach that someone might suggest


What I am trying to do is create a command button in a template that users can add to their documents. They may add multiple instances of the command button which I am hoping to put into AutoText. When the user clicks on any one of the buttons in their document, the caption needs to change. Because the users may add multiple versions of the button, I will not know which button is being clicked unless I can retrieve the button's name or ID so that I can manipulate it in a separate procedure - or in the click event.

So you have a .dotm with an Command Button with some code and maybe a module. The user creates a new .dotx from the template, and they get the Autotext and possible the first command button if it's already created.

They run the Autotext and it puts a second command button in the dotx for the user to click.

If they click CB1, then it's caption changes, and if they click CB2 then its caption changes.

Just changing a couple of CB captions doesn't sound like it's accomplishng much, so people will naturally assume that there's more to it that just that, and that your goal is not just to change CB captions, but that there's a reason behind why you want to do that.

Paul

fumei
07-04-2012, 03:50 PM
Look, sorry if I sounded difficult. It actually IS relevant to know why you want to do something.

1. There may very well be a better way to accomplish what you want.
2. There very well may be some real issue with accomplishing what you want.

I ask why, because as I wrote, clicking the button ONLY does whatever is in the click event. So if clicking it changes Caption "yadda" to Caption "blah", clicking it again makes NOTHING happen. The change itself does nothing significant. It makes me wonder why because what is the point? It makes me wonder if there something else that is supposed to be happening, and if there is...we need to know.

You asked: "When the user clicks on any one of the buttons in their document, the caption needs to change"

OK, let's start with ONE button. When the user clicks it, its Caption changes - and yes, you already have this code.Private Sub btnOne_Click()
btnOne.Caption = "ouch!"
End Sub The Caption is changed to "ouch!".

OK? Now, let's say you have this as an AutoText. OK?

The user uses the AutoText to insert X number of "copies" into the document. "When the user clicks on any one of the buttons in their document, the caption needs to change."

"Any one"

Here is what happens. If they click the original button...the Caption changes. Unless it has been clicked already, in which case - again - NOTHING happens. BUT, if they ckick ANY of the other inserted buttons...NOTHING happens.

Why? Because of what I have already posted. Every commandbutton has its own click event. The "copied" (via AutoText) button are independent controls and have NO click event. Each one MUST have its own separate click event written. Using AutoText to insert other commandbutton will yes insert a commandbutton. But the commandbutton, when clicked will do NOTHING. It has no code attached. The AutoText will NOT copy any code attached to the original.

Does this help you understand the problem?

Your other buttons will have no code attached unless you write that code.

Really, I am trying to help, and yes it IS significant to know what your purpose is. It is quite possible we can suggest something that WILL work for you - for the purpose you need, rather than the situation you think will work.

As stated, what you think you want will not work. Unless of course you are going to go into each users VBA editor - AFTER they insert other buttons - and manually write in code. Or teach them how to write their own code. Which is a scary thought. And THAT is why having users insert their own commandbutton is a very odd thing to do.

Paul:
So you have a .dotm with an Command Button with some code and maybe a module. The user creates a new .dotx from the template, and they get the Autotext and possible the first command button if it's already created.

They run the Autotext and it puts a second command button in the dotx for the user to click.

If they click CB1, then it's caption changes, and if they click CB2 then its caption changes.
Oh no it does not. If they click CB2, unless code has been written for it, nothing happens. Every commandbutton has its own event and it MUST be written into the module. Copying the control itself via an AutoText does NOT write a new event procedure.

Paul_Hossler
07-04-2012, 05:58 PM
Oh no it does not. If they click CB2, unless code has been written for it, nothing happens. Every commandbutton has its own event and it MUST be written into the module. Copying the control itself via an AutoText does NOT write a new event procedure.


You're correct of course. I didn't think that through very hard, mostly because it's nothing I'd ever do.

I don't trust my users well enough to leave my styles alone, let alone start adding Command Buttons

Paul

msblane
07-04-2012, 10:50 PM
Actually, what I originally asked for was how to add code to the click event of an activex command button that can read the name or index of the command button and then change its caption. All the replies mentioned how to change the caption which is helpful - but none mentioned how to get the name or the index number.

What I am trying to do is give users on my template the ability to add a command button that simulates a check box or radio button (or series thereof) using a single click. I can't use Greg Maxey's excellent add-in because the company I am developing for will not allow me to get the add-in installed on every user's computer. I also want to use a single-click rather than a double-click to use the buttons. I thought that if I formatted the caption of the button using the Wingdings font and then changed the caption to a letter or number that displays as a check box in Wingdings, I could simulate. The users of my template want to be able to add multiple check-boxes and/or radio buttons to a protected form and then distribute the form to more than 100 users and have it work. Greg Maxey has another workaround for check-boxes but it only works with an unprotected form.

I am now thinking of creating the button in code so that I can control the name.

Any ideas will be welcome.

Thanks!

fumei
07-05-2012, 04:38 PM
Actually, what I originally asked for was how to add code to the click event of an activex command button that can read the name or index of the command button and then change its caption. All the replies mentioned how to change the caption which is helpful - but none mentioned how to get the name or the index number.This is simply not true.
Private Sub CommandButton1_Click()

With CommandButton1
There is its name. It is true that this only works within the control's click event .

Commandbuttons in a document (unlike ones on a useerform) do NOT have an actual .Name property. Their index number is whatever is their position in the InlineShapes collection.

Jay Freedman
07-07-2012, 02:41 PM
The real problem here is that you've picked the wrong kind of button to start with. As fumei has explained to you, every ActiveX command button must have a separate event handler macro (and the name of that macro includes the name of the button object, which is why it makes no sense for the code to try to "read" the name of the button).

The better choice for this application is a "MacroButton field", explained here (http://www.word.mvps.org/FAQs/MacrosVBA/AssignMacroToButton.htm) and here (http://www.word.mvps.org/FAQs/TblsFldsFms/UsingMacroButton.htm). This type of field can be stored as an AutoText entry. Its "caption" is part of the field code and can be changed by a macro. All of the MacroButton fields in the document can (and should) activate the same single macro -- as opposed to the one-macro-per-button requirement of ActiveX buttons -- and the macro can determine which field was clicked by examining the Selection (most easily if all the MacroButton fields are contained in table cells, so the macro can find the current row and column indexes). Finally, although by default a MacroButton needs a double-click to activate its macro, you can change that by including this in your template:

Sub AutoNew()
Options.ButtonFieldClicks = 1
End Sub

Sub AutoOpen()
Options.ButtonFieldClicks = 1
End Sub

fumei
07-07-2012, 09:43 PM
Which is why it is significant for us to know WHAT it is you want to achieve, and no be rigidly focused on HOW you think you want to achieve it. I still do not understand what the purpose is.

As Jay points out a macrobutton can have multiples of itself all pointing to ONE piece of code. So. That can be achieved, but to what end?

ariesian
10-18-2012, 07:15 AM
msblane i also have the same problem i think
my problem is how can i change the name using textbox from the form2 and change the command button name in form1

i know the code if im only using it only in one form but i want to know how i can call form1 to change the command button caption name change

fumei
10-18-2012, 11:39 AM
If I understand correctly, your situation is NOT the same.

msblane asked about ActiveX controls in the document. You seem to be asking about controls on userforms. These are quite different.

ATor2882
12-04-2013, 10:07 AM
Hello, sorry if I reopen this discussion.
I have a problem with Macro Buttons: I have a document with a lot of buttons, and I don't want to use ActiveX command buttons, because I would have need to manage that buttons with a single macro. I created in my document a macro button in this way:
10923
Clicking on it, through the macro CheckIt, I would like to change its caption from the empty check box to a box with a mark sign inside. I tried to change the text of the selection in this way:

Selection.range.Font.Name = "Wingdings"
Selection.range.Text = ChrW(&HFE)
But after that the macro button disappears and in its place I have only the mark sign character .
How could I change the caption of a macrobutton without replacing the button itself? Thanks in advance.

Jay Freedman
12-04-2013, 10:28 AM
When you select the entire field by clicking on it, and assign text (in this case, a single character) to the Selection, you're replacing the entire field with that character. Instead, you need to replace the single character within the field's code. The article http://gregmaxey.mvps.org/word_tip_pages/add_interactive_toggle_objects_to_document.html shows how to do that.