Solved

Find and Replace Symbols Programmatically in Word 2007

Posted on 2010-11-08
4
1,863 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:versatilebb
  • 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:versatilebb
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:versatilebb
ID: 34112443
That did the trick!  Thanks much.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now