Word as the basis for a delphi report that has detail

0

I have a standard word document used in the company to make a presence list in training, but now they want to generate this list, with the names of the participants, already filled through the system in delphi 6. I saw here in the forum how to include field data in word, but in my case besides some that only need to be inserted once, like most of the examples given here (contract, something tab), I have the detail, that is, all participants in the training. I need some fields to be filled in only once, training name, date and instructor, but I need somehow a loop to fill in the detail. Can someone help me? It's kind of urgent.

    
asked by anonymous 21.12.2017 / 13:14

1 answer

1

I'll explain by following the rationale logic as if we were developing from scratch an impression in Word.

First: Have a report template in Word, perhaps putting it as a resource of the application will ensure better security to prevent it from being changed by a third party (as if it were a static file).

Second: Use variables of type variant to instantiate the Word object, it would look something 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, name of training, etc. In the document, place 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.

Room: Once you have 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 solved my problem, I hope it solves your problem.

    
21.12.2017 / 17:17