Toggle symbols with VBA

R

robotman

I'm trying to set up a roster where I can click on a symbol in front
of a person's name and it will change from an "X" to a "O".

I'm an expert at Excel VBA, but not familiar with Word VBA.

Can someone give me some ideas on how I could set up a macro to allow
the user to check on a symbol (or character) in front of a person's
name in a list and it would toggle between 2 or 3 symbols?

The list will be dynamic, so I need to write some generic code that
can identify which symbol is clicked on and toggle it to the next
symbol.

Thanks.

John
 
M

m rafala

One way would be to put a CommandButton or some other control (see control
toolbox toolbar) into the document so that the caption changes with each
click.

You would need to put the following code in the document's ThisDocument
module:

Private Sub CommandButton1_Click()
If CommandButton1.Caption = "O" Then
CommandButton1.Caption = "X"
Else
CommandButton1.Caption = "O"
End If
End Sub
 
D

Dave D-C

There's no equivalent of
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
but you could hit a menu button, leaving the cursor at your symbol:
in Thisdocument:
Option Explicit

Private Sub Document_Open()
Dim CB As CommandBar, CBC As CommandBarControl
Set CB = Application.CommandBars.ActiveMenuBar
CB.Reset
Set CBC = CB.Controls.Add(msoControlButton, , , , True)
CBC.FaceId = 342 ' my favorite
CBC.OnAction = "Doit"
End Sub

Private Sub Document_Close()
Application.CommandBars.ActiveMenuBar.Reset
End Sub

Sub Doit()
Const TblArg = "XO", TblVal = "OX"
Dim ch$, i%
Selection.Collapse wdCollapseStart
ch = Chr$(Asc(Selection.Text & " "))
i = InStr(1, TblArg, ch)
If i > 0 Then
Selection.Delete
Selection.TypeText Mid$(TblVal, i, 1)
End If
End Sub
 
D

Dave D-C

Of course, to make it "dynamic", you would want globals
TblArg$ and TblVal$
and set them to what you want, when you want.
 
S

Shauna Kelly

Hi John

For a simple way to do this, that operates with a right-click, and requires
no VBA, see
How to add pop-up lists to any Word document, so you can click your way
through changes in seconds
http://word.mvps.org/FAQs/TblsFldsFms/AutoTextList.htm

For a fancier way (that operates on single-click or double-click depending
on your settings in Word) and that does require VBA, see
Enable a user to double-click text in a document to change its value
http://word.mvps.org/FAQs/MacrosVBA/AssignMacroToText.htm

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
R

robotman

Thanks for everyones' ideas.

Autotext seems to work well for what I'm trying to do. Do you know if
I can set up something like Autotext but use graphics instead of text?

Under "Background Item #1" in the first link it says you can use "text
and/or graphics", but I don't see how to do the graphics with
Autotext.

Thanks.

John
 
S

Shauna Kelly

John

You can store any kind of content in an AutoText: images, tables, fields,
plain text, whatever. It can be one word; it can be 100 pages. Just select
the content and do Insert > AutoText > AutoText. Choose where to save it,
give it a name and you're under way.

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 

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