WPCubed Product Support

Replace token with another WPRichText content

Hi,

I have an instance of WPRichText with this:

"My name is <name>"

I want to replace "<name>" with the content of another WPRichText, so I did:

WPRichText1.Finder.ReplaceAll("<name>",WPRichText2.AsString);

The thing is, the replaced text apears as:

{\rtf1\ansi\deff0\uc1\ansicpg1252\deftab720{\fonttbl{\f0\fnil\fcharset1 Arial;}{\f1\fnil\fcharset2 Wingdings;}{\f2\fnil\fcharset2 Symbol;}}{\colortbl\red0\green0\blue0;\red255\green0\blue0;\red0\green128\blue0;\red0\green0\blue255;\red255\green255\blue0;\red255\green0\blue255;\red128\green0\blue128;\red128\green0\blue0;\red0\green255\blue0;\red0\green255\blue255;\red0\green128\blue128;\red0\green0\blue128;\red255\green255\blue255;\red192\green192\blue192;\red128\green128\blue128;\red0\green0\blue0;\red128\green128\blue0;}\wpprheadfoot1\paperw12240\paperh20160\margl567\margr740\margt1167\margb2830\headery254\footery254\endnhere\sectdefaultcl{\

If I do:

WPRichText1.Finder.ReplaceAll("<name>",WPRichText2.Text);

Then its OK, but I loose the formatting I had in WPRichText2.

I tried with Finder using Finder.FoundText but it happens the same thing.

So I ask, How do I replace the token <name> in WPRichText1 with the content of another WPRichText without loose the text format and without have this RTF code?

Thanks

#1 ♥ 0
6.6k Views 3 users read 3 min
support

Hi,

ReplaceAll does not "load" text, si it does not interpret RTF.

You do this:

while Finder.Next ... do
begin
Finder.SelectText;
SelectionAsString := rtfcode
end;

#2 ♥ 0

@wpsupport:
Hi,

ReplaceAll does not "load" text, si it does not interpret RTF.

You do this:

while Finder.Next ... do
begin
Finder.SelectText;
SelectionAsString := rtfcode
end;

It worked, thanks Julian, I mp'ed you.

#3 ♥ 0

Strange, I have a very similar problem.

If I use

WPRichText1.Finder.ToStart;     WPRichText1.Finder.ReplaceAll('xxxx', 'yyyy');     WPRichText1.ReformatAll(True, True);

then it works. Unfortunately I can't use that approach. I need to evaluate tags and replace them intelligently.
(A tag would look like this: {V Params})

So I tried

var StartCurPos : integer;
begin
     WPRichText1.Finder.ToStart;

     while WPRichText1.Finder.Next('xxxx') do
     begin
          StartCurPos:=WPRichText1.Finder.Position;

  // here the end tag would be searched and the content of 
  //   the tag would be evaluated, however I keep this
  //   sample simple

          WPRichText1.SetSelPosLen(StartCurPos, 4);

//          WPRichText1.InputString('yyyy');
          WPRichText1.SelectionAsString:='yyyy';
     end;
end;

and this doesn't work, no matter which insertion method I choose. The formatting of the paragraph is always lost for the inserted text.

HELP!
Thanks!

#4 ♥ 0
support

Hi,

my recommendation did not use SetSelPosLen. It was

while Finder.Next ... do 
begin 
Finder.SelectText; 
SelectionAsString := rtfcode 
end;

That used to work.

You can also examine the code of "ReplaceTokens" - it does pretty similar stuff.

Julian

#5 ♥ 0
This category is archived. No new posts are possible.