首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

Delphi + Word 有关问题

2012-02-20 
Delphi + Word 问题我贴上我的全部的测试程序请高手指点。主要问题是程序只能运行一次,我想再次运行就出问

Delphi + Word 问题
我贴上我的全部的测试程序请高手指点。 
主要问题是程序只能运行一次,我想再次运行就出问题,请问是什么地方的原因。 
unit Unit1; 

interface 

uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, StdCtrls, Word2000, OleServer; 

type 
  TForm1 = class(TForm) 
  WordApplication1: TWordApplication; 
  WordDocument1: TWordDocument; 
  Button1: TButton; 
  Button2: TButton; 
  Button3: TButton; 
  Button4: TButton; 
  procedure Button1Click(Sender: TObject); 
  procedure FormClose(Sender: TObject; var Action: TCloseAction); 
  procedure Button2Click(Sender: TObject); 
  procedure Button3Click(Sender: TObject); 
  procedure Button4Click(Sender: TObject); 
  procedure B_replace(ss1:String;ss2:String); 
  procedure B_close; 
  private 
  { Private declarations } 
  public 
  { Public declarations } 
  end; 

var 
  Form1: TForm1; 

implementation 

{$R *.dfm} 

procedure TForm1.B_close; 
var 
  savechanges, originalformat, routedocument: olevariant; 
begin 
  savechanges := "C:\Documents and Settings\Administrator\桌面\油品购销合同.doc "; 
  originalformat := unassigned; 
  routedocument := unassigned; 
  try 
  WordDocument1.SaveAs(savechanges); 
  WordDocument1.Close; 
  WordDocument1.Free; 
  WordApplication1.Disconnect; 
  WordApplication1.Quit; 
  except 
  on e: exception do 
  begin 
  showmessage(e.message); 
  WordApplication1.disconnect; 
  end; 
  end; 
end; 

procedure TForm1.B_replace(ss1:String;ss2:String); 
var  
  findtext,matchcase,matchwholeword,matchwildcards,matchsoundslike, 
  matchallwordforms,s_forward,wrap,format,replacewith,replace,s1,s2,s3,s4:olevariant; 
begin 
  findtext:=ss1; 
  matchcase:=false; 
  matchwholeword:=true; 
  matchwildcards:=false; 
  matchsoundslike:=false; 
  matchallwordforms:=false; 
  s_forward:=false; 
  wrap:=wdfindcontinue; 
  format:=false; 
  replacewith:=ss2; 
  replace:=true; s1:=false; s2:=false;s3:=false;s4:=false; 
  WordDocument1.Range.Find.Execute(findtext,matchcase,matchwholeword,matchwildcards,matchsoundslike, 
  matchallwordforms,s_forward,wrap,format,replacewith,replace,s1,s2,s3,s4); 

end; 

procedure TForm1.Button1Click(Sender: TObject); 
begin 
  try 
  WordApplication1.Connect; 
  except 
  Showmessage( "Word没有正确安装!您不能使用该功能! "); 
  exit; 
  end; 
  WordApplication1.Visible:=true; 
  WordApplication1.Caption:= "油品购销合同 "; 
end; 

procedure TForm1.Button2Click(Sender: TObject); 


begin 
  Button3Click(Sender); 
  WordApplication1.Visible:=true; 
  B_replace( "name ", "上海良惠检测仪器厂 "); 
  B_replace( "dz ", " "); 
  B_replace( "dh ", " "); 
  B_replace( "yb ", " "); 
  B_replace( "wzmc ", "特种溶剂油 "); 
  B_replace( "wzsl ", "40吨 "); 
  B_replace( "jsjg ", "5000元/吨 "); 
  B_replace( "zje ", "20万元 "); 
  B_replace( "dx ", "贰拾零万零仟零佰零拾零元零角零分 "); 
  B_replace( "GXXXX-XX-XX-XXXX ", "XS2007080001 "); 
  B_close; 
end; 

procedure TForm1.Button3Click(Sender: TObject); 
var  
  itemindex:olevariant; 
  filename,confirmconversions,readonly,addtorecentfiles,passworddocument,passwordtemplate, 
  revert,writepassworddocument,writepasswordtemplate,format,t1,t2:olevariant; 
begin 
  filename:= "E:\操作Word文档\myword\油品购销合同.doc "; 
  confirmconversions:=false;readonly:=false; 
  addtorecentfiles:=false; 
  passworddocument:= " "; 
  passwordtemplate:= " ";revert:=true;t1:=false; t2:=false; 
  writepassworddocument:= " "; 
  writepasswordtemplate:= " "; 
  format:=wdopenformatdocument; 
   
  WordApplication1.documents.open(filename,confirmconversions, 
  readonly,addtorecentfiles,passworddocument,passwordtemplate, 
  revert,writepassworddocument,writepasswordtemplate,format,t1,t2); 
  itemindex:=1; 
  WordDocument1.connectto(WordApplication1.documents.item(itemindex)); 
  WordApplication1.options.checkspellingasyoutype:=false; 
  WordApplication1.options.checkgrammarasyoutype:=false; 
end; 


procedure TForm1.Button4Click(Sender: TObject); 
begin 
  //WordApplication1.Visible:=false; 
close; 
end; 

end.

[解决办法]
ole对象未释放?

热点排行