Solved

Find and Replace Symbols Programmatically in Word 2007

Posted on 2010-11-08
4
2,052 Views
Last Modified: 2012-08-13
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!
' 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

Open in new window

0
Comment
Question by:Bryce Bassett
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 34091189
Yes. There is some inconsistency in the way that difference methods address symbols.

Here are a couple of macros:
Sub FindCellWithSymbol()
    Dim rng As Range
    Set rng = ActiveDocument.Range
    With rng.Find
        .Text = ChrW(61554)
        .Forward = True
        Do While .Execute
            If rng.Tables.Count = 1 Then
                rng.Expand wdCell
                rng.Select
                Exit Do
            Else
                rng.Collapse wdCollapseEnd
                rng.End = ActiveDocument.Range.End
            End If
        Loop
    End With
End Sub

Sub FindAndReplaceSymbols()
    Dim rng As Range
    Set rng = ActiveDocument.Range
    With rng.Find
        .Text = ChrW(61554)
        .Forward = True
        Do While .Execute
            rng.InsertSymbol Font:="Wingdings", CharacterNumber:=-3930, Unicode:=True
            rng.Collapse wdCollapseEnd
            rng.End = ActiveDocument.Range.End
        Loop
    End With
End Sub

Open in new window

0
 

Author Comment

by:Bryce Bassett
ID: 34095716
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
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

Open in new window

tables.jpg
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 350 total points
ID: 34107812
The logical way that I indicated was to reset the end point of the range after each successful find.

However, in that case, the Find unaccountably misses some of the cells. Also, it is not possible to represent the range of a selection of a block of columns that is less than the whole table.

For these two reasons, this code searches each cell individually.
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
Dim cl As Cell

' 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
    'If Selection.Columns.Count < Selection.Range.Columns.Count Then
    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
    For Each cl In ThisTable.Range.Cells
    Set rng = cl.Range
        With rng.Find
        .Text = ChrW(findcode)
            .Forward = True
            If .Execute Then
                rng.InsertSymbol Font:="Wingdings", CharacterNumber:=replacecode, Unicode:=True
            End If
        End With
    Next cl
End If
If ReplaceIn = "selection" Then
    For Each cl In Selection.Cells
        Set rng = cl.Range
        With rng.Find
        .Text = ChrW(findcode)
            .Forward = True
            If .Execute Then
                rng.InsertSymbol Font:="Wingdings", CharacterNumber:=replacecode, Unicode:=True
            End If
        End With
    Next cl
End If
End Sub

Open in new window

0
 

Author Closing Comment

by:Bryce Bassett
ID: 34112443
That did the trick!  Thanks much.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This collection of functions covers all the normal rounding methods of just about any numeric value.
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question