実際に差し込み印刷を作成しているわけではありません。
役に立つかもしれないいくつかのコード (以下の注を参照) を次に示します。接続を処理し、データ型を提供するためにDelphi のTWordApplication
コンポーネントを使用していますが、次のように進むべき方向性を示す必要があります。
// Drop a TWordApplication (from the Servers palette page) on a new blank form,
// and change it's name to WordApp. No other property changes or components are
// used.
procedure TForm1.FormCreate(Sender: TObject);
var
Doc: _Document;
DBName: string;
Pause, SQL, Connection, SaveChanges: OleVariant;
begin
WordApp.Connect;
Doc := WordApp.Documents.AddOld(EmptyParam, EmptyParam);
WordApp.Visible := True;
SetUpMergeDoc(Doc);
DBName := 'YourDatabasePath\DatabaseName';
Connection := 'YourADOorODBCConnectionString';
SQL := 'SELECT * FROM customer';
Doc.MailMerge.OpenDataSource(DBName, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, Connection,
SQL, EmptyParam, EmptyParam, EmptyParam);
// Do the actual mailmerge.
Pause := False;
Doc.MailMerge.Destination := wdSendToNewDocument;
Doc.MailMerge.Datasource.FirstRecord := wdDefaultFirstRecord;
Doc.MailMerge.Datasource.LastRecord := integer(wdDefaultLastRecord);
Doc.MailMerge.Execute(Pause);
// Save the mailmerged document
SaveChanges := wdDoNotSaveChanges;
WordApp.Quit(SaveChanges, EmptyParam, EmptyParam);
Doc := nil;
WordApp := nil;
end;
procedure TForm1.SetUpMergeDoc(Doc: _Document);
var
R: Range;
Direction: OleVariant;
begin
R := Doc.Range(EmptyParam, EmptyParam);
Direction := wdCollapseEnd;
R.InsertAfter('Dear ');
R.Collapse(Direction);
{ Insert a field with the name of the datasource field }
Doc.MailMerge.Fields.Add(R, 'Company');
R := Doc.Range(EmptyParam, EmptyParam);
R.Collapse(Direction);
R.InsertParagraphAfter;
R.InsertAfter('We have yet to receive payment for our invoice of ');
R.Collapse(Direction);
Doc.MailMerge.Fields.Add(R, 'LastInvoiceDate');
R := Doc.Range(EmptyParam, EmptyParam);
R.Collapse(Direction);
R.InsertAfter('.');
R.InsertParagraphAfter;
R.InsertAfter('Cough up or we''ll send the boys round.');
R.InsertParagraphAfter;
end;
注: このコードの多くは、Deborah Pate のWeb サイトのWord 自動化ページからダウンロードした例から取得したものです。(Deborah は TeamB のメンバーでした (そして今でもそうかもしれません)。) 経由で接続するように変更し、Delphi XE でコンパイルするように更新しただけです。TWordApplication
先ほど言っTWordApplication
たように、Word への接続を確立し、使用されているメソッドと型を公開するために使用しています。他のすべてはコード自体で行われます。