Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   MySQL (https://www.clubdelphi.com/foros/forumdisplay.php?f=21)
-   -   Exportar el contenido de un Richtext hacia Word (https://www.clubdelphi.com/foros/showthread.php?t=96481)

darkamerico 08-11-2023 19:47:42

Exportar el contenido de un Richtext hacia Word
 
Saludos amigos:
He implementado las siguientes funciones para lograr pasar el contenido desde un componente TRxRichEdit hacia Word:

Código Delphi [-]

procedure TfrmPrincipal.btnXportDocXClick(Sender: TObject);
begin
  if saveDLG.Execute then ExportarArchivoDocX(doc, saveDLG.FileName)
  else ShowMessage('No existe un Documento cargado para Exportar');
end;

procedure ExportarArchivoDocX(const RichEdit: TRxRichEdit; const FileName: string);
var
  WordApp, Document: Variant;
begin
  WordApp := CreateOleObject('Word.Application');
  WordApp.Visible := False;

  Document := WordApp.Documents.Add;

  try
    CopiarContenidoConFormatoAlPortapapeles(RichEdit);
    Document.Content.Paste;
    Document.SaveAs(FileName, 16); // 16 = formato docx
    Document.Close;
    WordApp.Quit;
  finally
    Document := Unassigned;
    WordApp := Unassigned;
  end;
  AbrirDocumentoWord(FileName+'.docx');
end;

procedure CopiarContenidoConFormatoAlPortapapeles(richEdit: TRxRichEdit);
var
  stream: TMemoryStream;
  format: Cardinal;
  data: HGLOBAL;
  ptr: Pointer;
begin
  stream := TMemoryStream.Create;
  try
    richEdit.Lines.SaveToStream(stream);
    stream.Position := 0;

    format := RegisterClipboardFormat(CF_RTF);
    data := GlobalAlloc(GMEM_MOVEABLE or GMEM_ZEROINIT, stream.Size + 1);
    try
      ptr := GlobalLock(data);
      try
        Move(stream.Memory^, ptr^, stream.Size);
      finally
        GlobalUnlock(data);
      end;

      Clipboard.Open;
      try
        Clipboard.SetAsHandle(format, data);
      finally
        Clipboard.Close;
      end;
    except
      GlobalFree(data);
      raise;
    end;
  finally
    stream.Free;
  end;
end;

function AbrirDocumentoWord(const filePath: string): Variant;
var
  WordApp: Variant;
  WordDoc: Variant;
begin
  WordApp := CreateOleObject('Word.Application');
  WordApp.Visible := True;
  WordDoc := WordApp.Documents.Open(filePath);
  Result := WordDoc;
end;

A pesar que el código anterior funciona bien, me gustaría evitar que se cree un documento en el disco duro y pasar directamente el contenido desde el componente TRxRichEdit hacia word, manteniendo su formato claro.

Alguien podría darme una mano por favor?

Gracias de antemano.

duilioisola 09-11-2023 08:32:33

Creo entender que quieres
  1. que se abra Word
  2. pegarle el contenido RTF
  3. que se quede visible, sin guardar, para que el usuario pueda seguir haciendo modificaciones

Supongo que podrías hacer lo siguiente:

Código Delphi [-]
  try
    CopiarContenidoConFormatoAlPortapapeles(RichEdit);
    Document.Content.Paste;
    {
    Document.SaveAs(FileName, 16); // 16 = formato docx
    Document.Close;
    WordApp.Quit;
    }
    WordApp.Visible := True;
  finally
    Document := Unassigned;
    WordApp := Unassigned;
  end;

darkamerico 09-11-2023 13:03:16

Muchas Gracias duilioisola,
efectivamente, tenias razón, me cree otra funcion para hacer el trabajo y quedó asi:

Código Delphi [-]
procedure ExportarArchivoDocX2(const RichEdit: TRxRichEdit);
var
  WordApp, Document: Variant;
begin
  WordApp := CreateOleObject('Word.Application');
  WordApp.Visible := False;

  Document := WordApp.Documents.Add;

  try
    CopiarContenidoConFormatoAlPortapapeles(RichEdit);
    Document.Content.Paste;
    WordApp.Visible := True;
  finally
    Document := Unassigned;
    WordApp := Unassigned;
  end;
end;


Cita:

Empezado por duilioisola (Mensaje 553215)
Creo entender que quieres
  1. que se abra Word
  2. pegarle el contenido RTF
  3. que se quede visible, sin guardar, para que el usuario pueda seguir haciendo modificaciones

Supongo que podrías hacer lo siguiente:

Código Delphi [-]
  try
    CopiarContenidoConFormatoAlPortapapeles(RichEdit);
    Document.Content.Paste;
    {
    Document.SaveAs(FileName, 16); // 16 = formato docx
    Document.Close;
    WordApp.Quit;
    }
    WordApp.Visible := True;
  finally
    Document := Unassigned;
    WordApp := Unassigned;
  end;



La franja horaria es GMT +2. Ahora son las 00:58:47.

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