Bryce Bassett
asked on
Find and Replace Symbols Programmatically in Word 2007
Hi:
I'm working with VBA automation in Word 2007. I have two symbols I'm using extensively in a document template. On the Word Insert menu they show up as Wingdings character code 114 (a 3D-looking checkbox), and Wingdings character code 166 (a 3D-looking radio button). When I use the macro recorder and insert these 2 characters, I get the code below. So I know how to insert them programmatically. When I record a search and replace with these symbols pasted in, they show up as ChrW (61544) and ChrW(61606). So I know they have at least 3 numerical equivalents, but I don't know which numbers to use and how to do the following:
1. Recognize a symbol.
a. I use code to go to a given table cell and select the first character of the contents.
b. I want to know whether the selection is one of these two symbols. If it were plain text, I could say "If selection = "X". But how can I find out "if selection = Wingdings character code 114 ( = character number -3930 = ChrW (61554) ) ?
2. How to search for one and replace it with the other.
Similar question. What VBA code would I use to search for one of these symbols and replace it with the other?
Thanks for you help!
I'm working with VBA automation in Word 2007. I have two symbols I'm using extensively in a document template. On the Word Insert menu they show up as Wingdings character code 114 (a 3D-looking checkbox), and Wingdings character code 166 (a 3D-looking radio button). When I use the macro recorder and insert these 2 characters, I get the code below. So I know how to insert them programmatically. When I record a search and replace with these symbols pasted in, they show up as ChrW (61544) and ChrW(61606). So I know they have at least 3 numerical equivalents, but I don't know which numbers to use and how to do the following:
1. Recognize a symbol.
a. I use code to go to a given table cell and select the first character of the contents.
b. I want to know whether the selection is one of these two symbols. If it were plain text, I could say "If selection = "X". But how can I find out "if selection = Wingdings character code 114 ( = character number -3930 = ChrW (61554) ) ?
2. How to search for one and replace it with the other.
Similar question. What VBA code would I use to search for one of these symbols and replace it with the other?
Thanks for you help!
' this is a 3D checkbox
Selection.InsertSymbol Font:="Wingdings", CharacterNumber:=-3982, Unicode _
:=True
'this is a 3D radio button
Selection.InsertSymbol Font:="Wingdings", CharacterNumber:=-3930, Unicode _
:=True
ASKER
Thanks, Graham
This is very helpful. But I'm still ironing out a few bugs. Could I ask you to take another look? I've attached my whole macro below. Again, I have tables in my document that may contain the box or radio symbol. This macro is a smart toggle that can be used to swap the symbols either in the entire table where the selection point is located, or only the current selection.
The steps are:
1. Make sure we're in a table
2. Determine whether we're doing the find/replace for the whole table, or just a selection.
3. Find out which symbol is in the table or current selection (it will only be one or the other)
4. Swap symbols from the found to the other, in the table or selection as appropriate.
The swap works, but I don't yet have a handle on limiting the range to which it applies: See attached image. If I'm dealing with a selection (say, one column), it goes ahead and swaps all the symbols in the entire table. If I'm dealing with a table, it swaps symbols not only in the current table but every table after that in the document. I admit I'm not strong on find/replace coding.
Can you help?
Thanks,
Bryce
This is very helpful. But I'm still ironing out a few bugs. Could I ask you to take another look? I've attached my whole macro below. Again, I have tables in my document that may contain the box or radio symbol. This macro is a smart toggle that can be used to swap the symbols either in the entire table where the selection point is located, or only the current selection.
The steps are:
1. Make sure we're in a table
2. Determine whether we're doing the find/replace for the whole table, or just a selection.
3. Find out which symbol is in the table or current selection (it will only be one or the other)
4. Swap symbols from the found to the other, in the table or selection as appropriate.
The swap works, but I don't yet have a handle on limiting the range to which it applies: See attached image. If I'm dealing with a selection (say, one column), it goes ahead and swaps all the symbols in the entire table. If I'm dealing with a table, it swaps symbols not only in the current table but every table after that in the document. I admit I'm not strong on find/replace coding.
Can you help?
Thanks,
Bryce
Sub ToggleCheckRadio(control As IRibbonControl)
Dim ThisTable As Table
Dim rng As Range
Dim WhichType As String
Dim ReplaceIn As String
Dim findcode As Long, replacecode As Long
Dim x As Integer
' MAKE SURE WE'RE IN A TABLE
If Selection.Information(wdWithInTable) = False Then
MsgBox "Selection point must be in a table to use this command."
Exit Sub
End If
' DETERMING WHETHER SEARCHING WHOLE TABLE OR SELECTION
If Len(Selection.Range) = 0 Then
Set ThisTable = Selection.Tables(1)
ReplaceIn = "table"
' DETERMINE WHETHER TABLE CONTAINS CHECKBOX OR RADIO SYMBOL For x = 1 To 3 ' examine first cell in top 3 rows
Set rng = ThisTable.Cell(x, 1).Range
With rng.Find
.Text = ChrW(61554) ' checkbox
.Forward = True
.Execute
If .Found = True Then
WhichType = "checkbox"
Exit For
End If
End With
Next x
For x = 1 To 3
Set rng = ThisTable.Cell(x, 1).Range
With rng.Find
.Text = ChrW(61606) ' radiobutton
.Forward = True
.Execute
If .Found = True Then
WhichType = "radio"
Exit For
End If
End With
Next x
Else 'SEARCHING SELECTION ONLY
ReplaceIn = "selection"
Set rng = Selection.Range
' DETERMINE WHETHER SELECTION CONTAINS CHECKBOX OR RADIO SYMBOL
With rng.Find
.Text = ChrW(61554) ' checkbox
.Forward = True
.Execute
If .Found = True Then
WhichType = "checkbox"
End If
End With
With rng.Find
.Text = ChrW(61606) ' radiobutton
.Forward = True
.Execute
If .Found = True Then
WhichType = "radio"
End If
End With
End If ' --------------------------------------------------
' SWAP SYMBOLS IN GIVEN SELECTION
Select Case WhichType
Case "checkbox"
findcode = 61554
replacecode = -3930
Case "radio"
findcode = 61606
replacecode = -3982
Case Else
Exit Sub
End Select
If ReplaceIn = "table" Then Set rng = ThisTable.Range
If ReplaceIn = "selection" Then Set rng = Selection.Range
With rng.Find
.Text = ChrW(findcode)
.Forward = True
Do While .Execute
rng.InsertSymbol Font:="Wingdings", CharacterNumber:=replacecode, Unicode:=True
Loop
End With
End Sub
tables.jpg
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
That did the trick! Thanks much.
Here are a couple of macros:
Open in new window