"Ads by Google" links provided by Google.com, which has sole control over all advertising content it displays on this site.

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

Barry MacDonnell's
Toolbox for WordPerfect

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

Page updated Jul 20, 2008
REPLACEM - Replace multiple words in one pass

Download REPLACEM.ZIP (Version 1.2; 1/8/05; 17,297 bytes)

Compatible with WordPerfect 6 and later versions

Downloading, Documentation, Modifications, and Support

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.

Related macro: See footnote.

This is a macro that you can use to find and replace many words or phrases in the current document in one easy operation. 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. It 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.

Simply store your replacements in a 2-column table in a separate WordPerfect file that you create first. This "lookup table" makes it easy to update the word list as well as keep separate lists for different projects.

Be sure to read the brief instructions embedded at the top of the macro itself (just use WordPerfect to edit the macro).

Note that the words or phrases in column 1 of the lookup table are apparently limited by WordPerfect's own Find function to about 79 characters, but if they are found, they will be replaced by the word or phrase in column 2 of the same table row. (If there is a blank cell in column 2, the word or phrase in column 1 will be deleted in the main document -- the equivalent of manually using WordPerfect's Replace function with <nothing> entered in the Replace field.)

TIPS

  • If you want to swap words or phrases -- for example, "Party 1" and "Party 2" -- wherever they appear in your document, use the "A-B-C Word Triangle" method. 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(s) on the opposite corner (lower right, or corner "B"), and some sort of intermediate word(s) at the apex (or corner "C"). You 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 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"). Here's how:
  • First 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 replacing several words or phrases at once using a separate word list, replace the next word or phrase on your swap list with ABCD2, etc. In this step, you are replacing all "A" list words with "C" list words.
  • 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". In this step, you are changing all "B" list words into "A" list words.
  • Finally, replace the "C" list words (the nonsense words) with the corresponding "B" list words.
  • If you are using lists of words, you will need three lists 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." You would 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.
  • Advanced users: If you simply want to use a macro to quickly apply formatting to several different words in a document, see the macro in the footnote below.
Footnote 1

If you only need to apply formatting to several different words or phrases in a document -- wherever they are found -- you can use a macro like the one below (after a macro published by Greg Turner and Kenneth Hobson on WordPerfect Universe here).

Assume you wish to apply italics to these words that might appear anywhere in a document:

Agreement, Assignment, Leased Premises, Master Lease, Transferor, Transferee.

Instructions

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

(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 modify the macro code below (the items in red) to point to the text file and to tell the macro which attribute to apply. Then play the macro.

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.
  • Note that the macro searches for items that match the item's case. Thus, "Agreement" will be processed, but not "agreement." You can change this in the SearchCaseSensitive() command in the ForEach() loop.
  • 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.

Tips

  • To copy the macro code into WordPerfect, see here for instructions.
  • Always test the effects of the macro on a copy of your document (or a section of your document) to see how it works.

Example macro to add italics to various words using an external source list of words

// 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.

// See Instructions, Notes, and Tips above.

// 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 -
// 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!)
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

 

Footnote 2

Here is a simple version of the above macro (created by Greg Turner) 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.)

Instructions

  • Enter each word you want to process in your 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. [For a macro that can do the same job as REPLACEM above -- but with a find/replace list created inside the macro code -- see Footnote 3.]
  • Be sure to replace the sample 6 words ("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

  • The words (or phrases) should not be longer than 79 characters each, including spaces, but excluding the pair of double quote marks (which are required).
  • You can search for words using the Case Sensitive option by enabling that command below by removing the pair of slash (//) marks preceding the SearchCaseSensitive command. For example, if you enable that command and use the Capitalized version of of the word "Agreement" in the Array command, the macro will skip over any lover case version of the word (i.e., "agreement"). If you need both upper- and lower-case versions of a word italicized, enter both versions in the Array command (each version on its own line).

Tips

  • To copy the macro code into WordPerfect, see here for instructions.
  • 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 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," so they require special treatment; see the alternative macro below (but which does not process headers, footers, endnotes, or other "substructures").
  • Once the words are processed, you can instantly delete the attributes, add additional attributes, or change attri butes with Ron Hirsch's REPLACE CODES macro here.

Example macro to add italics to various words, using a list embedded in the macro

// Macro begins

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

// See Instructions, Notes, and Tips above.

Array[]={
"Agreement";
"Assignment";
"Leased Premises";
"Master Lease";
"Transferor";
"Transferee"

}

ForEach (vVar;Array[])
 PosDocVeryTop()
 SearchString (StrgToLookFor: vVar)
 ReplaceString (RplcStrg: vVar)
 ReplaceWithAttributes (State: Yes!; Attribute: Italics!)
 //SearchCaseSensitive (State: Yes!)  // (see Notes above)
 ReplaceForward (SearchMode: Extended!)
Endfor

Return // (exit here)

// Macro ends

Example macro to convert various words to UPPER case, using a list embedded in the macro

// Macro begins

// Purpose: Convert all words to UPPER case that are found
// in a source list of words embedded in the macro itself,
// wherever they exist in the body text area of the document.
// (Additional code would be required to process substructures
// such as headers, footers, footnotes, endnotes, and text boxes.)

// See Instructions, Notes, and Tips above.

// For lower case or Initial Caps:
// Replace the
ConvertUpperCase command to ConvertLowerCase or ConvertCaseInitialCaps

Array[]={
"Agreement";
"Assignment";
"Leased Premises";
"Master Lease";
"Transferor";
"Transferee"

}

OnNotFound(NextOne@)
MatchSelection
ForEach (vVar;Array[])
 PosDocVeryTop()
 SearchString (StrgToLookFor: vVar)
 SearchNext (SearchMode: Regular!)
 ConvertCaseUppercase
 SelectMode(Off!)
 Label(NextOne@)
 //SearchCaseSensitive (State: Yes!)  // (see Notes above)
Endfor

Return // (exit here)

// Macro ends

 

Footnote 3

Here is a macro 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

  • Enter each pair of words you want to process in your document on a separate line in the Array[] command below. With each pair of words/phrases 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 words ({"first; second"}, 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

  • 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.
  • 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.
  • 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.]

Tips

  • To copy the macro code into WordPerfect, see here for instructions.
  • Always test the effects of the macro on a copy of your document (or a section of your document) to see how it works.

Example macro

// Macro begins

// See Instructions, Notes, and Tips above.

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:
*/

Array[]={
{"first";"second"};
{"his";"her"};
{"this";"that"}

}

SearchCaseSensitive (Yes!)
ReplaceWithCase(Yes!)
SearchFindWholeWordsOnly (Yes!)
BookmarkCreate("cursor_location") // (see Notes above)
ForNext(i;1;Array[0]/2)
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

Return // (exit here)

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