Search and Replace text in Header

ScaleRichView support and discussion (TRichView add-on for WYSIWYG editing)
Post Reply
DelphiDude
Posts: 17
Joined: Sun Mar 27, 2016 5:15 pm

Search and Replace text in Header

Post by DelphiDude »

i have the srichview working with loading an rtf and the do a search and replace but how do i get it to search and replace info in the header.
Just curious since the search and replace with the srichview needs to be done programmatically, is there a better/simpler way to search and replace all of a select text with my text?
Here is the search and replace code:

Code: Select all

    rveDoc.RichViewEdit.SetSelectionBounds(0, rveDoc.RichViewEdit.GetOffsBeforeItem(0), 0, rveDoc.RichViewEdit.GetOffsBeforeItem(0));
    if rveDoc.RichViewEdit.SearchText(InSearch,[RVEdit.TRVESearchOption.rvseoMatchCase,
    RVEdit.TRVESearchOption.rvseoDown, RVEdit.TRVESearchOption.rvseoMultiItem]) then
    begin
      rveDoc.RichViewEdit.InsertText(InReplace, False);
      while (rveDoc.RichViewEdit.SearchText(InSearch,[RVEdit.TRVESearchOption.rvseoMatchCase,
    RVEdit.TRVESearchOption.rvseoDown, RVEdit.TRVESearchOption.rvseoMultiItem,
    RVEdit.TRVESearchOption.rvseoSmartStart])) do
      begin
        rveDoc.RichViewEdit.InsertText(InReplace, False);
      end;
    end;
Sergey Tkachenko
Site Admin
Posts: 17236
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

To search in the active sub-editor (main document, header, footer, note, text box), use ActiveEditor instead of RichViewEdit:

Code: Select all

rveDoc.ActiveEditor.SetSelectionBounds(
  0, rveDoc.ActiveEditor.GetOffsBeforeItem(0), 
  0, rveDoc.ActiveEditor.GetOffsBeforeItem(0)); 
while (rveDoc.ActiveEditor.SearchText(InSearch,[RVEdit.TRVESearchOption.rvseoMatchCase, 
    RVEdit.TRVESearchOption.rvseoDown, RVEdit.TRVESearchOption.rvseoMultiItem, 
    RVEdit.TRVESearchOption.rvseoSmartStart])) do 
        rveDoc.ActiveEditor.InsertText(InReplace, False);


If you need this replacement as an editing (undoable) operation, this use this code.
DelphiDude
Posts: 17
Joined: Sun Mar 27, 2016 5:15 pm

Still not getting my header updated...

Post by DelphiDude »

Here is my loading of the rtf file.

Code: Select all

      rveDoc.Clear;
      rveDoc.ActiveEditor.LoadRTF(_rtfDirectory + cklstbxRTF.Items.Items[I].Text + '.rtf');
      rveDoc.Format;
      rveDoc.Update;
then after i get my data from the DB i call the search and replace routine.

Code: Select all

procedure TfrmHKLDocuments.SearchAndReplace(InSearch, InReplace: string) ;
var
  startpos, Position, endpos: integer;
begin
    rveDoc.CanUpdate := False;
    rveDoc.SetBusy(True);
    rveDoc.StopCursor;

    rveDoc.ActiveEditor.SetSelectionBounds(
      0, rveDoc.ActiveEditor.GetOffsBeforeItem(0),
      0, rveDoc.ActiveEditor.GetOffsBeforeItem(0));
    while (rveDoc.ActiveEditor.SearchText(InSearch,[RVEdit.TRVESearchOption.rvseoMatchCase,
      RVEdit.TRVESearchOption.rvseoDown, RVEdit.TRVESearchOption.rvseoMultiItem,
      RVEdit.TRVESearchOption.rvseoSmartStart])) do
        rveDoc.ActiveEditor.InsertText(InReplace, False);
   rveDoc.StartCursor;
   rveDoc.SetBusy(False);
   rveDoc.CanUpdate := True;
end;
then i save the file.

Code: Select all

      if sdDocFile.Execute() then
      begin
        rveDoc.ActiveEditor.SaveDocX(sdDocFile.FileName, False);
        ShellExecute(0, 'open', Pchar(sdDocFile.FileName), nil, nil, SW_SHOWNORMAL);
      end
      else
      begin
        rveDoc.ActiveEditor.SaveDocX(_docDirectory + _KeyID + ' - ' +
          Trim(cklstbxRTF.Items.Items[I].Text) + '.doc', False);
      end;
when the WORD open the new file the header text is unchanged.
Sergey Tkachenko
Site Admin
Posts: 17236
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

First, about loading. To load a new document, load it in SRV.RichViewEdit, not in SRV.ActiveEditor.
Because, if a note or a header is active, the document will be loaded in this note or a header. So the code is:

Code: Select all

      rveDoc.Clear; 
      rveDoc.RichViewEdit.LoadRTF(_rtfDirectory + cklstbxRTF.Items.Items[I].Text + '.rtf'); 
      rveDoc.Format;
As for replacing:

Code: Select all

procedure TfrmHKLDocuments.SearchAndReplace(InSearch, InReplace: string; Editor: TSRVRichViewEditType) ; 
var 
  startpos, Position, endpos: integer; 
begin 
    if not rveDoc.StartEditing(Editor) then
      exit;
    rveDoc.CanUpdate := False; 
    rveDoc.ActiveEditor.SetSelectionBounds( 
      0, rveDoc.ActiveEditor.GetOffsBeforeItem(0), 
      0, rveDoc.ActiveEditor.GetOffsBeforeItem(0)); 
    while (rveDoc.ActiveEditor.SearchText(InSearch,[RVEdit.TRVESearchOption.rvseoMatchCase, 
      RVEdit.TRVESearchOption.rvseoDown, RVEdit.TRVESearchOption.rvseoMultiItem, 
      RVEdit.TRVESearchOption.rvseoSmartStart])) do 
        rveDoc.ActiveEditor.InsertText(InReplace, False); 
   rveDoc.CanUpdate := True; 
end;


To replace in the main document, call SearchAndReplace(..., srvrveMain).
To replace in the header of the current page, call SearchAndReplace(..., srvrveHeader).
To replace in the footer of the current page, call SearchAndReplace(..., srvrveFooter).
Post Reply