使用Delphi 7控制Word 2000生成文檔的方法

最近幫同學用Delphi 7寫的一個數據庫應用中的一個功能是將查詢的結果導出到一個Word文檔中保存。雖然Delphi 7的Servers面板中提供了TWordApplication和TWordDocument組件,但是幫助中卻幾乎沒有寫它們的使用方法。於是在中國期刊網上down了許多的相關文章來看,只可惜幾乎都是用Delphi 5寫的(Delphi 7中不能兼容),而且都只是簡單的介紹了一下,甚是鬱悶。在經過一天的摸索之後終於用Delphi 7實現了這個功能。代碼如下:
//uses Word2000, ComObj;
//WordApp: TWordApplication;
//WordDoc: TWordDocument;
procedure TfrmDetails.btnExportClick(Sender: TObject); //單擊“導出“按鈕
var
  V:Variant;
  Template,NewTemplate,DocumentType,Visible:OleVariant;
  itemIndex:OleVariant;
  fileName:Olevariant;
  NoPrompt,OriginalFormat:OleVariant;
  RouteDocument,SaveChanges:OleVariant;
begin
  //指定文檔的路徑和文件名
  fileName:='C:/LogAdmin/doc/'+'值班日誌'+Trim(DBTextID.Caption)+'.doc';
  //如果該日誌的對應Word文檔已經存在則提示是否覆蓋
  if FileExists(fileName)=true then
    begin
      Beep;
      if Application.MessageBox('文檔已經存在,是否覆蓋?','警告',MB_OKCANCEL)=IDCANCEL then
        Abort;
    end;
  //測試當前是否運行了Word 2000
  try
    V:=GetActiveOleObject('Word.Application');
  except
    //未運行則運行之
    V:=CreateOleObject('Word.Basic');
  end;
  try
    //連接到Word 2000
    WordApp.Connect;
  except
    Beep;
    MessageDlg('不能生成文檔,請確認是否安裝了Word 2000!',mtError,[mbOK],0);
    Abort;
  end;
  //顯示Word 2000
  WordApp.Visible:=true;
 
//給調用Add函數使用的實參賦值
  Template:=EmptyParam;
  NewTemplate:=False;
  DocumentType:=wdNewBlankDocument;
  Visible:=true;
  //調用Add函數
  WordApp.Documents.Add(Template,NewTemplate,DocumentType,Visible);
  //連接到新建的文檔
  itemIndex:=1;
  WordDoc.ConnectTo(WordApp.Documents.Item(itemIndex));
  //文檔另存爲
  WordDoc.SaveAs(fileName);
  //開始向Word文檔中寫入內容
  with WordApp.Selection do
    begin
      Font.Size:=20;
      Font.Bold:=2;
      Paragraphs.Alignment:=wdAlignParagraphCenter;
      TypeText('值班日誌詳細內容');
      TypeParagraph; //換行
      TypeParagraph; 
      Font.Size:=12;
      Font.Bold:=0;
      Paragraphs.Alignment:=wdAlignParagraphLeft;
      TypeText('編號:    '+DBTextID.Caption);
      TypeParagraph;
      TypeText('日期:    '+DBTextDate.Caption);
      TypeParagraph;
      TypeText('溫度:    '+DBTextT.Caption);
      TypeParagraph;
      TypeText('溼度:    '+DBTextH.Caption);
      TypeParagraph;
      TypeText('天氣:    '+DBTextWeather.Caption);
      TypeParagraph;
      TypeText('值班人:  '+DBTextName.Caption);
      TypeParagraph;
      TypeText('值班時間:'+DBTextTime.Caption);
      TypeParagraph;
      TypeText('有無異常:'+lbException.Caption);
      TypeParagraph;
      TypeText('使用工具:');
      TypeParagraph;
      TypeText(DBMemoTool.Text);
      TypeParagraph;
      TypeText('現場環境:');
      TypeParagraph;
      TypeText(DBMemoEnv.Text);
      TypeParagraph;
      TypeText('記錄一:  ');
      TypeParagraph;
      TypeText(DBMemoR1.Text);
      TypeParagraph;
      TypeText('記錄二:  ');
      TypeParagraph;
      TypeText(DBMemoR2.Text);
      TypeParagraph;
      TypeText('記錄三:  ');
      TypeParagraph;
      TypeText(DBMemoR3.Text);
      TypeParagraph;
      TypeText('備註:    ');
      TypeParagraph;
      TypeText(DBMemoMemo.Text);
      TypeParagraph;
    end;
 
//保存文檔
  NoPrompt:=false;
  OriginalFormat:=wdOriginalDocumentFormat;
  WordApp.Documents.Save(NoPrompt,OriginalFormat);

  //關閉文檔
  SaveChanges:=wdSaveChanges;
  OriginalFormat:=wdOriginalDocumentFormat;
  RouteDocument:=false;
  WordApp.Documents.Close(SaveChanges,OriginalFormat,RouteDocument);
 
//斷開和Word 2000的連接
  WordApp.Disconnect;

  MessageDlg('日誌內容導出成功!保存爲'+fileName,mtInformation,[mbOK],0);
  //關閉窗體
  frmDetails.Close;
end;

發佈了33 篇原創文章 · 獲贊 2 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章