Use Character Codes to Find or Insert Special Characters





Use Character Codes to Find or Insert Special Characters

figs/beginner.gif figs/hack30.gif

It's easy to find common characters on the keyboard. But when you need to find or create an uncommon character, using character codes can make things much easier.

Word uses Unicode characters to internally store all the text you type, including special characters and symbols. Unicode is, to paraphrase the official Unicode web site (http://www.unicode.org), a universal character-encoding standard, designed to ensure that any text can be represented on any platform, and in any language.

Prior to the introduction of the Unicode standard, many software programs used (and many still do use) other character encodings, such as the original ASCII character set, or similar encodings that include the ASCII characters and some additional ones. ASCII and other older character-encoding standards do not have the capacity to represent all the characters possible in multiple languages, and they often create problems when transferring text among applications used in different countries or regions.

Though Word uses Unicode internally, its ASCII roots poke through when you insert characters into documents and search for characters using Find and Replace.

In this hack, the term "ASCII" refers to the characters represented in Word by the codes 0-255. ASCII is a bit less of a mouthful than "Windows Code Page 1052," the real name of the encoding set—see http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1252.TXT.


1 Inserting Special Characters

You can use both ASCII character codes and Unicode character codes to insert special characters into text in Word.

1.1 Using ASCII codes

There are 256 characters in the ASCII set, numbered from 0 to 255. Not all of the codes represent printable characters, and not all are used in Windows, but if you're familiar with the code for a particular symbol, entering it from the keypad can be quicker than going through InsertSymbol.

For example, the ASCII code for a micro sign (m) is 181. To insert a micro sign at the insertion point, do the following:

  1. Turn on Num Lock for the numeric keypad.

  2. Hold down the Alt key.

  3. On the numeric keypad, type 0181.

  4. Release the Alt key.

The micro symbol will be inserted into your document.

1.2 Using Unicode codes

Unicode supports many more than 256 characters. It has enough "space" to represent every character in every language, with plenty to spare. Unicode codes are usually represented as hexadecimal values, so they're a mix of digits and the letters A-F.

Not all fonts support Unicode, but many of the common ones, such as Times and Arial, do.

The Unicode code for a musical eighth-note character is 266A. To insert one at the insertion point, do the following:

  1. Type 266A.

  2. Press Alt-X.

The code you typed will be converted to the eighth-note symbol.

Though you can search for characters by their Unicode numbers in Word 2000 (as described in the next section), you can't insert them directly using this method in Word 2000.


You may find that it's easier to search for codes using the links available at the Unicode web site (http://www.unicode.org) and insert characters using this method than it is to search among the thousands of characters in the InsertSymbol dialog.

2 Searching for Special Characters

You can use these same character codes when searching for special characters in text. The ASCII codes are particularly useful when you're performing a wildcard search.

2.1 Searching with ASCII codes

Independent of the ASCII and Unicode codes, Word includes several special character codes that you've likely seen before, such as ^p to search for a paragraph mark or ^t to search for a tab. The Word help files cover these codes extensively, but three deserve special attention because they can match more than one character:

  • ^# matches any digit.

  • ^$ matches any letter.

  • ^w matches any whitespace.

The special character codes will help you with simple searches, but if you check the "Use wildcards" box in the Find and Replace dialog, you'll get the error message shown in Figure.

Some of Word's special codes can't be used with wildcard searching active
figs/wrdh_0409.gif


So how do you match a paragraph mark when wildcard searching is active? By using the ASCII code. To search for a character by its ASCII code, type ^0 in the "Find what" box, followed by the character code.

The ASCII code for a paragraph mark (technically, it's a carriage return) is 13. So, to search for paragraph marks while wildcard searching is activated, you'd type ^013 in the "Find what" box.

Be aware that some fonts assign different characters to the ASCII codes.


With wildcard searching active, you can also search for ranges of characters. For example, type [^0100-^0104] in the "Find what" box to search for characters between d and f.

2.2 Searching with Unicode codes

You can also search for a character using its Unicode code by prefacing it with ^u. However, you can't directly search using the hexadecimal code; you must enter its decimal equivalent.

For example, the decimal equivalent of 266A, the musical eighth-note character, is 9834. So, to search for that character, enter ^u9834 in the "Find what" box.

Unlike ASCII codes, Unicode codes won't work with wildcard searching active.


So how do you convert a hexadecimal number to a decimal number? Fortunately, VBA includes a function that will do it for you. To convert a "hex" number to its decimal equivalent, select ToolsMacroVisual Basic Editor to display the Visual Basic Editor in a separate window.

In the small window titled "Immediate" near the bottom of the screen, type the following and press Enter:

?CDec(&H

code

)

code is the Unicode code, as shown in Figure.

Converting a hexadecimal value to its decimal equivalent
figs/wrdh_0410.gif


3 What's That Character?

What if you need to replace some obscure character in an unusual font? For example, say you open a giant document from a client and find the same odd character at the beginning of every paragraph. If Word won't let you paste the character into its Find and Replace dialog, it seems you're stuck repairing it by hand.

If you knew the character's numeric code, you could search for it, but this character falls way off the usual list. How can you find its numeric code? Put the following macro in the template of your choice [Hack #50], select ToolsMacroMacros, choose WhatCharacterCode from the list, and click the Run button:

Sub WhatCharacterCode( )

MsgBox Asc(Selection.Text)

End Sub

This macro will display the ASCII character code for the first character in the current selection; you can then search for it using the ^0 syntax.

If the macro reports a value of 63 and fails to match the character, you may be facing a Unicode character. The following macro will report the Unicode code of a character, which you can search for using the ^u syntax:

Sub WhatUnicodeCharacterCode( )

MsgBox AscW(Selection.Text)

End Sub

The result displayed will be the decimal version of the Unicode character code, not the hexadecimal version used when inserting Unicode characters.


Jack Lyon and Andrew Savikas


     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows