How to create a dynamic table in Word via Delphi?

Asked

Viewed 788 times

0

I have documents .doc/.docx, that I am doing the replacement of Tags, but now I need to create a dynamic table, based on a select, in these same files that I am replacing tags. This table must be below the existing text in the file.

I have done several researches, but I could not succeed with the examples, due to not being well explained. Someone could give an example?

  • Dude, the one time I needed to mess with table in Word, I was able to insert lines, remove and change line data, but I couldn’t create it dynamically. So I had a template file with a one-row table, and I was inserting the other lines as needed. If this fits you, I’ll insert an answer showing you how.

  • @Robertofagundes, already helps, this way I can leave the table in a specific place of the file and go inserting data.

2 answers

1

I made for exporting a program, see if help:

procedure TfrmPrincipal.Word1Click(Sender: TObject);
var
  WordApp, NewDoc, WordTable : OleVariant;
  i : Integer;
  s : string;
begin
  if RichEdit1.Lines.Count > 1 then
  begin
    WordApp := CreateOleObject('Word.Application');
    WordApp.Visible := True;
    NewDoc := WordApp.Documents.Add;
    WordTable := NewDoc.Tables.Add(WordApp.Selection.Range, 1, 1);
    WordTable.Cell(1,1).Range.Paragraphs.Alignment := wdAlignParagraphcenter;
    WordTable.Cell(1,1).Range.Paragraphs.SpaceAfter := 0;
    WordTable.Borders.OutsideLineStyle := wdLineStyleSingle;
    s := '';
    for i := 1 to RichEdit1.Lines.Count-1 do
    begin
      s := s + RichEdit1.Lines.Strings[i]+#13+#10;
    end;
    s := Copy(s,1,Length(s)-2);
    WordApp.Selection.Font.Size := 8;
    WordApp.Selection.Font.Name := 'Courier New';

    WordTable.Cell(1, 1).Range.Text := s;
    // Cleanup...
    WordApp := Unassigned;
    NewDoc := Unassigned;
    WordTable := Unassigned;
  end;
end;

0


There are native components for this. To enable go on:

Component/Install Packages.../ Microsoft Office XP Sample Automation Server Wrapper Components.

Will enable a call palette Servers. Probably what you want is the Twordapplication.

Browser other questions tagged

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