我用以下一段代码打开和关闭word的时候,在第二次打开word时候就会出现
"...'被调用的对象已与其客户断开连接'..."的异常,不知道原因,请大家帮忙诊断!
procedure TForm1.Button1Click(Sender: TObject);
//连接word并将Memo1的内容插入word
begin
try
try
WordApplication1.Connect; //连接word
except
messagedlg('无法连接,没有安装word', mterror, [mbok], 0);
Abort;
end;
WordApplication1.Visible := true; //将wrod程序设为不可见
WordApplication1.Caption := 'delphi control word';
WordApplication1.Options.CheckSpellingAsYouType := False; //关闭拼写检查
WordApplication1.Options.CheckGrammarAsYouType := False; //关闭语法检查
WordDocument1.Range.InsertAfter(Memo1.Text); //插入Memo1的内容到wrod
except
on e: exception do
begin
showmessage(e.Message);
WordApplication1.Disconnect;
end;
end;
end;procedure TForm1.Button4Click(Sender: TObject);
//退出
begin
try
WordDocument1.Close; //关闭编辑的文档
WordApplication1.Disconnect; //断开与程序的连接
close; //退出程序
except
on e: exception do
begin
showmessage(e.Message);
WordApplication1.Disconnect;
end;
end;
end;
"...'被调用的对象已与其客户断开连接'..."的异常,不知道原因,请大家帮忙诊断!
procedure TForm1.Button1Click(Sender: TObject);
//连接word并将Memo1的内容插入word
begin
try
try
WordApplication1.Connect; //连接word
except
messagedlg('无法连接,没有安装word', mterror, [mbok], 0);
Abort;
end;
WordApplication1.Visible := true; //将wrod程序设为不可见
WordApplication1.Caption := 'delphi control word';
WordApplication1.Options.CheckSpellingAsYouType := False; //关闭拼写检查
WordApplication1.Options.CheckGrammarAsYouType := False; //关闭语法检查
WordDocument1.Range.InsertAfter(Memo1.Text); //插入Memo1的内容到wrod
except
on e: exception do
begin
showmessage(e.Message);
WordApplication1.Disconnect;
end;
end;
end;procedure TForm1.Button4Click(Sender: TObject);
//退出
begin
try
WordDocument1.Close; //关闭编辑的文档
WordApplication1.Disconnect; //断开与程序的连接
close; //退出程序
except
on e: exception do
begin
showmessage(e.Message);
WordApplication1.Disconnect;
end;
end;
end;
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货