Tech Tip: Creating Write Pro Documents From Text
PRODUCT: 4D | VERSION: 18 | PLATFORM: Mac & Win
Published On: May 3, 2021
A Write Pro document can be initialize using the WP New command. A source parameter can also be passed to the WP New command to create a new document with content based on the source. The source can be one of three types:
- A BLOB containing a 4D Write or 4D Write Pro document file.
- An Object of a 4D Write Pro document, range, or element.
- A string of an HTML source.
When using a string of an HTML source, if the data is not properly formatted as HTML the command can throw an error.
For example the following code will generate a 106 Error:
C_OBJECT($WP_obj) $WP_obj:=WP New("&") |
This is because many symbols, including the ampersand, need to be encoded.
Below is a utility method that will automatically format a text so that symbols are HTML encoded and returns a WP document containing the text:
// Util_Text_To_WPDoc C_TEXT($1; $input_t) C_TEXT($tag_t) C_TEXT($stmt_t) C_TEXT($result_t) If (Count parameters=1) $input_t:=$1 $tag_t:="<!--#4DTEXT \"\"-->" $stmt_t:=Insert string($tag_t; $input_t; 14) PROCESS 4D TAGS($stmt_t; $result_t) $0:=WP New($result_t) End if |
Below is an example of using the method with the initial example that failed:
C_OBJECT($WP_obj) $WP_obj:=Util_Text_To_WPDoc("&") |