線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2409
推到 Plurk!
推到 Facebook!

如何處理 word 之合併列印

缺席
yandav
一般會員


發表:16
回覆:20
積分:7
註冊:2002-10-05

發送簡訊給我
#1 引用回覆 回覆 發表時間:2009-05-18 23:15:03 IP:123.204.xxx.xxx 訂閱
 請教各位前輩:
我有一個 table, 名為 custom.db,有欄位 : no(編號) , name(姓名)
我要將資料送到一個已存在之 word 檔 : custom.doc
在 custom.doc 中有插入 2 個 MergeField 之功能變數 : field1 及 field2 (如下圖)

請問 程式該如何寫 ?
(論壇中有幾篇合併列印的文章, 可是都找不到我要的答案)
我的環境為 XP,Delphi5, Word2003
謝謝!
yandav
一般會員


發表:16
回覆:20
積分:7
註冊:2002-10-05

發送簡訊給我
#2 引用回覆 回覆 發表時間:2009-05-19 16:24:59 IP:123.204.xxx.xxx 訂閱
我在微軟技術支援服務網站,找到這篇:
如何自動化 Word 以執行 [合併列印],從 Delphi
網址: http://support.microsoft.com/kb/229310/zh-tw
參考他的作法,解決了我的問題. 我的程式如下
我不知是否該結案,因為仍有兩點疑惑,希望前輩能解惑
1.感覺上,這個方法,不太直接,要開一個暫存的 doc 當做資料來源,是否可用 dataset 直接合併?
2.如果我要合併一筆資料的欄位很多,那麼暫存 doc 的 table 不就要一直往右邊延伸,會不會有版面不夠寬的問題?
我暫且不結案,若一週後,仍無人幫忙回答,我再結案.
謝謝!

[code delphi]
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, DBTables, Buttons;

type
TForm1 = class(TForm)
SpeedButton1: TSpeedButton;
Query1: TQuery;
procedure SpeedButton1Click(Sender: TObject);
private
{ Private declarations }
wrdApp, wrdDoc: Variant;
procedure CreateMailMergeDataFile;
procedure FillRow(Doc : Variant; Row : Integer;
Text1,Text2 : String);
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation
uses ComObj;
Const wdAlignParagraphLeft = 0;
Const wdAlignParagraphCenter = 1;
Const wdAlignParagraphRight = 2;
Const wdAlignParagraphJustify = 3;
Const wdAdjustNone = 0;
Const wdGray25 = 16;
Const wdGoToLine = 3;
Const wdGoToLast = -1;
Const wdSendToNewDocument = 0;


{$R *.DFM}
procedure TForm1.CreateMailMergeDataFile;
var
wrdDataDoc : Variant;
iCount : Integer;
begin
// Create a data source at C:\DataDoc.doc containing the field data
wrdDoc.MailMerge.CreateDataSource('C:\DataDoc.doc',,,'f1, f2');
// Open the file to insert data
wrdDataDoc := wrdApp.Documents.Open('C:\DataDoc.doc');
//Query the data
With Query1 do begin
Close;
SQL.Clear;
SQL.Text:='Select * from custom where Nno=' '''' 'BB0002' '''';
Open;
// Fill in the data
FillRow(wrdDataDoc, 2, FieldByName('Nno').AsString, FieldByName('Name').AsString);
Close;
end;
// Save and close the file
wrdDataDoc.Save;
wrdDataDoc.Close(False);
end;
procedure TForm1.FillRow(Doc: Variant; Row: Integer; Text1, Text2: String);
begin
Doc.Tables.Item(1).Cell(Row,1).Range.InsertAfter(Text1);
Doc.Tables.Item(1).Cell(Row,2).Range.InsertAfter(Text2);
end;
procedure TForm1.SpeedButton1Click(Sender: TObject);
var
wrdSelection, wrdMailMerge, wrdMergeFields : Variant;
begin
// Create an instance of Word and make it visible
wrdApp := CreateOleObject('Word.Application');
wrdApp.Visible := True;
wrdApp.Documents.Add(Template:='D:\TemplateFile\f1.dot',NewTemplate:=False, DocumentType:=0);
wrdDoc:=wrdApp.ActiveDocument;
wrdSelection := wrdApp.Selection;
wrdMailMerge := wrdDoc.MailMerge;
// Create MailMerge data file
CreateMailMergeDataFile;
// Perform mail merge
wrdMailMerge.Destination := wdSendToNewDocument;
wrdMailMerge.Execute(False);
// Close the original form document
wrdDoc.Saved := True;
wrdDoc.Close(False);
// Notify the user we are done.
ShowMessage('Mail Merge Complete.');
// Clean up temp file
DeleteFile('C:\DataDoc.doc');
end;
end.

[/code]
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#3 引用回覆 回覆 發表時間:2009-05-19 20:08:44 IP:118.169.xxx.xxx 訂閱
感謝分享^_^
遲遲沒有滿意的回覆,按下[缺席]結案即可,由於您分享您的解決方式,文章會繼續保留下去

===================引 用 yandav 文 章===================
我在微軟技術支援服務網站,找到這篇:
如何自動化 Word 以執行 [合併列印],從 Delphi
網址: http://support.microsoft.com/kb/229310/zh-tw
參考他的作法,解決了我的問題. 我的程式如下
我不知是否該結案,因為仍有兩點疑惑,希望前輩能解惑
1.感覺上,這個方法,不太直接,要開一個暫存的 doc 當做資料來源,是否可用 dataset 直接合併?
2.如果我要合併一筆資料的欄位很多,那麼暫存 doc 的 table 不就要一直往右邊延伸,會不會有版面不夠寬的問題?
我暫且不結案,若一週後,仍無人幫忙回答,我再結案.
謝謝!
系統時間:2024-05-08 8:31:48
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!