|
|
|
October 1999: Performing a Series of Search and Replace Functions in a Document
A common use for a macro is to perform a series of search and replace operations throughout a document to correct or change formatting or text. Macros are ideally suited to this sort of repetitious task. There are two good methods you can use within a macro, depending on the exact changes you are making.
Simple Search/ReplaceIf you need to do a series of simple Search and Replaces, you can use the code shown below.
To include additional search and replace operations, just copy one of the sets of search and replace commands, and replace the search and replace strings. You can include as many sets of commands as needed. I've used this method in a macro I created to clean up dialog code that is pasted from the Dialog Editor. I really don't like all the parameter names cluttering up my code, so I run this macro after pasting a dialog initially created in the DE. In all cases, the replace string is a blank string, so I use the FORNEXT command to simplify the multiple search and replaces I needed to do in this macro.
To run this macro, first paste your dialog code (see the June 1997 back issue if you don't know what I am talking about here). Next, select the dialog code that was just pasted, then run this macro. All the extraneous parameter names will be removed.
Complex Search and Text ManipulationIf you are using Search, then performing some other action, you macro code will be a bit more complex. Normally, if a search fails, the macro will simply terminate. PerfectScript includes commands to test for the Not Found condition, and we need to do checking for this condition for each search so that we can send execution to the next search.
The sample code below shows how to perform multiple searches in the same macro.
You can include as many searches as necessary in this macro by changing the value of vLastSearch, and including a Label with the appropriate search commands, and the commands for the other operations you want to perform.
Copyright Notice: The information included in the PerfectScript Journal is protected by US Copyright. The author grants you the right to use the routines in your own macros as needed. You may not sell, distribute, or publish them in any form.If you choose to use the information here, you do so entirely at your own risk. No representations are made regarding the fitness of this information for your particular purpose, or for your ability or inability to use the information. You are advised to make backups of all relevant files before implementing any suggestion or technique.© Copyright 1999 by J. Jeppson. |