Ver Mensaje Individual
  #1  
Antiguo 08-11-2023
darkamerico darkamerico is offline
Miembro
 
Registrado: dic 2010
Posts: 233
Reputación: 14
darkamerico Va por buen camino
Red face 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.
Responder Con Cita