Word as the basis for a report in Delphi that has Detail

Asked

Viewed 352 times

0

Galera 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 data fields in the word, but in my case besides some that only need to be entered once, as most of the examples given here (contract, record of something), I have the Detail, that is, all training participants. I need some fields to be filled out just once, training name, date and instructor, but I need somehow a loop to fill out the Detail. Can someone help me? It’s kind of urgent.

1 answer

1

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, I edited the publication. See if it helps you now.

Browser other questions tagged

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