Hack 30 Use Character Codes to Find or Insert Special Characters
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.
4.5.1 Inserting Special Characters
You can use both ASCII character codes and Unicode character codes to insert special characters into text in Word. 4.5.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
Insert For example, the ASCII code for a micro sign (m) is 181. To insert a micro sign at the insertion point, do the following:
Turn on Num Lock for the numeric keypad.
Hold down the Alt key.
On the numeric keypad, type 0181.
Release the Alt key.
The micro symbol will be inserted into your document. 4.5.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:
Type 266A.
Press Alt-X.
The code you typed will be converted to the eighth-note symbol.
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 Insert 4.5.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. 4.5.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 4-9.
Figure 4-9. Some of Word''s special codes can''t be used with wildcard searching activeSo 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.
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. 4.5.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.
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 Tools 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 4-10.
Figure 4-10. Converting a hexadecimal value to its decimal equivalent4.5.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 Tools
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
Jack Lyon and Andrew Savikas
|