Tech Tip: Utility Method to Replace Text from Write Pro Area
PRODUCT: 4D Write Pro | VERSION: 19 | PLATFORM: Mac & Win
Published On: April 10, 2023
The utility method wpReplaceText() is used to replace a text from the body of a Write Pro Area.
#DECLARE($param_o : Object) var $userSel : Object var $options : Integer var $target : Object var $ranges : Collection If (Count parameters=1) Case of : ($param_o.wpObject=Null) : ($param_o.wpAreaName=Null) : ($param_o.oldText=Null) : ($param_o.newText=Null) : ($param_o.onlyFirstInstance=Null) Else $target:=WP Get body($param_o.wpObject) $options:=wk case insensitive+wk diacritic insensitive $userSel:=WP Selection range(*; $param_o.wpAreaName) If ($param_o.onlyFirstInstance) // True replaces first instance var $start; $pos : Integer var $text : Text var $wpRange_o : Object $text:=WP Get text($param_o.wpObject) $wpRange_o:=WP Text range($userSel; wk start text; wk start text) $start:=$wpRange_o.end $pos:=Position($param_o.oldText; $text; $start) If ($pos>0) $wpRange_o:=WP Text range($userSel; $pos; $pos+Length($param_o.oldText)) WP SET TEXT($wpRange_o; $param_o.newText; wk replace) End if Else // False replaces all instances $ranges:=WP Find all($target; $param_o.oldText; $options; $param_o.newText) End if End case End if |
Example:
$param_o:=New object $param_o.wpObject:=WParea $param_o.wpAreaName:="WParea" $param_o.oldText:="first" $param_o.newText:="second" $param_o.onlyFirstInstance:=True wpReplaceText($param_o) |
Note:
Since True is passed, only the first instance of "first" is replaced in the body of the Write Pro Area with "second".
Similarly, the following example will replace all instances of "first" with "second".
Example:
$param_o:=New object $param_o.wpObject:=WParea $param_o.wpAreaName:="WParea" $param_o.oldText:="first" $param_o.newText:="second" $param_o.onlyFirstInstance:=False wpReplaceText($param_o) |