1 Macro: Multiple Command Buttons

E

expect_ed

Is there a way to do this? I have a 30 page document that has the
instructions as hidden text. Every so often I have a set of Hide Text Reveal
Text command buttons so that the instructions can show or hide. I have to
have a separate macro for each button because I cannot figure out how to
reassign the button to the macro for an existing button. Any help
appreciated.
ed
 
B

Beth Melton

Create a general procedure and then for the Click event of each button, use
Call <MacroName>. For example:

Sub ShowHiddenText()
'code
End Sub

Sub cmdFirstButton_Click()
Call ShowHiddenText
End Sub

You could go one step further and use a single macro for all of the buttons.
For this you need to add an argument for your general procedure, such as:
"ShowHide as Integer" and then when you call the procedure, provide the
appropriate argument. Here are some code examples:

Sub ShowRevealText(ShowHide As Integer)
Select Case ShowHide
Case 0
'Hide text
Case 1
'Show text
End Select
End Sub

'Then for your buttons use:
Private Sub cmdShow_Click()
Call ShowRevealText(1)
End Sub

Private Sub cmdHide_Click()
Call ShowRevealText(0)
End Sub

Please post all follow-up questions to the newsgroup. Requests for
assistance by email can not be acknowledged.

~~~~~~~~~~~~~~~
Beth Melton
Microsoft Office MVP

Co-author of Word 2007 Inside Out:
http://www.microsoft.com/MSPress/books/9801.aspx#AboutTheBook

Word FAQ: http://mvps.org/word
TechTrax eZine: http://mousetrax.com/techtrax/
MVP FAQ site: http://mvps.org/
 
E

expect_ed

Beth, Thanks for your help.
Unfortunately I lack the knowledge to make full use of it.
I think my problem is I do not understand how to link a procedure to a
command button. If I copy an existing button and paste it further down the
document it apparently links to a different procedure.

I only know this because if I click the button nothing happens and if I go
into design mode and double click a button a new empty procedure appears in
the VBA editor.

Since the code to hide or reveal hidden text is only one line anyway the
first part of your response, even though I follow it, does not really save me
much, although I can see how it would if I was doing a toggle with error
checking etc. or anything with multiple lines of code.

I sort of follow your second example but still cannot see a way to have the
second iteration of the HIDE button link to the same code. Perhaps there is
no way to do that? Does each individual command button require that one use
a separate procedure. The model I am used to (Filemaker Pro) each button
object can be independently directed to a procedure (script) so that a single
procedure can drive dozens of buttons.

Clearly there is something I am missing. Any help greatly appreciated.
thanks
ed
 
B

Beth Melton

The general procedure, the last example, uses an argument that will allow
you to determine whether the procedure will run your code for show the text
or run the code for hiding the text. Right now, 0=Hide and 1=Show. (But they
can mean anything you want).

Then in the Select Case statement, add your code accordingly.

Case 0
'Add your hide text code
Case 1
'Add your show text code.

Each command button does need some type of procedure, even if it's to call
another procedure, and you'd use one of the following:

In the empty procedure behind each command for showing the text you need:
Call ShowRevealText(1)

In the empty procedure behind each command button for hiding the text you
need:
Call ShowRevealText(0)

If you're using just one line of code then perhaps you don't need something
so elaborate. Are you just changing ShowHiddenText? If that's the case then
you might be able to use a single button that will toggle the display
accordingly, such as:

ActiveWindow.View.ShowHiddenText = Not ActiveWindow.View.ShowHiddenText

Or you could call a general procedure from each button even if it is a
single line. That way if you do want to add something you only need to do it
once.

Please post all follow-up questions to the newsgroup. Requests for
assistance by email can not be acknowledged.

~~~~~~~~~~~~~~~
Beth Melton
Microsoft Office MVP

Co-author of Word 2007 Inside Out:
http://www.microsoft.com/MSPress/books/9801.aspx#AboutTheBook

Word FAQ: http://mvps.org/word
TechTrax eZine: http://mousetrax.com/techtrax/
MVP FAQ site: http://mvps.org/
 
E

expect_ed

Thanks Beth,
My main goal was to avoid 18 procedures if I wanted 18 buttons scattered
throughout the document. It's clear now that there is no way around that.
In the meantime your toggle code (setting = not setting) is very helpful.
And the idea to use a driving procedure so editing only requires one edit is
a good point also.
thanks again for your help.
ed
 

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