Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   DBGrid in MS-Word exportieren und Text einfügen (https://www.delphipraxis.net/192742-dbgrid-ms-word-exportieren-und-text-einfuegen.html)

localh0st 16. Mai 2017 12:07

Delphi-Version: 10 Berlin

DBGrid in MS-Word exportieren und Text einfügen
 
Hallo,

ich habe es mit folgendem Beispiel geschafft, mein DB-Grid in Word zu exportieren:
http://delphi.cjcsoft.net//viewthread.php?tid=47128

Ich möchte nun gerne mittels OLE-Automation darüber, sowie darunter Text in das Dokument schreiben können.

Das mache ich folgendermaßen:

Delphi-Quellcode:
begin

  try
    Word := CreateOleObject('Word.Application');
  except
    ShowMessage('Fehler.');
    exit;
  end;
  WordDokument := Word.Application.Documents.Add;
  Word.Visible := true;
   // Schriftart für die Überschrift
  Word.Selection.Font.Name := 'Calibri';
  Word.Selection.Font.Size := 20;
  Word.Selection.ParagraphFormat.Alignment := 1;
  // Überschrift
  Word.Selection.TypeText('EinText');
  // Schriftart für restlichen Text
  Word.Selection.Font.Size := 11;
  Word.Selection.ParagraphFormat.Alignment := 0;
  // Text einfügen
  Word.Selection.TypeText('Firma: Musterfirma GmbH'+#13);
  Word.Selection.TypeText('Mitarbeiter: '+Vorname+' '+Nachname+#13);
  Word.Selection.TypeText('Personalnummer: '+inttostr(PersonalNr)+#13+#13);
end;

//DB-Grid einfügen
Ich habe schon versucht den Code der verlinkten Seite einzubauen. Er erstellt mir daraufhin aber immer nur ein Dokument mit dem Grid und übernimmt den Text nicht. Natürlich habe ich das erstellen des Dokuments aus dem verlinkten Code herausgenommen, sodass nur ein Dokument erstellt wird.

Hat hier jemand einen Ansatz?

Dankeschön.

nahpets 16. Mai 2017 12:43

AW: DBGrid in MS-Word exportieren und Text einfügen
 
ungetestet:
Delphi-Quellcode:
procedure GridToWord(WordApp : OleVariant; Grid :TDBGrid ; FormatNum :integer);
var
  x : integer;
  y : integer;
  GColCount : integer;
  GRowCount : integer;
begin
  GColCount := Grid.Columns.Count;
  GRowCount := Grid.DataSource.DataSet.RecordCount;
  WordApp.Range.Font.Size := Grid.Font.Size;
  WordApp.PageSetup.Orientation := 1;
  WordApp.Tables.Add( WordDokument.Range,GRowCount+1,GColCount);
  WordApp.Range.InsertAfter('Date ' + Datetimetostr(Now));
  WordApp.Range.Tables.Item(1).AutoFormat(FormatNum,1,1,1,1,1,0,0,0,1);
  for y := 1 to GColCount do begin
    WordApp.Tables.Item(1).Cell(1,y).Range.InsertAfter(Grid.Columns[y-1].Title.Caption);
  end;
  x := 1;
  Grid.DataSource.DataSet.First;
  while not Grid.DataSource.DataSet.Eof do begin
    x := x + 1;
    for y := 1 to GColCount do begin
      WordApp.Tables.Item(1).Cell(x,y).Range.InsertAfter(Grid.DataSource.DataSet.FieldByName(Grid.Columns[y - 1].FieldName).Asstring);
      // Würde hier nicht dashier ausreichen?
      WordApp.Tables.Item(1).Cell(x,y).Range.InsertAfter(Grid.Columns[y - 1].Field.AsString);
    end;
    Grid.DataSource.DataSet.Next;
  end;
  WordApp.Range.Tables.Item(1).UpdateAutoFormat;
end;

begin
  try
    WordApp := CreateOleObject('Word.Application');
  except
    on e : Exception do begin
      ShowMessage(e.Message);
      exit;
    end;
  end;
  WordApp.Visible := true;
  WordDokument := WordApp.Application.Documents.Add;
   // Schriftart für die Überschrift
  WordApp.Selection.Font.Name := 'Calibri';
  WordApp.Selection.Font.Size := 20;
  WordApp.Selection.ParagraphFormat.Alignment := 1;
  // Überschrift
  WordApp.Selection.TypeText('EinText');
  // Schriftart für restlichen Text
  WordApp.Selection.Font.Size := 11;
  WordApp.Selection.ParagraphFormat.Alignment := 0;
  // Text einfügen
  WordApp.Selection.TypeText('Firma: Musterfirma GmbH');
  WordApp.Selection.TypeParagraph;
  WordApp.Selection.TypeText(Format('Mitarbeiter: %s %s'[Vorname,Nachname]));
  WordApp.Selection.TypeParagraph;
  WordApp.Selection.TypeText(Format('Personalnummer: %d',[PersonalNr]));
  WordApp.Selection.TypeParagraph;
  WordApp.Selection.TypeParagraph;
  GridToWord(WordApp, Grid, FormatNum);
end;

p80286 16. Mai 2017 12:44

AW: DBGrid in MS-Word exportieren und Text einfügen
 
Auf den ersten Blick, wer mit einer Selection arbeiten will benötigt vorher ein select!
Ggf. ist ein Range-Object sinnvoller, da keine Anzeige notwendig ist, und es gibt nur ein select.

Gruß
K-H

localh0st 16. Mai 2017 13:19

AW: DBGrid in MS-Word exportieren und Text einfügen
 
Zitat:

Zitat von nahpets (Beitrag 1371595)
ungetestet:
Delphi-Quellcode:
procedure GridToWord(WordApp : OleVariant; Grid :TDBGrid ; FormatNum :integer);
var
  x : integer;
  y : integer;
  GColCount : integer;
  GRowCount : integer;
begin
  GColCount := Grid.Columns.Count;
  GRowCount := Grid.DataSource.DataSet.RecordCount;
  WordApp.Range.Font.Size := Grid.Font.Size;
  WordApp.PageSetup.Orientation := 1;
  WordApp.Tables.Add( WordDokument.Range,GRowCount+1,GColCount);
  WordApp.Range.InsertAfter('Date ' + Datetimetostr(Now));
  WordApp.Range.Tables.Item(1).AutoFormat(FormatNum,1,1,1,1,1,0,0,0,1);
  for y := 1 to GColCount do begin
    WordApp.Tables.Item(1).Cell(1,y).Range.InsertAfter(Grid.Columns[y-1].Title.Caption);
  end;
  x := 1;
  Grid.DataSource.DataSet.First;
  while not Grid.DataSource.DataSet.Eof do begin
    x := x + 1;
    for y := 1 to GColCount do begin
      WordApp.Tables.Item(1).Cell(x,y).Range.InsertAfter(Grid.DataSource.DataSet.FieldByName(Grid.Columns[y - 1].FieldName).Asstring);
      // Würde hier nicht dashier ausreichen?
      WordApp.Tables.Item(1).Cell(x,y).Range.InsertAfter(Grid.Columns[y - 1].Field.AsString);
    end;
    Grid.DataSource.DataSet.Next;
  end;
  WordApp.Range.Tables.Item(1).UpdateAutoFormat;
end;

begin
  try
    WordApp := CreateOleObject('Word.Application');
  except
    on e : Exception do begin
      ShowMessage(e.Message);
      exit;
    end;
  end;
  WordApp.Visible := true;
  WordDokument := WordApp.Application.Documents.Add;
   // Schriftart für die Überschrift
  WordApp.Selection.Font.Name := 'Calibri';
  WordApp.Selection.Font.Size := 20;
  WordApp.Selection.ParagraphFormat.Alignment := 1;
  // Überschrift
  WordApp.Selection.TypeText('EinText');
  // Schriftart für restlichen Text
  WordApp.Selection.Font.Size := 11;
  WordApp.Selection.ParagraphFormat.Alignment := 0;
  // Text einfügen
  WordApp.Selection.TypeText('Firma: Musterfirma GmbH');
  WordApp.Selection.TypeParagraph;
  WordApp.Selection.TypeText(Format('Mitarbeiter: %s %s'[Vorname,Nachname]));
  WordApp.Selection.TypeParagraph;
  WordApp.Selection.TypeText(Format('Personalnummer: %d',[PersonalNr]));
  WordApp.Selection.TypeParagraph;
  WordApp.Selection.TypeParagraph;
  GridToWord(WordApp, Grid, FormatNum);
end;

Super, Dankeschön :thumb:


Alle Zeitangaben in WEZ +1. Es ist jetzt 19:13 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz