I will explain following the logic of reasoning as if we were developing from scratch an impression in Word.
First: Have a report template in Word, maybe you put it as a resource
of the application guarantees you a better security to prevent it from being changed by a third party (as if it were a static file).
According to: Use variables of type variant
to instantiate the Word object, it would be more or less like this:
uses Word2000, ComObj;
function CriarObjetoWord(out pObjWord: variant): boolean;
begin
Result := False;
try
pObjWord := CreateOleObject('Word.Application');
Result := True;
except
ShowMessage('Erro ao criar objeto Word');
end;
end;
Third: You will need to define in your report which fields will be changed, as you said yourself, you will need to change teacher, date, training name, etc. In the document, put an identifier in the report for the text to be replaced, for example:
Eu, @nomeprofessor, atesto que estou realizando o curso de @nomedocurso na data @data.
Quarter: After having the template ready, just change and/or add the fields as follows:
procedure PreencherRelatorio;
var
vObjWord: variant;
i, vQtd: integer;
begin
if not CriarObjetoWord(vObjWord) then
Exit;
{ para substituir campos }
vObjWord.Content.Find.Execute(FindText := ´@nomeprofessor´, ReplaceWith := 'Lucas de Souza');
vObjWord.Content.Find.Execute(FindText := ´@nomedocurso ´, ReplaceWith := 'Lógica de Programação');
vObjWord.Content.Find.Execute(FindText := ´@data´, ReplaceWith := '21/12/2017');
{ vai pra última linha }
vObjWord.Selection.EndKey(wdStory);
{ para adicionar linhas }
vQtd := vObjWord.ActiveDocument.Bookmarks.Count;
for i := 0 to 10 do
begin
vObjWord.ActiveDocument.Bookmarks.Add(Name := 'Teste');
vObjWord.ActiveDocument.Bookmarks.Item(vQtd + 1).Range.Text := 'Olá: ' + IntToStr(i) + #13;
end;
end;
This way solved my problem, I hope you solve yours.
Good afternoon, Lucas. I don’t think I expressed myself well. These fields that are changed only with a content I have already managed to do, the problem is the list of students, since it is not to fill only one student but the whole list. But thank you for your explanation.
– Barbara Fidelis
Barbara, I edited the publication. See if it helps you now.
– Lucas de Souza Cruz