| Barry MacDonnell's
Toolbox for WordPerfect
Macros, tips, and
templates for Corel® WordPerfect® for Windows®
| Page updated Jun 2, 2020
| Add Codes
- Quickly search the current
document for up to 10 specified words or phrases and, when found, add
one or more text attributes (e.g., Bold, Underline, Italics, Redline,
Large size, etc.) to them wherever they appear in that document
Related pages -
• ReplaceCodes - A related macro that can search for a menu-chosen text attribute (16 attributes, such as bold, italics, underline, redline, highlighting, etc.) wherever found in the document and then change it to something else — such as underlined text to italic text.
• Code2txt - A macro that can change text surrounded by WordPerfect format codes (e.g., [Bold On]...[Bold Off]) to text surrounded by user-defined plain-text markers (e.g., <B>...</B>) — and vice versa.
• DelCodes - a small suite of three macros to quickly delete one or more attribute codes of various types — including many types not processed by the Replace Codes macro.
• HiLite - A suite of macros for annotating or marking up drafts or other documents using highlighting; includes a macro (HiLiteDoc) that can highlight specific words in a document [and another to remove all (or just some) highlighting from a document with an option to remove the underlying highlighted text].
• LineWrap - A macro to remove hard returns ([HRt]) at the end of lines in plain-text (ASCII-type) files that have been imported into WordPerfect, and replace them with soft returns ([SRt]). This allows "word wrapping" of sentences and paragraphs and makes reformatting such paragraphs or other copied material (such as e-mail messages or copied material from a Web site) much easier.
• ReplStyl - A macro that can replace one built-in (system) or custom (user-created) style with another in a document, or remove a style's codes to remove the style wherever it was applied in the document.
| Purpose and operation
Add Codes.wcm is a macro that can quickly search for user-specified characters, words, or phrases and apply one or more text attributes (i.e., format codes) to that text.Tips
Note: The responsible codes for text attributes are visible in Reveal Codes. However, not all possible codes are offered by the macro (e.g., highlighting), but those that are applied can be changed, augmented, or removed with a companion macro: see Replace Codes in the Library.
■ When played on an open document it will display a menu with 10 text entry fields and several processing and formatting options:
■ The fields are used to contain words or phrases to search in the document and, when found, the macro will rapidly apply one or more chosen "text attributes" such as Bold, Italics, Underline, Redline, Large size, etc. — or some combination of these — to all such items found in the document.
Optionally it can do this (using the "Pause to confirm..." choice) to just certain items that you can choose when the macro plays.
■ You can use just one menu field (you must use at least one) or you can use several fields. Empty fields are automatically detected and skipped over.
■ You can type the desired items to search into any of the menu's 10 fields (and in any order) -or- you can click in the document and navigate to an example of a search item and then select, copy, and paste it into an available dialog field. All material used as search items will be inserted in the fields as plain text.
¤ A "word" or "phrase" is one or more letters, numbers, space characters, and normal (keyboard) punctuation. The program will remove existing formatting (e.g., bold or italics attributes) from text that you paste into the menu's fields. The macro will then search for, and identify, text strings to add chosen formatting to them. (Such formatting — whether existing or applied by the macro — can be quickly and easily removed or changed anytime with the related Replace Codes macro: see below.)
¤ There is a maximum of 79 such characters per field. Longer strings are truncated by the program to this limit.
Tip: If you still want to find such lengthy items (or even shorter items embedded in longer strings), un-check (clear) the "Search for WHOLE words only..." checkbox in order to search for PARTIAL words. But applying format codes to such long, potentially truncated strings is not ideal for most tasks except perhaps for lhelping to visually locate them during editing.
¤ Character symbols (inserted with Insert, Symbol or inserted with Ctrl+w) will not be processed, nor will any text strings containing them. (However, keyboard symbols and punctuation marks will be processed.)
¤ If a searched item in the document has the same text attribute (e.g., Bold, Italics, etc.) that was chosen from the macro's menu, no harm is done. Choosing a menu formatting option and applying that attribute does not affect an existing attribute of the same type previously applied to the item.
¤ The items that are typed or pasted into each menu field are removed from the menu when the macro finishes. If you frequently search for one or more specific items, they can be retained (and displayed) as preferred menu search defaults: See the tip below.
¤ If an item in any menu field is not found in the document the macro will immediately search the next item without notification. Otherwise, a total count of items processed will appear at the end of processing.
¤ There are a few limitations, discussed in the Known Limitations section below.
■ The macro's menu has additional search options (checkboxes):
□ start at the top of the document or at the current cursor location,
□ search for whole words or specific characters/strings,
□ search "case sensitive" or search "any case", and
□ pause at each instance found to get your confirmation:
Yes (current item) / No (skip item) / All the rest / Quit (exit)
Additional check boxes and radio buttons let you apply one or more text attributes to the items in the 10 fields wherever they are found.
When items are found which match what is contained in the dialog's fields and also match your search specifications, the macro will immediately apply your chosen text attribute(s) to them.
■ Two general methods to reverse the changes made by the Add Codes macro:
 Undo the macro's changes.
When the macro finishes you can undo its work with Edit, Undo (or just use Ctrl+z).
If done immediately, all text attributes added by the macro removed in one operation. If not done immediately, removals can depend on how large an Edit, Undo/Redo History number you have previously set (300 Undo's is the maximum) and how many subsequent edits you have made during that session. And for later removals, even after re-launching the program, see the next paragraphs.
If you later want to quickly remove or change one or more attributes (applied to one or several items in a document) see the the free companion macro, Replace Codes.
Replace Codes lets you replace, augment (add to), or delete any of 16 existing format code types — including some that the Add Codes macro does not deal with. Hence you could use it to replace any of the text attributes that Add Codes added with (e.g.) the current highlight color or a custom style. (It also allows pausing at each instance for your confirmation.)
Other ways to remove text attribute codes
▸ Use WordPerfect's Find and Replace (more here) to search for the codes and replace them with <Nothing>.
▸ Or just open Reveal Codes and manually delete (or drag) the codes applied to particular items from the Reveal Codes window.
☼ Always make a backup of your document before playing this (or any) macro.
☼ The macro's menu choices can be set to your preferred defaults in the macro's main code section.
For example, if you use the macro often to search for one or more of the same items — such as a company name or a web site address — you can open the macro for editing (Tools, Macro, Edit) and then edit the section in the User Modification Area containing 10 variables that can "feed" their text into the 10 text entry fields as soon as the macro is launched.
Once the edited macro is saved (Save & Compile button on the macro toolbar) the text that you assigned to those variables will be retained until you disable, remove, or change them in that section of the macro code.
Thus you can use any combination of three methods to input your search words or phrases: type them or paste them into the menu dialog at macro run time, and/or store one or more search words inside the macro's User Modification Area.
[Tip: If you have more than 10 items to retain inside the macro's code you can simply save one or more copies of the Add Codes macro with a different name and with different "feed" items, and then play them in sequence on the same open document.]
Known limitations and cautions
■ Be aware that the S̶t̶r̶i̶k̶e̶o̶u̶t̶ and Redline text attributes are NOT the same thing as deletions and insertions created with File, Document, Compare/Review even though they may look the same on your screen.
The Compare/Review feature (not available in some editions/versions of WordPerfect) produces deletions and insertions using special codes that work differently from text attributes. Hence those Compare/Review codes are not used by the Add Codes macro. See (e.g.) "Using Document Compare" and WordPerfect's Help (F1) for more.
■ While hyphenated words typically can be processed by the macro — i.e., words with either one or more hyphen codes ([- Hyphen]) or one or more hyphen characters (-)) — it cannot process words with hard space codes ([HSpace]) since these are converted to normal space characters by the macro language before they can be searched. [Also see below for searching mixed hyphen codes in the same phrases.]
Note: Hyphen characters or hard spaces are often used to "glue" text together so it wraps as a block to the next line at the right margin. As noted, hyphens of either type (except mixed hyphen types in the same text string) can be searched and processed by this macro. But you might need to visually inspect and edit phrases with Hard Spaces to manually add text attributes to them.
■ Searching for a format code inside another code, such as a ...
Style code (including the initial Open Style code in a document),
Text variable, or a
... is not curretnly possible in WordPerfect (without lots of additional code This is a limitation of the program, not of this macro. (It even exists in the program's Edit, Find and Replace feature.)
Hence this macro can only process text attribute codes that are directly visible in Reveal Codes.
Text characters or format codes inside the aforementioned "container-like" codes need to be edited/changed from inside those container codes. Double-click on those codes in the Reveal Codes window to examine and edit them.
■ Mixing hyphen codes and hyphen characters, and/or mixing regular space characters with hard space codes, in the same search text item is problematic.
While the macro has been written to detect WordPerfect format codes such as hyphenated text (joined with [- Hyphen] codes) it cannot detect text containing mixed types of these items — such as three or more words joined by both hyphen codes ([- Hyphen]) and hyphen characters (-).
[Note: Hyphen characters can be manually entered in a document with the Ctrl+hyphen key combination while editing that document. Similarly, hard space codes are entered using Ctrl+spacebar. The latter are converted by the macro language during processing to regular space characters; this conversion is not currently detected by the macro.]
This mixed-type issue is typically rare, but be aware of it when adding multiple (more than two) search words in any individual field in the macro dialog that might be joined by such mixed hyphen types or space types. You might want to manually search for just a character part of the string to locate the item and add formatting to it manually.
[Macro writers might be interested in the methods used to process these items: See the next section.]
Notes for macro writers
This macro addresses the macro language's poor ability to search for both hyphen codes and hard space codes (shown in Reveal Codes as [- Hyphen]).
Hyphen characters can be manually entered in a document with the Ctrl+hyphen key combination while editing that document. Similarly, hard spaces are entered using Ctrl+spacebar.
However, the macro language strips them out in the dialog (or in an assignment to a variable) and replaces them with their character equivalents — which are not the same to a search routine.
Here's the problem with hyphen codes (and similarly with hard space codes):
Hyphen codes are entered in WordPerfect documents using the hyphen key.
However, storing hyphenated words for a macro search (e.g., in a dialog box field or in a macro variable) containing hyphen codes automatically converts them to hyphen characters. (Similarly, hard space codes are converted to regular space characters.)
The solution used in this macro for any hyphen characters that were embedded in stored words is to first transform them into hyphen codes and then conduct a separate search for that word with this type of hyphen. [See the ForEach..Endfor segment in the macro code.]
Does it fix all such problems?
While the macro's method works well for the vast majority of such "connected" text it cannot account for some odd (and hopefully rare) situations in the document, such as (e.g.) when three or more words are joined by mixed hyphen types (i.e., a string of characters containing both hyphen character(s) and hyphen code(s).
Mixed, multiple hyphen types in these search items will not be found by this macro —- there are too many possible combinations and complications — so the only reasonable solution seems to be to visually inspect the document following macro play for any such connected words that do not have your desired text attribute applied (e.g., bold, etc.) and then apply the attribute to those document items manually. [Tip:Search using Edit, Find and Replace and search for a part of the connected word; then "step" through the document with Find Next.]