Fill in a Word document from Delphi

Asked

Viewed 5,023 times

6

I want to create a system in Delphi, where I fill in the labels and then creates a word document with the information I typed...

I put an example below where I will type the information and when I generate it will play this information in a Word document

inserir a descrição da imagem aqui inserir a descrição da imagem aqui

  • 8

    Note to the "veterans" of the site: I even agree that the question is in need of an improvement, and details, but the people who vote to close could be a little less hasty, and spend more time guiding than voting. If it doesn’t work, then it’s okay.

  • 1

    @user8626 : Welcome to Sopt. I suggest you read this link to improve your question: http://answall.com/help/how-to-ask . After a read, you can click "edit" just below your question to add more details.

  • Good morning Anderson. If @Caputo’s reply answers your question I ask you to please accept it so that it does not appear unanswered. If not, please provide more details than you really want so that a response can come.

2 answers

6


One of the easiest ways to integrate Delphi with the Word is through a OleObject, but keep in mind that if on the machine that will run the application, if the Wordnot installed, will generate a EOleException unregistered.

function PreencherDadosArquivo(const NomeArquivo: string): Boolean; 
var 
  WordApp: Variant;
  Documento: Olevariant;
begin 
  WordApp:= CreateOleObject('Word.Application'); 
  try 
    WordApp.Visible := False;
    Documento := WordApp.Documents.Open(NomeArquivo);

    Documento.Content.Find.Execute(FindText := '[Nome]', ReplaceWith := edtNome.Text); 
    Documento.Content.Find.Execute(FindText := '[Sobrenome]', ReplaceWith := edtSobreNome.Text); 
    Documento.Content.Find.Execute(FindText := '[Endereco]', ReplaceWith := edtEnd.Text); 
    Documento.Content.Find.Execute(FindText := '[Telefone]', ReplaceWith := edtTel.Text); 

    Documento.SaveAs('SeuNovoNomeArquivo.doc')
  finally
    WordApp.Quit;
  end;
end;

But to do so your document should be as follows, and remembering to put the name of the objects TEdit for your form:

|Nome   |   Sobrenome   |   Endereco    |  Telefone    |  
--------------------------------------------------------  
|[Nome] |   [Sobrenome] |   [Endereco]  |  [Telefone]  |  

1

A simple way would also be to generate the PDF interface, through a quickreport for example and install a virtual printer, which when it is "print" saved in word doc.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.