"Ads by Goooooogle" 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 Apr 16, 2008
REPLACEM - Replace multiple words in one pass

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

Compatible with WordPerfect versions 6,78,9,10,11,12,X3

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

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 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 be in a document:

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

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. [To copy its code into WordPerfect, see here for instructions.]

Please note:

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

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

3. 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).

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

// Macro begins here -

// Set the drive, path and filename of the external "source" 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.

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.

NOTE that 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). Also note that you can search for words using the Case Sensitive option by enabling that command below by removing the pair of slash (//) marks.

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.

// Macro begins

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!)
ReplaceForward (SearchMode: Extended!)
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"

Notes

1. 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 if you need to change the list items. Be sure to REPLACE the sample 3 pairs (in red) inside the Array[] with those words you need to process. Be sure the last item does NOT end with a semicolon.

2. Keep all words and phrases in the source word file to less than 79 characters per item, including spaces. (An item is an element in the array; here, there are two items 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.

3. 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 red) commands and add the command, PosDocVeryTop, at both the beginning of the macro and again just after the ReplaceForward command.

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

 

// Macro begins

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 in a pair should not exceed 79 characters (including spaces);
when finished, delete the first 3 "sample" items:
*/

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

}

SearchCaseSensitive (Yes!)
ReplaceWithCase(Yes!)
SearchFindWholeWordsOnly (Yes!)
BookmarkCreate("cursor_location")
ForNext(i;1;Array[0]/2)
// I leave the following four lines in (but disabled) just as a "this sort of thing can be done"
// SearchString ("The "+Array[i;1])
// ReplaceString (Array[i;1])
// ReplaceForward (Regular!)
// BookmarkFind("qza")
SearchString (Array[i;1])
ReplaceString (Array[i;2])
ReplaceForward (Regular!)
BookmarkFind("cursor_location")
EndFor

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

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

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