首先用Word編輯報表格式,并排好版,把將要輸出的數(shù)據(jù)項用表單域代替,并取名。這里我們暫時假設(shè)有表單域Item1及Item2(均為文本型),將這個文檔存為模板文件Example.dot,然后按如下步驟進行:
1)運行Delphi3,在Form1里加入一個System部件集里的TDdeClientCov部件,取名為DdeExample,將其ConnectMode設(shè)為ddeManual(手動方式);將DdeService設(shè)為‘(WinWord)’;將Serviceapplication設(shè)為‘WinWord’。
2)編寫一個自定義過程,以激活Word,如下:
PRocedure Tform1.WordActive(Cmds: TStrings);
var
WordPath: String;
begin
if(not DdeExample.OpenLink) then {判斷是否巳動態(tài)鏈接}
begin
if(FindWindow('OpusApp', nil)=0) then
begin
WordPath := 'C:msofficewinword';
if(WordPath=') then
ShowMessage('中文Word未安裝或未設(shè)置路徑,請安裝設(shè)置Word中文 版。')
else begin
DdeExample.ServiceApplication := WordPath+'Winword.exe';
if(DdeExample.OpenLink) then {如果巳動態(tài)鏈接執(zhí)行宏命令}
DdeExample.ExecuteMacroLines(Cmds,False)
else
ShowMessage('無法啟動Word中文版!');
DdeExample.ServiceApplication := 'WinWord.exe';
end;
end
else begin{如果巳動態(tài)鏈接執(zhí)行宏命令}
DdeExample.ExecuteMacroLines(Cmds,False);
end;
end
else
DdeExample.ExecuteMacroLines(Cmds,false);
end;
在private聲明區(qū)里加入如下:
procedure ActiveWord(Cmds: TStrings);
3)在Form1中加入一個按鈕Button1,在其onclick事件里寫如下代碼:
procedure TForm1.Button1Click(Sender: TObject);
var
Cmds:TStringList;{創(chuàng)建Cmds}
TempItem1,TempItem2:String;
begin
cmds:=TStringList.Create;
cmds.Clear;
TempItem1:='數(shù)據(jù)項一';
TempItem2:='數(shù)據(jù)項二';
with Cmds do
begin
Clear;
Add('[FileNew.Template ="Example.Dot″]');{打開模板文件Example.Dot}
Add('[AppMaximize]');{文檔最大化}
Add('[SetFormResult"Item1″,″'+TempItem1+'″]');{將數(shù)據(jù)TempItem1傳給表單域Item1}
Add('[SetFormResult"Item2″,″'+TempItem2+'″]);{將數(shù)據(jù)TempItem2傳給表單域Item2}
end;
WordActive(DdeExample,Cmds);{調(diào)用自定義過程}
Cmds.Free;{釋放Cmds}
end;
運行這個程序,單擊Button1,大家可以發(fā)現(xiàn)Word被啟動了,屏幕上出現(xiàn)了:數(shù)據(jù)項一;數(shù)據(jù)項二兩個數(shù)據(jù)項。最后,大家可以任意修改本報表的格式及數(shù)據(jù),因為這時這個報表與具體的應(yīng)用程序巳沒有關(guān)系了。
本例中用的是中文Word6或中文Word7。由于Word97的宏命令巳變?yōu)閂isual Basic語句,如大家想用Word97實現(xiàn),請將其宏命令改變?yōu)橄鄳?yīng)的代碼。
這是個簡單的示例,大家可以利用Word的宏錄制功能,錄取更多的宏(如自動生成表格、填充文字、變動字體等宏命令),并與數(shù)據(jù)庫的各種表聯(lián)系起來,依次加入Cmds中即可實現(xiàn)您所要求的更復(fù)雜的功能。
新聞熱點
疑難解答
圖片精選