FoxPro TextMerge

You can use the Copy To command to create a simple text file from a table with various forms of delimiter between fields. The TextMerge commands let you create a more complex text file. The contents can be a mixture of plain text, fields and variables, and FoxPro functions.

The commands are:

Set Textmerge On

Tell FoxPro to evaluate all the text it sees between the double chevron delimiters << and >>. If necessary, these delimiters can be changed with the Set Delimiters To command.

Set Textmerge To <filename> Noshow

Direct the output to a file. The file will be created if it does not already exist. If the file does exist and Safety is On then you will be asked whether you want to overwrite it. The Noshow clause suppresses output to the screen and is useful if this command is being used in an application.

\xyz

Send the characters 'xyz' to the file. The characters are preceded by a carriage return/linefeed combination. There is no need for quotes around the characters. If you put quotes around the characters then the quotes will be output as well.

\\xyz

Send the characters 'xyz' to the file on the current line.

Set TextMerge To

Close the output file.

Set Textmerge Off

Stop evaluating expressions within << and >>.

<<foxpro expression>>

Evaluate the FoxPro expression. The expression can be the name of a field or memory variable or a FoxPro function. The example below shows how you can mix <<foxpro expression>> with plain text on the same line.

Text Merge Example

This example produces an HTML page holding a simple list of customers:

Use Customer

*-- Start the merge process
*-- Use the noshow option so that no output
*-- shows on screen
Set Textmerge On
Set Textmerge To customer.htm noshow

*-- Just a minimal HTML header for the document
\<html>
\<head>
\<title>HTML from FoxPro</title>
\</head>

*-- Now for the body
\<body>

*-- Start with a level 1 heading and a line
\<h1>Customer List</h1>
\<hr />

*-- Output one line for each record in the table
Scan
   \Company name is <<Upper(Company)>>
   \<br />
Endscan

*-- Closing tags for the html page
\</body>
\</html>

*-- Close the text file and stop merging
Set Textmerge To
Set Textmerge Off
Use In Customer

This short program will produce a text file named 'customer.htm' which looks like this in a browser:

HTML from VFP merge commands

This is a very simple page but even so the mixture of TextMerge and HTML can be confusing to write. Both use the < and > symbols and it's easy to get confused. TextMerge is however a very powerful and flexible technique which lets you build a page which will display a mixture of text and data.

Visual FoxPro offers other ways of generating HTML. These are described on the HTML from FoxPro page.

Gotcha

There are two things to watch out for when using text merge and a third when using it to produce HTML:

  • Everything on the line after a \ or \\, including any trailing spaces, will be output.
  • The carriage return/linefeed comes before the characters are output.
  • Remember that a \\ produces a new line of output in the text of the HTML file. It will not produce a new line on the web page. You never really need to use a \\ when generating HTML because browsers ignore new lines in the HTML source.

Scope

The SCAN command can take a scope clause if you need to produce a file based on a particular set of records. More details on the scope of Visual FoxPro commands here.

MS Access technical tips

Visual FoxPro technical tips

General Tips

 

More tips from Alvechurch Data

Generating HTML web pages from FoxPro

FoxPro commands and functions for generating HTML documents

Read More

IntelliSense in Visual Foxpro

Using and extending FoxPro Intellisense

Read More

Copying data to CSV and other text files from FoxPro

FoxPro commands to copy data to comma separated and other types of delimited text files

Read More

NotePad++

Using NotePad++ to create a simple HTML page

Read More

NotePad++ and FireFox

Using NotePad++ and FireFox together

Read More