Solved

Find and Replace Symbols Programmatically in Word 2007

Posted on 2010-11-08
4
1,890 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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

910 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

22 Experts available now in Live!

Get 1:1 Help Now