Problem with variables

General TRichView support forum. Please post your questions here
Sergey Tkachenko
Site Admin
Posts: 17333
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Do you need to make text inside {} protected?
Can this text be multiline? Can it have '{' and '}' inside?
Bjoern
Posts: 21
Joined: Tue May 11, 2010 4:05 pm

Post by Bjoern »

It's only Singleline and have no { or } inside, it's only one or two words, e.g. {script1 user2}, the real javascript is inserted after the RTF2HTML-converting
Bjoern
Posts: 21
Joined: Tue May 11, 2010 4:05 pm

Post by Bjoern »

Hello,

the insering of protected text is easy, i use

Code: Select all

procedure MyProcedure(MyInsertString : string);
var
  OldStyleNo:Integer;

  function GetProtectedStyle(RVStyle: TRVStyle; StyleNo: Integer): Integer;
  var TextStyle: TFontInfo;
  begin
    TextStyle := TFontInfo.Create(nil);
    try
      TextStyle.Assign(RVStyle.TextStyles[StyleNo]);
      TextStyle.Protection := [rvprModifyProtect,rvprConcateProtect,rvprDoNotAutoSwitch,rvprStyleProtect];
      TextStyle.BackColor := clSilver ;
      Result := RVStyle.TextStyles.FindSuchStyle(StyleNo, TextStyle, RVAllFontInfoProperties);
      if Result<0 then begin
        RVStyle.TextStyles.Add;
        Result := RVStyle.TextStyles.Count-1;
        RVStyle.TextStyles[Result].Assign(TextStyle);
        RVStyle.TextStyles[Result].Standard := False;
      end;
    finally
      TextStyle.Free;
    end;
  end;

begin	
    if (MyForm.ActiveControl is TDBRichViewEdit) then
    begin
		MyInsertString := '{{'+MyInsertString+'}}';
		OldStyleNo := (MyForm.ActiveControl as TDBRichViewEdit).CurTextStyleNo;	
		(MyForm.ActiveControl as TDBRichViewEdit).CurTextStyleNo := GetProtectedStyle((MyForm.ActiveControl as TDBRichViewEdit).Style, OldStyleNo);
        (MyForm.ActiveControl as TDBRichViewEdit).InsertStringTag(MyInsertString,0);
        (MyForm.ActiveControl as TDBRichViewEdit).CurTextStyleNo := OldStyleNo;
    end;
end;
but i haven't found a way to make the text protected again after reading it from the database.

I think the right place for this is the DBRichViewEditLoadDocument-Event and i have found the procedure FillFields from the MailMerge-examples, but FillFields change the text and don't touch the Style, i need a function for changing the Style and don't touching the text.

Do anybody have an idea for this?

Greetings
Bjoern
Sergey Tkachenko
Site Admin
Posts: 17333
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

I am sorry for the long delay.
The simplest solution would be searching for text inside {}, selecting it (using SetSelectionBounds), and applying a protected style to the selection (ApplyTextStyle). However, this is not the fastest way, and using editing methods for processing documents is not an optimal solution.

I created a demo:
http://www.trichview.com/support/files/ ... fields.zip
This demo uses an undocumented function RVInsertString from RVInsertItems.pas. This function is much simpler than working with items directly, so you can find that this demo is simpler than other mail merging demos.
jullianmoreira
Posts: 1
Joined: Tue May 07, 2013 4:40 pm

Similar problem

Post by jullianmoreira »

Hi all, I am having a similar problem. In this case I am using a TRichViewEdit.

My case is:

1 - I have a Table on a Database that store the RVF file with some Variables like {$Table.Field}

2 - I Have a Report with the component TQRRichView that can load from a Stream to show the RVF file on the Report

3 - I am trying to Load the RVF file from the Database to a Runtime created TRichView, then I can change the variables inside the RVF file loaded from the Stream.

My true problem is that I can not search the variable within the RVF file loaded to the Runtime TRichView.

Obs: Until the point to Load from the Database I am successful, but from this on when I need to find the variables and change them I recieve the "List index out of Bound" error.

Here is my function to change the variables:
=============================================
function TAuxMacro.GetTextSubstituido(pQMacro, QRelaConfig: TADOQuery): Boolean;
var
vRVTmp : TRichViewEdit;
vRVS : TRVStyle;
i, im : Integer;
nome, s : String;
vStream : TStream;
begin
vStream := TMemoryStream.Create;
TBlobField(QRelaConfig.FieldByName('ReCo_Texto')).SaveToStream(vStream);
vStream.Position := 0;

vRVS := TRVStyle.Create(nil);
vRVTmp := TRichViewEdit.Create(nil);

vRVTmp.Style := vRVS;
vRVTmp.RVFOptions := vRVTmp.RVFOptions + [rvfoSaveTextStyles, rvfoSaveParaStyles];
vRVTmp.LoadRVFFromStream(vStream);

for im := 0 to Length(Macros) -1 do
begin
nome:= '{$' + Macros[im].tabela + '.' + Macros[im].traducao + '}';
vRVTmp.RVData.SearchTextR(True,False, False, True, False, False, nome);
vRVTmp.InsertText(pQMacro.FieldByName(Macros[im].Campo).AsString);
end;
end;

==============================================

If some one can help with this i will appreciate, thank you!
Sergey Tkachenko
Site Admin
Posts: 17333
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

SearchText and InsertText requires a formatted document. But it is not formatted after LoadRVFFromStream.
To solve this problem, call vRVTmp.Format after vRVTmp.LoadRVFFromStream.

Please do not use SearchTextR, use SearchText (or SearchTextA or SearchTextW).

PS: your code assumes that there is exactly one occurrence of each field.
It's better to write

Code: Select all

while vRVTmp.RVData.SearchText(...) do
  vRVTmp.InsertText(...); 
PPS: this is very inefficient solution; its better to take one of mail-merge demos as a base: http://www.trichview.com/forums/viewtopic.php?t=8
Post Reply