Page 1 of 1

Merging data table into form document

Posted: 2008-01-03 00:05:17
by Bob Stern
Since NWP lacks a Merge capability, I wrote this macro to merge data from a Data document into a Form document.

The Form document can have field names interspersed in the text. When you run the macro, it modifies the Form document by replacing each occurrence of a field name with the data value specified in a Data file.

The format of the Data file is a 2-column table with field names in the first column and data values in the second column.

You might want to save the Form document as a Nisus Template so you won't accidentally overwrite the original form when you perform the merge.

Here is the macro:

# Data table should have 2 columns:
# Field Name in 1st column. Data Value in 2nd column.

Prompt 'Data Table should be in front of Form document. No other window should be open. Otherwise, choose Stop.'

###

# Data table:

# Start with first Field Name cell:
Select All Document
Select Start
$KeepLooping = Select Table Cell 1, 1 # Returns True if selection succeeds.

While $KeepLooping

$field = Read Selection # Field name = first column.

# Select corresponding Data Value:
Select Relative Table Cell 0, 1
$data = Read Selection

Write Find Expression $field
Write Replace Expression $data
# Prompt "field = $field data = $data" # For debugging.

# Form document: Replace field name with data value:
Menu 'Exchange Front Windows'
Find and Replace $field, $data, '-iavW' # Case sensitive; All; Verbose; Wrap

# Data table: Select first column (field name) of next row:
Menu 'Exchange Front Windows'
$KeepLooping = Select Relative Table Cell 1, -1
# Returns False & exits loop if fails.
End