OLE Automation with Delphi and MS Word [Edit]

Hi,
I'm working with Delphi for a while now, but it's the first time that I need to export in Word some data taken from a sqlite database.

With SQLite there are no problems, everything works as it should. With Word, also because of the lack of good part of documentation, I'm currently having troubles, in particular, in the moment when i go to split a cell of a table, depending on the number of occurrences found with a SQLite query (1 occurrence = do nothing, 2 occurrences = split the cell into two rows, 15 occurrences = split the cell into 15 rows and so on).

 When i create the table in Word, everything works fine, and so does the first column popupation with the database found occurrences.

But when I ask him to split the cell, the next iteration that parses all results found by the query raises an Ole Error "The requested member of the collection does not exist".
Particularly, without the code used to split the cells, everything is fine, the program finishes to elaborate all iterations based on the query results and the process concludes succesfully.
 WITH the splitting code, only the first iteration completes and when the second begins, the Ole Error is raised.

I believe that there is something word doesn't like with the cursor position but the same cursor, controlling the parsing with  WordApp.Visible := True stays in the same place, at least visually (i don't know if word places the visual cursor in one place and a "virtual" one, in another)

This is my code...does someone understand where am I wrong?


adm_tbl := db.GetTable('SELECT * FROM rdb_administration_names ORDER BY administrations_order;');
  for l := 1 to adm_tbl.Count do
    begin
    WordApp.Selection.ParagraphFormat.Alignment := 1;
    WordApp.Selection.Font.Color := $0000FF;
    WordApp.Selection.Font.Size := 14;
    WordApp.Selection.TypeText(adm_tbl.FieldAsString(adm_tbl.FieldIndex['administration_name']));
    WordApp.Selection.TypeText(#13#10#13#10);
    comp_tbl := db.GetTable('SELECT * FROM rdb_compartments WHERE administration_hash = "' + adm_tbl.FieldAsString(adm_tbl.FieldIndex['administration_hash']) + '" ORDER BY compartments_order;');
    for m := 1 to comp_tbl.Count do
      begin
      WordApp.Selection.ParagraphFormat.Alignment := 0;
      WordApp.Selection.Font.Color := $0000C0;
      WordApp.Selection.Font.Size := 10;
      WordApp.Selection.TypeText('COMPARTIMENTO DI ' + comp_tbl.FieldAsString(comp_tbl.FieldIndex['compartment_name']));
      WordApp.Selection.TypeText(#13#10#13#10);
      WordApp.Selection.Font.Color := $0000C0;
      WordApp.Selection.Font.Size := 8;
      WordApp.Selection.Font.Bold := True;
      plant_tbl := db.GetTable('SELECT * FROM rdb_plants INNER JOIN rdb_plant_names ON rdb_plants.plant_hash = rdb_plant_names.plant_hash AND rdb_plants.compartment_hash = "' + comp_tbl.FieldAsString(comp_tbl.FieldIndex['compartment_hash']) + '" ORDER BY rdb_plant_names.plant_name');
      WordApp.Selection.Tables.Add(WordApp.Selection.Range,plant_tbl.Count+1,15);
      WordApp.Selection.Tables.Item(1).Cell(1, 1).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 1).Range.Text := 'Stazione';
      WordApp.Selection.Tables.Item(1).Cell(1, 2).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 2).Range.Text := 'Apparati';
      WordApp.Selection.Tables.Item(1).Cell(1, 3).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 3).Range.Text := 'Att.';
      WordApp.Selection.Tables.Item(1).Cell(1, 4).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 4).Range.Text := 'Sop.';
      WordApp.Selection.Tables.Item(1).Cell(1, 5).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 5).Range.Text := 'Segnalamento';
      WordApp.Selection.Tables.Item(1).Cell(1, 6).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 6).Range.Text := 'PS';
      WordApp.Selection.Tables.Item(1).Cell(1, 7).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 7).Range.Text := 'TC';
      WordApp.Selection.Tables.Item(1).Cell(1, 8).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 8).Range.Text := 'PL';
      WordApp.Selection.Tables.Item(1).Cell(1, 9).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 9).Range.Text := 'SCmt';
      WordApp.Selection.Tables.Item(1).Cell(1, 10).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 10).Range.Text := 'TE';
      WordApp.Selection.Tables.Item(1).Cell(1, 11).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 11).Range.Text := 'TLc';
      WordApp.Selection.Tables.Item(1).Cell(1, 12).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 12).Range.Text := 'M';
      WordApp.Selection.Tables.Item(1).Cell(1, 13).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 13).Range.Text := 'ID';
      WordApp.Selection.Tables.Item(1).Cell(1, 14).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 14).Range.Text := 'Blocco';
      WordApp.Selection.Tables.Item(1).Cell(1, 15).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 15).Range.Text := 'Note';
      pb.Max := plant_tbl.Count;
      for n := 1 to plant_tbl.Count do
        begin
        WordApp.Selection.Tables.Item(1).Cell(n+1, 1).Range.Font.Color := $000000; <--- ERROR HERE AT SECOND ITERATION
        WordApp.Selection.Tables.Item(1).Cell(n+1, 1).Range.Font.Size := 8;
        WordApp.Selection.Tables.Item(1).Cell(n+1, 1).Range.Font.Bold := True;
        WordApp.Selection.Tables.Item(1).Cell(n+1, 1).Range.Text := plant_tbl.FieldAsString(plant_tbl.FieldIndex['plant_name']);
        pb.Position := n;
        StatusBar.Panels[0].Text := 'Exporting ' + adm_tbl.FieldAsString(adm_tbl.FieldIndex['administration_name']) + '->' + comp_tbl.FieldAsString(comp_tbl.FieldIndex['compartment_name']) + '->' + plant_tbl.FieldAsString(plant_tbl.FieldIndex['plant_name']) + ' - ' + IntToStr(n) + ' of ' + IntToStr(plant_tbl.Count);
        unit_tbl := db.GetTable('SELECT * FROM rdb_controlunits WHERE plant_hash = "' + plant_tbl.FieldAsString(plant_tbl.FieldIndex['plant_hash']) + '" ORDER BY controlunit_start;');
        for int := 2 to 15 do
          begin
          WordApp.Selection.Tables.Item(1).Cell(n+1, int).Split(unit_tbl.Count, 1);
          end;
        //
        Application.ProcessMessages;
        plant_tbl.Next;
        end;
      WordApp.Selection.TypeText(#13#10);
      WordApp.Selection.EndKey(6);
      comp_tbl.Next;
      end;
    adm_tbl.Next;
    end;

Edited by: Gabriele Lanza on Feb 18, 2015 3:52 PM
0
Gabriele
2/18/2015 11:52:41 PM
📁 embarcadero.delphi.ole
📃 598 articles.
⭐ 1 followers.

💬 0 Replies
👁️‍🗨️ 1774 Views