|
|||||||||||||
|
February 1999: Making Macros Easy to Maintain
I often receive requests from clients to convert an older macro to WPWin 7 or 8. Use Variables for Paths and FilenameWhen the macro will be accessing specific files or directories a lot, create variables I recommend making a PROCEDURE that all the path and filename declarations will You can use this technique for any data that will be needed throughout the macro, or Variables are local to the routine where they are created so you'll need to either make Here's an example of using a PROCEDURE and Global variables:
Application(A1; "WordPerfect"; default; "EN")
// main
SetFilePaths()
Type(vMainDocPath)
HardReturn
// end main
//**********************************************************************
//* Routine Name: SetFilePaths
//* Input Variables: none
//* Return Variable: None
//* Description: Sets global var for paths/filenames to be used in the macro
//**********************************************************************
PROCEDURE SetFilePaths()
GLOBAL (vMainDocPath; vClient1Path; vClient2Path;vClientIndexFile)
vMainDocPath:="c:\workfiles\"
vClient1Path:=vMainDocPath+"Client1\"
vClient2Path:=vMainDocPath+"Client2\"
vClientIndexFile:=vmainDocPath+"index\clientindex.wpd")
ENDPROC
If you prefer not to use global variables (and I recommend avoiding Globals when Remember that variables are normally local to the routine they are assigned in. When Generally, it is a good idea to assign some value (even if just a "") to the
variables first,
Application(A1; "WordPerfect"; default; "EN")
vMainDocPath:="" // set the values to "" first
vClient1Path:=""
vClient2Path:=""
vClientIndexFile:=""
// pass the variable names in by reference
SetFilePaths(&vMainDocPath; &vClient1Path; &vClient2Path; &vClientIndexFile)
// the variables now exist at this level of the macro
//**********************************************************************
//* Routine Name: SetFilePaths
//* Input Variables: Variable names to pass the values back in by reference
//* Return Variable: None
//* Description: Sets global var for paths/filenames to be used in the macro
//**********************************************************************
PROCEDURE SetFilePaths(&vMainDocPath; &vClient1Path; &vClient2Path; &vClientIndexFile)
vMainDocPath:="c:\workfiles\"
vClient1Path:=vMainDocPath+"Client1\"
vClient2Path:=vMainDocPath+"Client2\"
vClientIndexFile:=vmainDocPath+"index\clientindex.wpd")
ENDPROC
Break tasks down into their smallest logical dutiesYou should always write your code as a series of small tasks. As you plan your
This high level plan can become the basis of breaking your code into smaller tasks. If a task will vary periodically (for example, the filename or the sort keys may
change), In the following macro, there are three separate tasks: Set Paths, Open File, and Sort
file. I then call the routine that opens a document, then the routine that sorts the
document.
Application(A1; "WordPerfect"; default; "EN")
vMainDocPath:="" // set the values to "" first
vClient1Path:=""
vClient2Path:=""
vClientIndexFile:=""
// main
SetFilePaths(&vMainDocPath; &vClient1Path; &vClient2Path; &vClientIndexFile)
OpenDoc(vClientIndexFile; TRUE)
SortDoc(-1; 1; 2; SortKeys.SortOrder.Ascending!)
//**********************************************************************
//* Routine Name: SetFilePaths
//* Input Variables: Variable names to pass the values back in by reference
//* Return Variable: None
//* Description: Sets global var for paths/filenames to be used in the macro
//**********************************************************************
PROCEDURE SetFilePaths(&vMainDocPath; &vClient1Path; &vClient2Path; &vClientIndexFile)
vMainDocPath:="c:\workfiles\"
vClient1Path:=vMainDocPath+"Client1\"
vClient2Path:=vMainDocPath+"Client2\"
vClientIndexFile:=vmainDocPath+"index\clientindex.wpd"
ENDPROC
//**********************************************************************
//* Routine Name: SortDoc
//* Input Variables: Variable names to pass the values back in by reference
//* Return Variable: None
//* Description: Sets global var for paths/filenames to be used in the macro
//**********************************************************************
PROCEDURE SortDoc (vFirstKey; vSecondKey; vThirdKey; vOrder)
SortKeys (
{Field: 1; Line: 1; Word: vFirstKey; SortType: Alphanumeric!; SortOrder: vOrder;
Field: 1; Line: 1; Word: vSecondKey; SortType: Alphanumeric!; SortOrder: vOrder;
Field: 1; Line: 1; Word: vThirdKey; SortType: Alphanumeric!; SortOrder: vOrder})
SortType (SortType: LineSort!)
SortAction (SortAction: Sort!)
SortUndo (State: NoUndo!)
SortCaseOrder (Case: LowercaseFirst!)
Sort ()
ENDPROC
//**********************************************************************
//* Routine Name: OpenDoc
//* Input Variables: vFilename: The name of the file to open
//* vInsert: TRUE = insert into current document
//* FALSE = open in new screen
//* Return Variable: None
//* Description: Opens the specified document
//**********************************************************************
PROCEDURE OpenDoc(vFilename; vInsert)
If(vInsert = TRUE)
FileInsert(vFilename; ; Insert!)
Else
FileOpen(vFilename)
Endif
ENDPROC
Comment ProfuselyThe best advise in making macros easy to maintain is to comment everything profusely. If you have to use confusing code, or do something out of the ordinary, you will //********************************************************************** //* Routine Name: name of the routine here //* Input Variables: name and description of paramters n //* vExample: TRUE = pass in a true //* FALSE = pass in a false //* Return Variable: What return value is passed back (for FUNCTIONS) //* Description: A description of what the routine accomplishes //********************************************************************** For more information on dialogs and DLL calls, get a copy of my book. For information on ordering my book see my web page. Copyright Notice: The information included in the PerfectScript Journal is
|