Finding and replacing symbols

Article contributed by Dave Rado

When it comes to performing Find and Replace operations, symbols listed in the Insert + Symbol dialog are a nightmare to work with in Word 97 and above – unless, that is, you stick exclusively to using Unicode characters, and never use decorative fonts such as Wingdings or Symbol. But with most of the fonts that are in every day use, doing that puts quite a severe limitation on you, and in any case, documents you are emailed will often contain characters formatted with decorative fonts.

Basic Latin symbols listed under (normal text)

It's easy to find and replace characters such as ©, é, ä, or any character listed in the Symbol dialog with the Font set to (normal text) and the Subset set to Basic Latin, (or to put it another way, any character included in the ANSI character set). Just insert the character(s) into your document and then cut and paste them into the dialog.

The easiest way is to insert both the find and replace strings into your document first, next to each other, so you can paste both – using Ctrl+V – into both the Find what and Replace with boxes and delete as appropriate.

So if you want to replace Andre with André, type AndreAndré, paste that into both boxes of the dialog, and delete André from the first box and Andre from the second.

If you want to do it programmatically, turn the macro recorder on while performing these operations.

Upper Unicode characters and symbols which use decorative fonts

When it comes to upper Unicode characters such as:

... or symbols from decorative fonts such as Symbol or Wingdings, things become very messy.

For example, if you apply the Wingdings font to the letter q, you get a square bullet. If you then search for the letter q – even if you specify that the font should be Wingdings – Word won't find the bullet symbol! Similarly, if you apply the Symbol font to the letter d, you get the Delta symbol, which Word will then not find if you search for the letter d.

In Word 97, if you highlight your square bullet symbol and paste it into the Find dialog, Word will find it. But in Word 2000 it won't!!!

If, in Word 97, you record the above Find operation using the macro recorder, it turns out Word 97 was searching for the Unicode character 61553. If you play back the macro in Word 2000, searching for Unicode 61553, Word 2000 will find the character. But it will also find (in both versions of Word) any other character whose Unicode number happens to be 61553. So using the Unicode number is not a wholly reliable method.

In the specific case of decorative fonts included in the Insert + Symbol dialog (such as Symbol and Wingdings), you can use the Microsoft-supplied FindSymbol macro included in Macros9.dot (which works with both versions of Word). Unfortunately, the FindSymbol macro does not work with special characters from the (normal text) character set displayed in the Insert + Symbol dialog (or with the characters displayed on the Special Characters tab of the dialog). As you will see further down, there is no reason why it shouldn't – it was just laziness on the part of the MS programmers.

Also, if you want to write your own Find and Replace macros, to find and/or replace specific symbols, the Microsoft macro is too unwieldy to be of much use to you.

In the case of upper Unicode characters displayed under (normal text), you can paste them into the Find dialog (in Word 2000 as well as Word 97); and unless you happen to have a symbol in the document from a decorative font that has the same Unicode number, that method will work reliably. But if you want to be safe, you will need to use a macro. Fortunately, such a macro is relatively straightforward to write – even if you are new to VBA.

How to write your own macro to do the job

Finding symbols
The first thing you need to do is find out both the font and Unicode number of the character you want to search for and /or replace with. If the character was inserted from the Insert + Symbol dialog, its name won't be displayed in the Fonts list on your toolbar, but you can get at the information by selecting the character and running the following macro:

Sub GetCharNoAndFont()

With Dialogs(wdDialogInsertSymbol)
    Debug.Print "Font: " & .Font
    Debug.Print "Char number " & .CharNum
End With

End Sub

Press Ctrl+G or select View + Immediate Window to see the results.

In the case of the Delta symbol, that will return:

Font: Symbol
Char number -3996

You can now use the following macros to find the next instance of the Delta symbol (if there is one). As you'll see shortly, it is very straightforward, even if you are a complete programming novice, to customise the following macros for your needs.


Sub FindDeltaSymbols()
    'Call the main "FindSymbols" macro (below), 
    'and tell it what character code  and font to search for
    Call FindSymbols(FindChar:=ChrW(-3996), FindFont:="Symbol")
End Sub


Sub  FindSymbols(FindChar As String, FindFont As String)

Dim FoundFont As String, OriginalRange As Range, strFound As Boolean
Application.ScreenUpdating = False

'set range to return to in case symbol not found
Set OriginalRange = Selection.Range

strFound = False
With Selection.Find
    .ClearFormatting
    .Text = FindChar 
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
   
    Do While .Execute    'Keep going until nothing found
        If Dialogs(wdDialogInsertSymbol).Font = FindFont Then
           'If the correct character was found, exit loop
 
         strFound = True
           Exit Do
        Else
           'Otherwise search again
           Selection.Collapse wdCollapseEnd
        End If
    Loop

    If Not strFound Then
        'if nothing found, search from the beginning of the document
  
     ActiveDocument.Range(0, 0).Select
        Do While .Execute
            If Dialogs(wdDialogInsertSymbol).Font = FindFont Then
                strFound = True
                Exit Do
            Else
                Selection.Collapse wdCollapseEnd
            End If
        Loop
    End If
  
End With

If Not strFound Then
    OriginalRange.Select
End If

Set OriginalRange = Nothing
Application.ScreenUpdating = True

End Sub

To customise it, all you have to do is to use the GetCharNoAndFont() macro again, to find out what chaacter number and font you need to use; and then change the character number and font used in the FindDeltaSymbols() macro to the correct values for the symbol(s) you want to find.

For example, if you want to find the one-eighth symbol, which is listed under (normal text) in the insert symbol dialog, insert the symbol into your document, select it, and run the GetCharNoAndFont() macro. This will return:

Font: (normal text)
Char number 8539

So just by changing the reference to Symbol in the FindDeltaSymbols() macro to (normal text) instead; and the reference to -3996 to 8539; we get the following:

Sub FindOneEigthSymbols()
    Call FindSymbols(FindChar:=ChrW(8539), FindFont:= "(normal text)")
End Sub

Use this with exactly the same FindSymbols() macro as before.

Finding and replacing symbols
If you want to do a Find and Replace operation, you will need the character numbers and fonts for both the Find and the Replace characters. As before, you can use the GetCharNoAndFont() macro to obtain this information.

You can then customise the following first of the following macros, which calls the second macro, and in this example replaces all instances of the Delta symbol in your document with Beta symbols. To customise it, just change the character numbers and fonts in the ReplaceAllDeltaSymbolsWithBetaSymbols() macro (but leave the ReplaceAllSymbols() macro as it is):


Sub ReplaceAllDeltaSymbolsWithBetaSymbols()
    'Call the main "ReplaceAllSymbols" macro (below), 
    'and tell it which character code  and font to search for, and which to replace with
    Call ReplaceAllSymbols(FindChar:= ChrW(-3996), FindFont:= "Symbol", _
            ReplaceChar:=-3998, ReplaceFont:="Symbol")
End Sub


Sub ReplaceAllSymbols(FindChar As String, FindFont As String, _
        ReplaceChar As String, ReplaceFont As String)

Dim FoundFont As String, OriginalRange As Range, strFound As Boolean
Application.ScreenUpdating = False

Set OriginalRange = Selection.Range
'start at beginning of document
ActiveDocument.Range(0, 0).Select

strFound = False
With Selection.Find
    .ClearFormatting
    .Text = FindChar
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
   
    Do While .Execute
        'keep searching until nothing found
  
     If Dialogs(wdDialogInsertSymbol).Font = FindFont Then
           
'Insert the replacement symbol where the found symbol was 
            Selection.InsertSymbol Font:=ReplaceFont, _
           CharacterNumber:=ReplaceChar, Unicode:=True
        Else
            Selection.Collapse wdCollapseEnd
        End If
    Loop
  
End With

OriginalRange.Select

Set OriginalRange = Nothing
Application.ScreenUpdating = True

End Sub