macrobutton for toggling 2 macros

B

BAH

Hi
Is there a way to create a macrobutton that enables me, when double-clicking
it, to toggle between two macros already created?
 
R

Russ

Are you talking about a toolbar button for a macro?
Toolbar buttons activate with one click.

Some options in Word can be toggled by code like:
Selection.Font.Bold = Not Selection.Font.Bold

If they are normally set with a value True or False.

Otherwise you have to find a way to store a value that can be tested each
time the button is activated.

Storing Values When a Macro Ends

http://snipurl.com/swu3
 
G

Graham Mayor

You could run a third macro to toggle between them eg

Sub ChooseMacros()
Dim sMacro As String
Start:
sMacro = InputBox("Enter 'A' for Macro 1" & vbCr _
& "or 'B' for Macro2", "A")
If sMacro = "A" Or sMacro = "a" Then
Application.Run MacroName:="Macro1"
ElseIf sMacro = "B" Or sMacro = "b" Then
Application.Run MacroName:="Macro2"
Else: MsgBox "The choices are A or B!", vbCritical
GoTo Start:
End If
End Sub

Sub Macro1()
MsgBox "This is Macro1"
End Sub
Sub Macro2()
MsgBox "This is Macro2"
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
H

Helmut Weber

Hi,

just another idea would be to check
ControlKeyState flags (API) and thus run
one out of of several macros,
depending on the ControlKeyState.

Like:

if left-shift then run Macro1
if right-shift then run Macro2
if Ctrl-shift-left then run macro3

Nothing for an end user
and for sure something for well organized people
or people with good memory.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
Y

yves

Rather than run yet another macro, would it not be simpler to toggle
the OnAction property of the button itself? After all, this is exactly
what is needed here, and Office VBA provides the resources. When any
of the two macros ends, have it toggle the button's OnAction property
by ending it with code like that:

If CommandBars("MyBar").Controls(ButtonIndex).OnAction="macro1" Then
CommandBars("MyBar").Controls(ButtonIndex).OnAction="macro2"
Else
CommandBars("MyBar").Controls(ButtonIndex).OnAction="macro1"
End If

where "MyBar" and ButtonIndex identify your button.

My two cents,
Yves Champollion
www.champollion.net
 
R

Russ

Very cool, Yves.
I haven't dealt with toolbar buttons that much, mainly I drag a routine into
a new menu to use it.
Thank goodness for the internet and forums to disseminate information.
 
K

Klaus Linke

BAH said:
Hi
Is there a way to create a macrobutton that enables me,
when double-clicking it, to toggle between two macros already created?

Buttons also have a State property, that determines whether they look
"pressed" or "depressed".
You could have one macro that checks out the State, runs the appropriate
code, and toggles the button to the other State.

Sub TestState()
Dim myCBB As CommandBarButton
Set myCBB = CommandBars.ActionControl
Select Case myCBB.State
Case msoButtonDown
MsgBox "run code to lift the button"
myCBB.State = msoButtonUp
Case msoButtonUp
MsgBox "run code to press the button"
myCBB.State = msoButtonDown
Case msoButtonMixed
MsgBox "shouldn't happen"
myCBB.State = msoButtonDown
End Select
End Sub


Klaus
 
B

Bob W

Klaus,
How would you modify this macro to toggle the "backward paragraph" button
(ActiveWindow.ActivePane.View.ShowAll) on and off? I'm creating templates
for unsophisticated Word users; toggling this button lets them show/hide
hidden text instructions, but they don't know what the backward paragraph
button is, nor how to make it visible if it's not in their menu bar. Being
able to embed a macrobutton in the template that they could use to toggle it
on/off would be helpful.
 

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