Home | Tips | Library | Other Authors | Other WP Sites | Writer's Links | Contact | Site Map | Donate

Barry MacDonnell's
Toolbox for WordPerfect

Macros, tips, and templates for Corel® WordPerfect® for Windows®
© Copyright 1996-2017 by Barry MacDonnell. All Rights Reserved.

Page updated Jul 15, 2017

REPLACEM - Replace multiple words in one pass

Download REPLACEM.ZIP (Version 1.23; 01/13/16; 20,808 bytes)

Updated in WordPerfect X7 but should work in other versions (back to at least WP9)

WordPerfect 11 users: See important information about using macros in the first release of WP11 (11.0.0.233) at the top of this page.

Downloading, Documentation, Modifications, and Support 

More macros in the Library...

Related macros:

If you simply want to use a macro to quickly apply colored highlighting or font attributes (e.g., bold, italics, or redline) to several different words in a document -

•  See HiLiteDoc to add colored highlighting to one or more different words/phrases.

•  See the macros in the footnotes below.

If you want to replace or augment (or remove) a font attribute (e.g., bold) from any words that have them with one or more other font attributes (e.g., bold+italic) see -

•  Replace Codes - Plus

  

Purpose

Replacem.wcm is a macro that you can use to find and replace many words or phrases in the current document in one easy operation. With it you can make extensive changes to existing documents with a mouse click.

Why use it? 

WordPerfect's QuickCorrect only works to replace words as you type; it can't find and replace existing (i.e., already typed) words. For that you usually use Edit, Find and Replace. But Find and Replace only works with a word or a phrase at a time, whereas this macro can work with dozens of words or phrases at a time. 

Replacem.wcm is useful whenever you need to make more than a few Find-and-Replace passes in a document -- especially if you need to repeat the same operation using the same words on additional documents.

For example, the task of editing a book or lengthy contract to change several items can be greatly simplified by using what amounts to an "inclusion" list of words for the macro to find and replace. (You store your replacements is a separate, editable WordPerfect file, making it easy to update the word list as well as keep separate lists for different projects.)

[Screen shot of Replacem.wcm menu]

Alternatives and options

¤    WordPerfect X6 (released in 2012) ships with a new macro, MultipleFindandReplace.wcm, that has a similar purpose. However, unlike Replacem.wcm, it does not allow prompting the user for confirmation and it does not store the find/replace items for future use. But for the current session with simple words/phrases, it can be easier to use since no separate user document containing replacements is required.

¤    If you simply want to use a custom macro to either (A) quickly apply font attributes (e.g., italics or redline) to several different words in a document, or (B) replace several different words with other words without creating a separate user document (required with Replacem.wcm), see the macro code snippets in the footnotes below.

¤    See also:
Replace Codes - Plus.wcm here (replaces, augments, or removes one or more font attributes such as bold, italics, redline, highlighting, etc. from any words that have them), and HiLiteDoc.wcm here (adds colored highlighting to one or more words wherever they are found -- good for marking up initial drafts).

¤    You can use styles to change or update various text in a document. Styles can contain format codes, graphical items, tables, watermarks, margin settings, and even other ("nested") styles. But since they can also contain text (or only text), they can be used to quickly update the same information that might be located in several places in a document. In fact, WordPerfect 10 introduced text variables (a form of style) to do just this task. [Also see Noal Mellott's template-and-macro approach to update a contract or short story (or any final merged document) on WordPerfect Universe here.]

Instructions and operation

[These instructions also exist at the top of the macro's code.]

Step 1. You must create and save at least one ordinary, separate WordPerfect document (.WPD) containing a list of your items and their replacements. The words must be in a 2-column table in plain text (no formatting codes), as explained below.

This separate document will be "called" by the macro to make the replacements to the specified items in the main document.

This is easy to do with Table, Create (or Format, Table, Create in earlier versions of Wordperfect).

Be sure the separate document contains only a 2-column table, with the first column containing the word or words to replace, and the second column containing the replacement word or words -- each pair in the same row -- something like this:
Replacem.wpd table example

When creating the list:

Use plain text. Don't use any formatting (fonts, font sizes, bold, italics, etc.) for the items in the table. Due to the way the macro stores these items, format codes are stripped out.

Don't paste. It is better to type the items into the list rather than paste them as unformatted text with Edit, Paste Special since doing this (as of WordPerfect 11; see here) converts normal hyphens (which are codes) into hyphen characters ("hard hyphens"). This means the normal-hyphen item in the document will not be found. Instead, just press the hyphen key if that is the way it was entered in the main document. [Tip: If you are not sure about the type of hyphen in the document at the item's location you can simply add both versions of the hyphenated item to the table list (each in their own table row). For more on the types of hyphens used in WordPerfect see here.]

Don't add anything around the table. The only thing in the document should be the 2-column table.

Keep the items in Column 1 short. The word or phrase in Row 1, Column 1 is limited by WordPerfect's Find function to about 79 characters, so keep all items under this length. Each item in Column 1 will be replaced by the word or phrase in the same table row (in Column 2).

How the macro handles empty cells in the table:

During playback, if the macro runs into an empty cell in Column 1, it will terminate immediately, so you don't need to worry about empty rows below your list of replacement words. However, don't leave empty cells in Column 1 in the middle of your list, or all words following that location won't get replaced!

On the other hand ... an empty cell in Column 2 means the adjacent word or phrase in Column 1 will be replaced with "nothing" (which may still leave behind an extra space). This is the equivalent of manually using WordPerfect's Find and Replace function with <Nothing> entered in the Replace field: It deletes the searched word when it is found.

Capitalization and items in lower case:

Replacement words (Column 2) are always NOT case-sensitive during macro play, so the replacement will be in the same case of the word(s) you typed in Column 2. Thus, the replacement item should be capitalized the way you intend it to appear in the document.

Searched words (Column 1) can optionally be case-sensitve. Normally this option is disabled (Off), so if you search for "Word" it will look for both "word" and "Word".

However, you should consider putting all words to be searched case-sensitive -- i.e., Capitalized or in UPPER case (proper names, acronyms, etc.) -- into Column 1 of a different replacement document from the one(s) used for strictly lower case words, and play the macro using that replacement document with the "case sensitive" option enabled. The macro should then find just those capitalized/uppercase words and replace them with the adjacent words in Column 2 of that replacement document.

Spaces, periods (full stops), and hard returns?

Don't forget to include spaces or periods where needed in the table cells, especially following any replacement sentences (i.e., Column 2 items) so that they will be inserted in the main document.

For example, the program often "cleans up" extra spaces when you, or a macro, deletes some text; an insertion at that point can cause the replacement to run into the following text unless you add a space to the inserted material (i.e., in Column 2). [Test a few such replacements to see how this works. On the other hand, extra unwanted spaces can be removed later with a macro such as found here.]

Be sure to exclude hard returns in Column 2 of the table, unless you really want them. [Extra unwanted hard returns can be removed with a macro such as the one found here.] 

Step 2. Save the separate document to disk. 

IMPORTANT: This replacement list document must be named REPLACEM.WPD (note the "M" at the end) and be saved in your default macros folder, along with this macro.  

To find the location of your default macro folder: Click Tools, Settings, Files, Merge/Macro and jot down the name. [You could save the table document to your regular default folder and then move it to the macro folder with Windows Explorer or equivalent file manager.]

You can create up to 9 additional word list files, naming them (in sequence) REPLACE2.WPD (for the second list) through REPLACE0.WPD (for the last list), to match the macro's menu selections. 

Tip: You can also change the names to something more meaningful. If you do that, you must also change the macro code in the Menu section of this macro's code to match the new flenames.

Step 3. Play the macro on the current document (always make a backup of the document before you begin).

Tip: If you have set Undo to "on" (with Edit, Undo/Redo History), you can quickly reverse any changes to your current document with Ctrl+Z.

Alternatives

•  Advanced users: If you simply want to use a custom macro to quickly apply font attributes (e.g., italics or redline) to several different words in a document, or replace several items with other items, etc., without creating a separate user document containing search items and their replacements (required with Replacem.wcm), see the macros in the footnotes 2-4 below.

•  If you want to swap individual words or phrases -- for example, "Party 1" and "Party 2" -- wherever they appear in your document, you can use the "A-B-C Word Triangle" method.

What is it? Imagine a triangle with the first word (or list of words) located on one corner (say, the lower left, or corner "A"), the second word (or list of words) on the opposite corner (lthe ower right, or corner "B"), and some sort of intermediate word(s) at the apex (or corner "C"). Like this:

C

A

B

Using this method, you will need to change "A" words to (intermediate) "C" words to "park" them out of the way temporarily. In effect, the "A" words no longer exist for your current purpose in the document. Then you need to change "B" words to "A" words (the first part of the swap). Finally, you need to change the intermediate "C" words to "B" words, completing the swap.

The net result is that you have swapped "A" words ("Party 1") with "B" words ("Party 2").

This can be done with either WordPerfect's Edit, Find and Replace or with the REPLACEM macro (see below.)

Here's how to do it with Edit, Find and Replace, which should be easy enough to use for occasional single A-B word/phrase swaps:

Step 1

First find and replace the first word or phrase (e.g., "Party 1," which is Word A) with an intermediate word (Word C) not used elsewhere in your document.

A "nonsense" word will do nicely, such as ABCD1. This "parks" the word or phrase out of the way temporarily.

[If you are swapping several words or phrases in one session, replace the next word or phrase on your swap list with a different nonsense word such as ABCD2, etc.]

You might want to pause at each item found before replacing it to make sure it's one that should be swapped. (Using Find and Replace, Match, Case can be helpful if there might be confusion with other common words.)

In this step, because you are replacing all "A" words with "C" words you are effectively hiding the "A" words from the next step.

Step 2

After the replacements have been made with nonsense words, replace the second word or phrase with the first. For example, replace "Party 2" with "Party 1".

Again, you might want to pause at each item found before replacing it to make sure it's one that should be swapped.

In this step, you are simply changing all "B" words into "A" words. (There  will no longer be any "B" words in the document.)

Step 3

Finally, find and replace the "C" words (the nonsense words) with the corresponding "B" words.

In this step, you are changing all "C" words into "B" words.

The net result is all "A" and "B" words have been swapped because all instances of "A" words in the document are now "B" words, and all "B" words are now "A" words. (There are no "C" words left since all of them were changed in step 3.)


Using REPLACEM for the task:

For single swaps (i.e., not several find-and-replace items) each of the three steps above can be placed in a separate table row in an external replacement document (described above) that is associated with the REPLACEM macro. [As noted above you could just use Edit, Find and Replace when swapping only a single word/phrase for another word/phrase. However, F&R would require the three separate steps described above.]

If you are using lists of several words/phrases to swap, you might find it easier to use three external replacement documents (described above) for this method. In our example, the first row of the first list would contain the words "Party 1" and "ABCD1." The first row of the second list would contain the words "Party 2" and "Party 1." the first row of the last list would contain the words "ABCD1" and "Party 2." The second and subsequent rows would follow this pattern. You would then play the macro three times using each of the three lists in turn.

•  For a simple macro that swaps any two selected items (e.g., "Party 1" and "Party 2") throughout a document, see SWAPTHEM.


Footnote 1

[Continued from above:]

Purpose

If you only need to apply formatting (bold, italics, Redline, etc.) to several different words or phrases in a document -- wherever they are found -- you can use a macro like EXAMPLE #1 below. (It is based on a macro published by Greg Turner and Kenneth Hobson on WordPerfect Universe here.)

Tips:

☼  For a simple version that you can create without the need to set up a separate "source list" file, see the next macro in Footnote 2 below.

☼  Instead of a macro, you can use WordPerfect's Find and Replace feature to apply text formatting (bold, italics, etc.) to a single word or phrase wherever it is found in the document. This is often quicker and easier than using a macro, especially for one-off tasks. See Footnote 2 on the How to efficiently use "Find:" in the Find and Replace (F&R) dialog page here.

Assumption

You wish to apply italics to these particular words that might appear anywhere in a document:
Agreement
Assignment
Leased Premises
Master Lease
Transferor
Transferee

IMPORTANT: If you use different words or phrases you will need to insert them in the separate document as described below.
Instructions for examples

The simplest thing to do is first create a separate, plain text (ANSI or ASCII) file with each item on a separate line.

Tip: This "source word" file can be created with a text editor, or it can be created in WordPerfect and saved as ANSI Text. For a simpler version of this macro that might appeal to those who write macros "on the fly," see Footnote 2 below.

Then copy the macro code below and modify it (the items in dark red) to point to the text file on disk and to tell the macro which attribute to apply. To copy the macro code into WordPerfect, see here for instructions.

Then play the macro.

Notes and tips

•  Keep all words and phrases in the source word file to less than 79 characters per item, including spaces. This is a limit of the macro language's SearchString() command. While longer items can be written and stored into the macro's Array[] command, only the first 79 characters/spaces of a matching item will be processed (in this example: italicized) in the document.

•  Note that the macro searches for items that match the item's case.Thus, "Agreement" will be processed, but not "agreement." If you need both upper- and lower-case versions of a particular word italicized, enter both versions in the Array command (each version on its own line).You can change this in the SearchCaseSensitive() command in the ForEach() loop. You can also change the SearchFindWholeWordsOnly() command to process partial words.

•  Other font attributes or relative sizes can be used besides italics; see the variable (vAttr) at the top of the macro's code. Use a number that corresponds to the attribute (e.g., 8=Italics).

•  Once the words are processed, you can instantly delete the attributes, add additional attributes, or change attributes with Ron Hirsch's REPLACE CODES macro here.

•  Always test the effects of the macro on a copy of your document to see how it works.

EXAMPLE #1
A macro to add an attribute (here, italics) to various words using an external source list of words.

[Be sure to see Instructions, Notes and Tips in paragraphs just above.]


// Macro begins here:
// Purpose: Add an attribute (here, italics) to all words found
// in a source list of words (a plain text ANSI or ASCII file),
// wherever they are found in the current document.

// Set the drive, path and filename of the external "source list" (text file) -
vFN:="
c:\temp\sourcewords.txt"

// Set the type of attribute to apply -- for example:
// 0=ExtraLarge; 1=VeryLarge; 2=Large; 3=Small; 4=Fine;
// 5=Superscript; 6=Subscript; 7=Outline; 8=Italics; 9=Shadow;
// 10=Redline; 11=DoubleUnderline; 12=Bold; 13=Strikeout;
// 14=Underline; 15=SmallCaps
vAttr:=
8

FileExists(x;vFN)
If(x=False)
    Messagebox(;"Error - File not found!";vFN+NToC(0F90Ah)+"cannot be found.")
Quit
Endif

pResetFindReplace ()

// This snippet was posted by Kenneth Hobson on WordPerfect Universe at
// http://www.wpuniverse.com/vb/showthread.php?postid=146096#post146096

Array[]=FileToArray(vFN)

// This snippet is modeled after code posted by Greg Turner -

ForEach(vVar;Array[])
    vLen:=StrLen(vVar)
    If(vLen>79)
        Messagebox(;"Caution";
        "The item"+NToC(0F90Ah)+vVar+NToC(0F90Ah)+
        "is too long and will be skipped.")
        Go(Cont@)
    Endif
    PosDocVeryTop()
    SearchString (StrgToLookFor: vVar)
    ReplaceString (RplcStrg: vVar)
    ReplaceWithAttributes (State: Yes!; Attribute: vAttr)
    SearchCaseSensitive (State: Yes!) // (or use No!)
    SearchFindWholeWordsOnly (State: Yes!) // (or use No!)
    ReplaceForward (SearchMode: Extended!)
    Label(Cont@)
Endfor

// Exit macro here -
pResetFindReplace ()
Return // (exit here)

// PROCEDURES AND FUNCTIONS HERE:

// Function by Kenneth Hobson -
//Read File into array and return array -
Function FileToArray(vFile)
hFile=OpenFile (vFile; Read!; Compatibility!; AnsiText!)
If (hFile=OpenFile.Error!)
MessageBox (;"Open File Problem";
    "Error processing password '^0' file!"; IconStop!+HasParameters!; vFile)
Quit
EndIf
Count=0
While (not FileIsEOF (hFile))
Count = Count+1
FileRead (hFile; x)
EndWhile
FilePosition(hFile; 0; FromBeginning!)
Declare TempArray[Count]
x=0
While (not FileIsEOF (hFile))
x=x+1
FileRead (hFile; TempArray[x])
EndWhile
CloseFile (hFile)
Return(TempArray[])
EndFunc

Procedure pResetFindReplace ()
SearchString ("")
ReplaceString ("")
// Only one of these 4 Match commands can be active at any one time:
// MatchPositionAfter ()
// MatchExtendSelection()
// MatchPositionBefore()
MatchSelection()
SearchFindWholeWordsOnly (No!)
MatchWithAttributes (No!)
ReplaceWithAttributes (No!)
SearchCaseSensitive (No!)
ReplaceWithCase (No!)
MatchWithFont (No!)
ReplaceWithFont (No!)
MatchWithFontSize (No!)
ReplaceWithFontSize (No!)
SearchInSelection (No!)
SearchWordForms (No!)
SearchWrap (No!)
MatchLimit (No!)
EndProcedure

// Macro ends here



Footnote 2

Purposes (2 examples)

EXAMPLE #2 below is a simple version (after a macro created by Greg Turner) of the macro in Footnote 1 for those who want to write a code snippet "on the fly" to do a simple job: Add an attribute (e.g., italics) to several different words wherever they are found in a document. (See Tips below for other attributes.)

EXAMPLE #3 below uses a different Search technique (a While...Endwhile loop) so as to apply multiple changes/attributes to the various words it finds.

Instructions

•  To copy the macro code into WordPerfect, see here for instructions.

•  Enter each word you want to process in your current document on a separate line in the Array[] command below. With each word/phrase on its own line in the Array[] command, the list can be selected and sorted alphabetically, if desired, to make it easier to maintain and update. [Tip: For a macro that can do the same job as REPLACEM above -- but with a find/replace list created inside the macro code rather than in a separate document -- see Footnote 3.]

•  Be sure to replace the sample 6 words in dark red ("Agreement," etc.) inside the Array[] with those words you need to process.

•  Be sure the last item does not end with a semicolon (see the example in the macro below). If it does, the macro will pop up an error message.

Notes

•  Keep all words and phrases in the source word file to less than 79 characters per item, including spaces. This is a limit of the macro language's SearchString() command. While longer items can be written and stored into the macro's Array[] command, only the first 79 characters/spaces of a matching item will be processed (in this example: italicized) in the document.

•  If you need both upper- and lower-case versions of a particular word italicized, enter both versions in the Array command (each version on its own line). If you need both upper- and lower-case versions of a particular word italicized, enter both versions in the Array command (each version on its own line).You can change this in the SearchCaseSensitive() command in the ForEach() loop. You can also change the SearchFindWholeWordsOnly() command to process partial words.

•  There may be a limit to the number of items you can directly assign to an Array[]. It appears to be about 247 items, as noted here.

Tips

☼  Always test the effects of the macro on a copy of your document (or a section of your document) to see how it works.

☼  You can modify the macro (in Example #2) to produce other attributes (bold, large, small caps, etc.) Simply replace the parameter, Italics!) in the ReplaceWithAttributes command with any of the following: Bold!, Underline!, DoubleUnderline!, ExtraLarge, VeryLarge!, Large!, Small!, Fine!, SmallCaps!, Redline!, Subscript!, or Superscript!. [However, UPPPER and lower case are not "attributes" (to a computer they are changes in characters) so they require special treatment (see EXAMPLE #3).]

☼  Once the words are processed, you can instantly delete attributes, add additional attributes, or change attributes with Ron Hirsch's REPLACE CODES macro here.

EXAMPLE #2
A macro to add italics to various items, using a list embedded in the macro.
[Be sure to see Instructions, Notes, and Tips just above.]

Note that Search parameters are reset just before the macro exits; however, you could also use a Procedure such as in Example #3 below.

Note also the Procedure that will exit substructures (e.g., header, footnote, text box, etc.) when the cursor might be inside one.


// Macro begins here:
//

// Purpose: Adds an attribute (here, italics) to all words found in a
// source list of words embedded in the macro itself (in the Array command),
// wherever they exist in the document.
//

// The macro searches only for whole words
("Agreement" and not "agree")
// and for case-sensitive items ("Agreement" and not "agreement").

// Be sure the last item does NOT end with a semicolon:
Array[]={
"Agreement";
"Assignment";
"Leased Premises";
"Master Lease";
"Transferor";
"Transferee"

}

ForEach (vVar;Array[])
    PosDocVeryTop()
    SearchString (StrgToLookFor: vVar)
    ReplaceString (RplcStrg: vVar)
    ReplaceWithAttributes (State: Yes!; Attribute: Italics!)
    SearchFindWholeWordsOnly(Yes!) // (or use No!)
    SearchCaseSensitive (State: Yes!) // (or use No!)
    ReplaceForward (SearchMode: Extended!)
   
pExitSubstructures ()
Endfor

SearchFindWholeWordsOnly(No!)  // (restore default)
SearchCaseSensitive(No!)  // (restore default)
Return // (exit macro here)

Procedure pExitSubstructures ()
// Exit from any header, footer, footnote, endnote,
// text box, graphic caption, etc.:
While (?Substructure)
    vSubDoc:=?CurrentSubDoc
    SubstructureExit
    If ((vSubDoc=10) or (vSubDoc=11))    
        BoxEnd (Save!)
    EndIf
EndWhile
vBoxType:=?BoxContentType
If (vBoxType=1 or vBoxType=3) BoxEnd (Save!) Endif
EndProc

// Macro ends here

EXAMPLE #3  [UPDATED 8/26/2013:]
A macro to convert various words to
(1) UPPER case,
(2) Redline, and
(3) Underline using a list embedded in the macro.

[Be sure to see Instructions, Notes, and Tips above.]

The main difference between this macro and the previous one is that it is not limited by the ReplaceWithAttributes() command, which can be used only once in a given Replace routine.

Instead, the macro uses a While...Endwhile loop to search and replace each item individually, one at a time, wherever it might be found in the document. By doing that, it can then apply multiple attributes and/or text conversions to each item (here, CAPS/redline/underline).

Note there are also two Procedures used in the macro to help make it more reliable: It will exit substructures (e.g., header, footnote, text box, etc.) when the cursor might be inside one, and it will reset find and replace parameters (explained here).


// Macro begins here:

// Purpose:
// Finds (and selects) the items listed in the Array[] and
// (1) converts them to all UPPER case, then
// (2) adds Redline and underline to them.
// The macro searches only for whole words ("Agreement" and not "agree")
// and  for case-sensitive items ("Agreement" and not "agreement").
// For both upper and lower case for a given item, add both versions to the Array.
// [See the two Search commands just prior to the ForEach loop below.]

// Tips:
// To replace the items with lower case or Initial Caps items:
// Change the
ConvertUpperCase command to ConvertLowerCase or ConvertCaseInitialCaps
// [See your WP's Help> Macro Commands for the other commands you can use, especially
// in place of (or in addition to) the lines shown in blue.]

// First, call two procedures:
pExitSubstructures ()
pResetFindReplace ()

// Define the words and phrases (< 79-character limit for each item) to process.
// [be sure the last Array item does NOT end with a semicolon]
Array[]={
"Agreement";
"Assignment";
"Leased Premises";
"Master Lease";
"Transferor";
"Transferee"

}

OnNotFound(NextOne@)
MatchSelection
SearchFindWholeWordsOnly(Yes!)
// (or use No!)
SearchCaseSensitive (State: Yes!) // (or use No!)
ForEach (vVar;Array[])
    PosDocVeryTop
    While(True)  // (endless loop until not found)
        SearchString (StrgToLookFor: vVar)
        SearchNext (SearchMode: Extended!)
        ConvertCaseUppercase
        AttributeAppearanceToggle(Redline!)
        AttributeAppearanceToggle(Underline!)
        SelectMode(Off!)
        pExitSubstructures ()
    Endwhile
    Label(NextOne@)
Endfor

// Exit here:
pResetFindReplace ()    // (restore F&R defaults)
Return

// Procedures called by the above code:

Procedure pExitSubstructures ()
// Exit from any header, footer, footnote, endnote,
// text box, graphic caption, etc.:
While (?Substructure)
    vSubDoc:=?CurrentSubDoc
    SubstructureExit
    If ((vSubDoc=10) or (vSubDoc=11))    
        BoxEnd (Save!)
    EndIf
EndWhile
vBoxType:=?BoxContentType
If (vBoxType=1 or vBoxType=3) BoxEnd (Save!) Endif
EndProc

Procedure pResetFindReplace ()
SearchString("")  ReplaceString("")
// Only one Match* command can be active at any one time:
// MatchPositionAfter()
// MatchExtendSelection()
// MatchPositionBefore()
MatchSelection()
SearchFindWholeWordsOnly(No!)  MatchWithAttributes(No!)
ReplaceWithAttributes(No!)  SearchCaseSensitive(No!)
ReplaceWithCase(No!)  MatchWithFont(No!)
ReplaceWithFont(No!)  MatchWithFontSize(No!)
ReplaceWithFontSize(No!)  SearchInSelection(No!)
SearchWordForms(No!)  SearchWrap(No!)  MatchLimit(No!)
EndProcedure

// Macro ends here



Footnote 3

Purpose

Here is a macro (EXAMPLE #4 below) posted by Roy "lemoto" Lewis on WordPerfect Universe (4/16/08, and modified slightly here) that finds each word/phrase in a list and replaces it with another word/phrase. For example:

...replace "first" with "second"
...replace "his" with "her"
...replace "this" with "that"

Instructions

•  To copy the macro code into WordPerfect, see here for instructions.

•  Enter each pair of words or other items you want to process in your document on a separate line in the Array[] command below. With each pair of items on its own line in the Array[] command, the list can be selected and sorted alphabetically, if desired, to make it easier to maintain and update.

•  Be sure to replace the sample pairs of items in dark red ({"first; second"}, etc.) inside the Array[] with those items you need to process.

•  Be sure the last item does not end with a semicolon (see the example in the macro below). If it does, the macro will pop up an error message.

Notes and Tips

•  Unlike REPLACEM above, this macro does not use a separate list (external file); rather, it uses a simple 2-dimension array to store the find/replace pairs. Therefore, you need to edit the macro and change the array to list your particular items. In other words, be sure to REPLACE the sample 3 pairs (in dark red) inside the Array[] with those words you need to process. Also be sure the last item does NOT end with a semicolon.

[As a matter of interest to those curious about the third parameter in the ForNext command below (i.e., Array[0]/2 ), this simply takes the number of items (elements) in the array (which is stored in [0]) and divides it by 2 to determine the total number of word pairs to process. The ForNext loop then executes that particular number of times. In the example below, with six items in the array, ForNext executes three times, one time for each pair of items.]

•  Keep all words and phrases in the source word file to less than 79 characters per item, including spaces. (An "item" in this context is an element in the array; here, there are two elements per find/replace operation.) This is a limit of the macro language's SearchString() command. While longer items can be written and stored into the macro's Array[] command, only the first 79 characters/spaces of a matching item will be processed.

•  There may be a limit to the number of items you can directly assign to an Array[]. It appears to be about 247 items, as noted here.

•  The macro processes the document from the current cursor location onward by setting a simple bookmark at that location and returning to it later. You could remove the four existing (dark blue) Bookmark commands and add the command, PosDocVeryTop, at both the beginning of the macro and again just after the ReplaceForward command. [For a sample macro that processes just within a block of pre-selected text, see Footnote 4 below.]

•  As written, the macro will ignore headers, footers, footnotes, endnotes, text boxes, and other "substructures." But these could be selected individually before playing the macro on each of them.

•  Finally, note the three search qualifiers that precede the actual search and replace commands:

SearchCaseSensitive (Yes!)
ReplaceWithCase(Yes!)
SearchFindWholeWordsOnly (Yes!)

[You may want to remove one or more of these to suit your own find/replace requirements. The default values are set at the top of the code with a Procedure.]

•  Always test the effects of the macro on a copy of your document to see how it works.

Alternate (optional) purpose (updated 8/21/14):

You can use the macro to replace each specified string with the same string preceded by a unique character not used elsewhere in the document (such as the heart symbol (♡)). For example,

...replace "first" with "first"
...replace "his" with "
his"
...replace "this" with "
this"

This would allow you to (later) search for each item in the order of appearance in the document by simply using Find and Replace (or a macro Search) to locate each symbol, one after the other. (All such symbols can then be deleted at once with Find and Replace.) For an example, see this post on WordPerfect Universe (which also searches for tabs, spaces, and double quotation marks using the NToC() command).

EXAMPLE #4  
[Be sure to see Instructions, Notes and Tips just
above.]

// Macro begins here:

pResetFindReplace ()

Prompt ("Replacing";"Please wait"; NoButtons!| InformationIcon!)

/* IMPORTANT:
Enter your own find-and-replace pairs below, following the pattern of the first 3 items;
note that the last item does not terminate with a semicolon, just a right brace;
note that each item (element) in a pair should not exceed 79 characters (including spaces);
when finished entering your own list, delete the first 3 "
sample" items.

Note also that ReplaceForward(Regular!) does not replace inside headers, footers, watermarks,
footnotes, endnotes, text boxes. To replace in them, use ReplaceForward(Extended!).
*/

// Be sure the last item-pair does NOT end with a semicolon:
Array[]={
{"first";"second"};
{"his";"her"};
{"this";"that"}

}

SearchCaseSensitive (Yes!) // (or use No!)
ReplaceWithCase(Yes!) // (or use No!)
SearchFindWholeWordsOnly (Yes!) // (or use No!)
BookmarkCreate("cursor_location") // (see Notes above)

ForNext(i;1;Array[0]/2)
    PosDocVeryTop
    SearchString (Array[i;1]) // (search for the first element in the pair)
    ReplaceString (Array[i;2]) // (replace with the second element in the pair)
    ReplaceForward (Regular!)
    BookmarkFind("cursor_location") // (see Notes above)
EndFor

// Return Find and Replace parameters to defaults:
pResetFindReplace ()

// Return to original cursor location:
BookmarkFind("cursor_location") // (see Notes above)
BookmarkDelete("cursor_location") // (see Notes above)

EndPrompt

// Exit macro here:
Return

Procedure pResetFindReplace ()
SearchString ("")
ReplaceString ("")
// Only one of these 4 Match commands can be active at any one time:
// MatchPositionAfter ()
// MatchExtendSelection()
// MatchPositionBefore()
MatchSelection()
SearchFindWholeWordsOnly (No!)
MatchWithAttributes (No!)
ReplaceWithAttributes (No!)
SearchCaseSensitive (No!)
ReplaceWithCase (No!)
MatchWithFont (No!)
ReplaceWithFont (No!)
MatchWithFontSize (No!)
ReplaceWithFontSize (No!)
SearchInSelection (No!)
SearchWordForms (No!)
SearchWrap (No!)
MatchLimit (No!)
EndProcedure

// Macro ends



Footnote 4

Purpose

EXAMPLE #5 below is a variation of EXAMPLE #4 in Footnote 3. However, instead of processing the document from the cursor location onward it processes only within a block of pre-selected text. 

[CAUTION: The macro uses the BookmarkBlock command, which can sometimes move or delete existing bookmarks when it re-selects text that includes such bookmarks. Hence it is a good idea to always use it on a backed up copy of the document. If you don't have bookmarks in the selected text then it should not present a problem.]

Instructions

•  To copy the macro code into WordPerfect, see here for instructions.

•  Enter each pair of words or other items you want to process in your document on a separate line in the Array[] command below. With each pair of items on its own line in the Array[] command, the list can be selected and sorted alphabetically, if desired, to make it easier to maintain and update.

•  Be sure to replace the sample pairs of items in dark red ({"first; second"}, etc.) inside the Array[] with those items you need to process.

•  Be sure the last item does not end with a semicolon (see the example in the macro below). If it does, the macro will pop up an error message.

Notes and Tips

•  Unlike REPLACEM above, this macro does not use a separate list (external file); rather, it uses a simple 2-dimension array to store the find/replace pairs. Therefore, you need to edit the macro and change the array to list your particular items. In other words, be sure to REPLACE the sample 3 pairs (in dark red) inside the Array[] with those words you need to process. Also be sure the last item does NOT end with a semicolon.

[As a matter of interest to those curious about the third parameter in the ForNext command below (i.e., Array[0]/2 ), this simply takes the number of items (elements) in the array (which is stored in [0]) and divides it by 2 to determine the total number of word pairs to process. The ForNext loop then executes that particular number of times. In the example below, with six items in the array, ForNext executes three times, one time for each pair of items.]

•  Keep all words and phrases in the source word file to less than 79 characters per item, including spaces. (An "item" in this context is an element in the array; here, there are two elements per find/replace operation.) This is a limit of the macro language's SearchString() command. While longer items can be written and stored into the macro's Array[] command, only the first 79 characters/spaces of a matching item will be processed.

•  There may be a limit to the number of items you can directly assign to an Array[]. It appears to be about 247 items, as noted here.

•  The macro processes all items found in the currently selected text by setting simple paired bookmark commands (shown in blue) to allow the macro to easily re-select the selection with each pass. [For a sample macro that processes from the current cursor location to the end of the document, see Footnote 3 above.] 

•  As written, the macro will ignore headers, footers, footnotes, endnotes, text boxes, and other "substructures." But these could be selected individually before playing the macro on each of them.

•  Finally, note these three search qualifiers that precede the actual search and replace commands:

SearchCaseSensitive (Yes!)
ReplaceWithCase(Yes!)
SearchFindWholeWordsOnly (Yes!)

[You may want to remove one or more of these to suit your own find/replace requirements. The default values are set at the top of the code with a Procedure.]

•  Always test the effects of the macro on a copy of your document to see how it works.

EXAMPLE #5 
A variation of EXAMPLE #4 that
processes only within a block of pre-selected text
[See Instructions, Notes and Tips just above.]


// Macro begins here:

pResetFindReplace ()

// Create a paired bookmark around the selection of text, then select it again:
BookmarkCreate("current_selection")
BookmarkBlock("current_selection")

/* IMPORTANT:
Enter your own find-and-replace pairs below, following the pattern of the first 3 items;
note that the last item does not terminate with a semicolon, just a right brace;
note that each item (element) in a pair should not exceed 79 characters (including spaces);
when finished entering your own list, delete the first 3 "
sample" items.

Note also that ReplaceForward(Regular!) does not search inside headers, footers, watermarks,
footnotes, endnotes, text boxes. To search in them, use ReplaceForward(Extended!).
*/

// Be sure the last item-pair does NOT end with a semicolon:
Array[]={
{"first";"second"};
{"his";"her"};
{"this";"that"}

}

SearchCaseSensitive (Yes!)  // (optional: See Notes above)
ReplaceWithCase(Yes!)
 // (optional: See Notes above)
SearchFindWholeWordsOnly (Yes!)  
 // (optional: See Notes above)
SearchInSelection(Yes!)  // (required: search only inside currently selected block)

ForNext(i;1;Array[0]/2)
    PosBlockTop  // (not really needed here, but useful with some other ForNext loops)
    SearchString (Array[i;1]) // (search for the first element in the pair)
    ReplaceString (Array[i;2]) // (replace with the second element in the pair)
    ReplaceForward (Regular!)  // (execute the replacements)
    BookmarkBlock("current_selection") // (re-select the selection)
EndFor

// Return Find and Replace parameters to defaults:
pResetFindReplace ()

// The next 2 commands to position curosr and remove select mode are OPTIONAL:
PosBlockBottom
SelectMode(Off!)

// Clean up: Remove bookmark code:
BookmarkDelete("current_selection")

// Exit macro here:
Return

Procedure pResetFindReplace ()
SearchString ("")
ReplaceString ("")
// Only one of these 4 Match commands can be active at any one time:
// MatchPositionAfter ()
// MatchExtendSelection()
// MatchPositionBefore()
MatchSelection()
SearchFindWholeWordsOnly (No!)
MatchWithAttributes (No!)
ReplaceWithAttributes (No!)
SearchCaseSensitive (No!)
ReplaceWithCase (No!)
MatchWithFont (No!)
ReplaceWithFont (No!)
MatchWithFontSize (No!)
ReplaceWithFontSize (No!)
SearchInSelection (No!)
SearchWordForms (No!)
SearchWrap (No!)
MatchLimit (No!)
EndProcedure

// Macro ends here