unit unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, OleServer, Word2000, WordXP;type TForm1 = class(TForm) Button1: TButton; CheckBox1: TCheckBox; ListBox1: TListBox; wordApp: TWordApplication; CheckBox2: TCheckBox; CheckBox3: TCheckBox; procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure wordAppDocumentBeforeClose(ASender: TObject; const Doc: _Document; var Cancel: WordBool); procedure ListBox1Click(Sender: TObject); procedure CheckBox3Click(Sender: TObject); procedure CheckBox2Click(Sender: TObject); procedure CheckBox1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1; folderName: String;implementation{$R *.dfm}{$R template.res}procedure TForm1.Button1Click(Sender: TObject);var templateName: OleVariant; newTemplate: OleVariant; ItemIndex: OleVariant; vSelection: Selection; vBookMark: BookMark; vTable: Table; I : Integer;begin //构造打印模板文件名全路径 case ListBox1.ItemIndex of 0: begin templateName := folderName + 'cld.dot'; end; 1: begin templateName := folderName + 'cljg.dot'; end; end; newTemplate := False; try wordApp.Connect(); except MessageDlg('您的计算机上还未安装Microsoft Office Word97或更高的版本!', mtError, [mbOK], 0); Abort; end; //以指定的模板文件创建新Word文档 wordApp.Documents.AddOld(templateName, newTemplate); wordApp.Caption := ListBox1.Items.Strings[ListBox1.ItemIndex]; vSelection := wordApp.Selection; case ListBox1.ItemIndex of 0: begin //取文档中的第1张表 vTable := wordApp.ActiveDocument.Tables.Item(1); vTable.Cell(1, 2).Range.Text := '李鑫 于 北京供电公司'; vTable.Cell(1, 4).Range.Text := '昆明锐祺软件'; vTable.Cell(2, 2).Range.Text := '昆明市白塔路延长线387号星耀大厦1102B座 李鑫(收) 650051 (0871)3126628'; vTable.Cell(3, 2).Range.Text := DateToStr(Now); vTable.Cell(3, 4).Range.Text := DateToStr(Now); vTable.Cell(5, 1).Range.Text := '哈哈哈哈哈哈,我也不知道了。'; vTable.Cell(7, 1).Range.Text := '不知道了'; vTable.Cell(9, 2).Range.Text := '不知道'; end; 1: begin ItemIndex := 1; end; end; for I := 1 to wordApp.ActiveDocument.Bookmarks.Count do begin ItemIndex := I; //取指定Index的书签 vBookMark := wordApp.ActiveDocument.Bookmarks.Item(ItemIndex); //ShowMessage(vBookMark.Name+IntToStr(wordApp.ActiveDocument.Bookmarks.Count)+'Now='+IntToStr(i)); if LowerCase(vBookMark.Name) = 'nd' then begin vBookMark.Select(); vSelection.InsertAfter('2003'); end; if LowerCase(vBookMark.Name) = 'bh' then begin vBookMark.Select(); vSelection.InsertAfter('1'); end; end; wordApp.Visible := CheckBox1.Checked; wordApp.UserName := '李鑫'; //先最小化 wordApp.WindowState := 2; //再最大化 wordApp.WindowState := 1; //打印预览 wordApp.PrintPreview := CheckBox2.Checked; //立即打印 if CheckBox3.Checked then wordApp.PrintOutOld; wordApp.Disconnect();end;procedure TForm1.FormCreate(Sender: TObject);var templateName: String; fileStream: TResourceStream;begin //获得应用程序所在的目录 folderName := ExtractFilePath(Application.ExeName); if not FileExists(folderName+'template.res') then begin MessageDlg('错误:找不到模板资源文件template.res!', mtError, [mbOK], 0); Close; end; //构造WORD打印模板的文件名全路径 templateName := folderName + 'cld.dot'; //获得资源文件里的“处理单”打印模板 fileStream := TResourceStream.Create(hInstance, 'cld', 'WORD'); if not FileExists(templateName) then begin //拆离文件 fileStream.SaveToFile(templateName); //释放 fileStream.Free; end; //构造WORD打印模板的文件名全路径 templateName := folderName + 'cljg.dot'; //获得资源文件里的“处理结果”打印模板 fileStream := TResourceStream.Create(hInstance, 'cljg', 'WORD'); if not FileExists(templateName) then begin //拆离文件 fileStream.SaveToFile(templateName); //释放 fileStream.Free; end; ListBox1.Items.Add('处理单'); ListBox1.Items.Add('处理结果'); ListBox1.Selected[0] := True;end;procedure TForm1.wordAppDocumentBeforeClose(ASender: TObject; const Doc: _Document; var Cancel: WordBool);beginend;procedure TForm1.ListBox1Click(Sender: TObject);beginend;procedure TForm1.CheckBox3Click(Sender: TObject);beginend;procedure TForm1.CheckBox2Click(Sender: TObject);beginend;procedure TForm1.CheckBox1Click(Sender: TObject);beginend;end.