Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Copiar texto de rtf (https://www.clubdelphi.com/foros/showthread.php?t=37947)

johurgi 27-11-2006 20:26:41

Copiar texto de rtf
 
Hola a tod@s.

Mi cuestion es la siguiente, tengo un richedit en el cual cargo un fichero rtf de la siguiente forma.

Código Delphi [-]
richedit1.Lines.LoadFromFile('C:\fichero.rtf');

En este texto tengo que substituir unos valores sobre unas palabras claves como son nombre y telefono. Cual es mi sorpresa que cuando ya tengo cargado bien el texto y substituyo las palabras clave el formato de todo el texto se va. De que forma puedo hacerlo para que no pierda el formato.

El codigo entero es el siguiente.
Código Delphi [-]
var
n,t:string
begin
n:=table1nombre.Value;
t:=table1Telefono.Value;
richedit1.Lines.LoadFromFile('C:\fichero.rtf');
richedit1.Lines.Text:=StringReplace(richedit1.Text,'nombre',n,[rfReplaceAll]);
richedit1.Lines.Text:=StringReplace(richedit1.Text,'telefono',t,[rfReplaceAll]);
richedit1.Lines.SaveToFile('C:\final.rtf');
end;

gracias de antemano.

roman 27-11-2006 20:48:51

Prueba a ver si te sirve esta rutina:

Código Delphi [-]
procedure ReplaceText(RichEdit: TRichEdit; const Text, ReplaceText: String);
var
  TextStart: Integer;
  TextLength: Integer;

begin
  TextStart := RichEdit.FindText(Text, 0, Length(RichEdit.Text), [stWholeWord]);
  TextLength := Length(Text);

  while TextStart <> -1 do
  begin
    RichEdit.SelStart := TextStart;
    RichEdit.SelLength := TextLength;
    RichEdit.SelText := ReplaceText;

    TextStart := RichEdit.FindText(Text, 0, Length(RichEdit.Text), [stWholeWord]);
  end;
end;

Ejemplo de uso:

Código Delphi [-]
ReplaceText(RichEdit1, 'nombre', n);

// Saludos

johurgi 28-11-2006 12:25:46

Muchas gracias roman, con tu funcion ya me funciona perfecto.

roman 28-11-2006 18:52:15

Me alegra que ta haya servido. Pero tiene un pequeño defectillo que hay que corregir. En la línea al final del ciclo, donde dice:

Código Delphi [-]
TextStart := RichEdit.FindText(Text, 0, Length(RichEdit.Text), [stWholeWord]);

debe decir

Código Delphi [-]
TextStart := RichEdit.FindText(Text, TextStart, Length(RichEdit.Text), [stWholeWord]);

Como estaba, hacía la búsqueda desde el principio del texto cada vez, y con el cambio lo hace desde donde encontró la ocurrencia anterior de la palabra.

// Saludos

johurgi 28-11-2006 19:05:10

gracias nuevamente, ahora funciona mucho mas rapido.


La franja horaria es GMT +2. Ahora son las 09:35:21.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi