One of the easiest ways to integrate Delphi
with Word
is through OleObject
, but keep in mind that if the machine running the application, if Word
is not installed , will generate a EOleException
of Class not registered.
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 in order to do this your document should be as follows, and remembering to put the name of the objects TEdit
to yours in your form:
|Nome | Sobrenome | Endereco | Telefone |
--------------------------------------------------------
|[Nome] | [Sobrenome] | [Endereco] | [Telefone] |