| February 1998: Working
with Files When you use macros to help automate your work in
WordPerfect, you often need to manipulate files in some way. You'll need to create files,
open and modify existing files, save files, and sometimes delete files. This month we'll
talk about some macro routines that can be used to do some of the common file handling
chores.
Store filenames in variables
You should always store your filenames in variables, then use the variable name
throughout the macro when you need to manipulate that file. This lets you avoid having to
do time-consuming searches to change all occurences of a filename throughout your macro
when the filename or directory changes.
Assign variables such as filenames at the top of the macro. They will always be easy to
find and change when needed. I also color-code values such as filenames to make it easy to
find them, and to differentiate the names from other macro code. For example,
Application(A1; "WordPerfect"; default; "US")
// Set variables
vFormFile:=?PathDocument+"myform.frm"
vDataFile:=?PathDocument+"mydata.dat"
Checking if you have an available document window
If your macro will be opening files from the hard drive, it is a good idea to first
check to see if a document window is available. WordPerfect allows you to have up to nine
documents open at one time. In rare occurrences, there may not be an available window when
you attempt to open a file, and an error will occur. So, if your macro needs to have five
files open at the same time, there will need to be at least five available document
windows.
To check whether a document window is available, use the ?NumberOpenDocuments
system variable. In the example below, we check to make sure there is at least one
document window available. If not, a message instructs the user to close at least one,
then start over. Adjust the number of documents being tested to accommodate the number of
documents your macro needs to use.
If(?NumberOpenDocuments > 8)
MessageBox(;"Error"; "Please close at least one open document, then run the macro again.")
Quit
Endif
Switching to the correct document
In some macros you may have two documents open at the same time, and be moving
information from one document to another. To make sure you switch back and forth to the
correct document, use the ?DocNumber system variable to get the
document number of each document, and the SwitchDoc command to
move back and forth between them. This is more accurate than using a command such as WindowNext or WindowPrevious, which will
take you to the next window, but not necessary the correct document. For example,
FileOpen(vDoc1)
vDocNum1:=?DocNumber
FileOpen(vDoc2)
vDocNum2:=?DocNumber
// more commands in between
SwitchDoc(vDocNum1)
// More Commands
SwitchDoc(vDocNum2)
Opening a file in the default directory
Your macro may need to open a file that users store in their default document
directory. But the name of the default directory may be different for each user. You can
make sure the macro always finds the file by using the ?PathDocument
system variable.
vFilename:=?PathDocument+"myfile.wpd"
FileOpen(vFilename)
There are several other system variables that let your macro find the desired path automatically. To list a few,
?PathCurrent
?PathGraphics
?PathDatabase
?PathSpreadsheet
?PathMacros
Opening a file in a non-default directory
If your macro needs to open a file in a directory other than one that has a system
variable, you'll need to specify the exact directory and filename. Here are three ways to
do that:
Hardcode the filename
FileOpen("c:\groupfiles\forms\myform.wpd")
Assign the complete filename to a variable, then use it later
vFilename:="c:\groupfiles\forms\myform.wpd"
FileOpen(vFilename)
Assign separate variables for the path and filename, then
concatenate them
vPath:="c:\groupfiles\forms\"
vFile:="myform.wpd"
FileOpen(vPath+vFile)
Each of the three examples above do the exact same thing. When using any of these
methods, you need to make sure you get all the parts of the path and filename correct,
including the backslashes.
Combining two documents
At some point you may want to copy all the information from one file into a different
file. There are a couple of ways to do this.
Inserting a file from the hard disk (disk to screen)
To insert a second document into a document that you already have on the screen,
position the cursor where you want the second document to be inserted, then use the FileInsert command.
PosDocBottom()
HardPageBreak
FileInsert (Filename:vFilename; AutoDetect:Yes!; InsertIntoDoc:Insert!)
Appending to a file on the hard disk (screen to disk)
You can copy the file on the screen into a document stored on the hard drive with the AppendToFile command. The text will be copied to the bottom of the
file on disk. Here's an example:
SelectAll()
AppendToFile(vFilename)

For more information on commands that manipulate files, see the WordPerfect Macro
Online Help.
For information on ordering my book see my
web page.
© Copyright 1998 by J. Jeppson. |