老生常谈——delphi控件WORD的问题(50分)

  • 主题发起人 zcm1975117
  • 开始时间
Z

zcm1975117

Unregistered / Unconfirmed
GUEST, unregistred user!
我最近使用SERVERS面板上的控件来控制WORD,但使用终出一些问题,请大家指教:
一、我用DELPHI打开的是一个WORD的模板文件,在打开的文件上面修改后,存盘时它还是存到我
的模板文件里面去了,应该新保存一个文件才对呀。
二、我想知道我在调用TWordApplication时,检查这之前它是否调用过,如果调用过应该先
释放它。
三、我在释放TWordApplication,在执行WordApplication.Disconntion时,老是弹出错误
说什么“RPC”服务器不能使用。
我把我的代码贴在下面:大家帮我看看吧。
procedure TFbgFrm.OpenWord(fileName1:string);
var
itemindex :eek:levariant;
filename, confirmconversions, readonly, addtorecentfiles,
passworddocument, passwordtemplate, revert,
writepassworddocument, writepasswordtemplate, format,Encoding,Visible: olevariant;
begin
FileName:=FileName1;
confirmconversions := false;
readonly := false;
addtorecentfiles := True;
passworddocument := '';
passwordtemplate := '';
revert := true;
writepassworddocument := '';
writepasswordtemplate := '';
format := wdopenformatdocument;

wordapp.documents.open(filename, confirmconversions,
readonly, addtorecentfiles, passworddocument, passwordtemplate,
revert, writepassworddocument, writepasswordtemplate, format,Encoding,Visible);


{关联文档}
itemindex := 1;
worddoc.connectto(wordapp.documents.item(itemindex));

{turn spell checking of because it takes a long time if enabled and slows down winword}
wordapp.options.checkspellingasyoutype := false;
wordapp.options.checkgrammarasyoutype := false;

end;

procedure TFbgFrm.CloseWord;
//var
//savechanges, originalformat, routedocument: olevariant;
begin
//savechanges := wddonotsavechanges;
//originalformat := unassigned;
//routedocument := unassigned;
try
//wordapp.quit(savechanges, originalformat, routedocument);
WordDoc.Disconnect;
WordApp.Quit;
wordapp.disconnect;
except
on e: exception do
begin
showmessage(e.message);
wordapp.disconnect;
end;
end;
end;

try
wordapp.connect;
except
Application.MessageBox('打开Word出错,请确认已经装了Word','系统提示',MB_OK);
Exit;
end;
wordapp.visible := true;
wordapp.caption := '评估报告';
OpenWord(GetDir+'/评估报告.dot');
end;

procedure TFbgFrm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
CloseWord;
end;

procedure TFbgFrm.WordDocClose(Sender: TObject);
begin
wordapp.Visible:=True;
end;

 
怎么没有人理我呀?
我再加一句,出现RPC这个错误是我关闭WORD之后,再调用CLOSEWORD方法时才出现,如果
不关闭WORD,执行CLOSEWORD方法则不会出错。
 
我帮你解决第一个问题
WordApplication1.Documents.Add(TempPlate, newtemplate); //按照指定的模板,新建一个Word文件。
 
不是通过程序关闭WORD,就不应调用CLOSEWORD方法,因你已经断开了与word的连接。
 
to linsb:
那我怎么知道他有没有不是通过程序关闭WORD的呢?
 
我来回答你的问题。
1:设一个复选框,选择打开新文档。如下:
if newdoc.Checked then //建立一新的空白文档
begin
WordApplication.Documents.Add(Template, NewTemplate,documenttype,visible);
WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex));
end;
WordApplication.Options.CheckSpellingAsYouType := False;//不打开新文档继续写入:
WordApplication.Options.CheckGrammarAsYouType := False;
WordDocument.Range.InsertAfter(messages.Items[messages.itemindex]+#13);
template:=emptyparam; //使用模板的名称,emptyparam的目的是使word服务器采用默认的参数值
newtemplate:=false; //新建文档的类型,True表示为模板,False表示为文档
visible:=true; //打捞的窗口是否可见
documenttype:=emptyparam; //文档类型,默认为空白文档 ,所以赋值为emptyparam
itemindex:=1;
3 :关闭word,格式如下:
var
SaveChanges,
OriginalFormat,
RouteDocument: OleVariant; //声明WordApplication关闭word的三个参数.
begin
SaveChanges := WdDoNotSaveChanges;
OriginalFormat := UnAssigned;
RouteDocument := UnAssigned;
try
WordApplication.Quit(SaveChanges, OriginalFormat, RouteDocument);
WordApplication.Disconnect;
except
on E: Exception do
begin
Showmessage(E.Message);
WordApplication.Disconnect;
end;
end;
end;
 
通常是打开word后,你自己关闭(或退出)了word,你已经退出,调用CLOSEWORD方法就会出问题。
 
那我怎么知道用户有没有关闭它呀?现在就是要检测如果用户关闭了,就不执行那个方法
如果没有关闭就执行那个方法。
 
try
WordApplication.Quit(SaveChanges, OriginalFormat, RouteDocument);
WordApplication.Disconnect;
except
on E: Exception do
begin
Showmessage(E.Message);
// WordApplication.Disconnect;
end;
end;
如果不要显示出错信息:
except
//不执行任何语句
end;
在运行可执行程序就什么也不显示,而退出程序。但调试时会有错误信息,不要理会。
 
to linsb:
我去掉了那句话,可是它还是显示RPC错误呀!
 
注意:
在运行可执行程序就什么也不显示,而退出程序。但调试时会有错误信息,不要理会。
 
唉,怎么这么难办呀?
只要用户自己关闭了WORD后,再执行我的打印事件,就弹出“RPC服务器无法使用”
大家救救呀!
 
贴出打印模块的代码。
 
procedrue printzl;
begin
try
wordapp.connect;
except
Application.MessageBox('打开Word出错,请确认已经装了Word','系统提示',MB_OK);
Exit;
end;
wordapp.visible := true;
wordapp.caption := '评估报告';
OpenWord(GetDir+'/评估报告.dot');
end;

procedure TFbgFrm.OpenWord(fileName1:string);
var
itemindex :eek:levariant;
Template,NewTemplate,DocumentType,Visible:eek:levariant;
begin
Template := fileName1;
NewTemplate := False;
DocumentType := wdTypeDocument;
Visible := True;
WordApp.Documents.Add(Template,NewTemplate,DocumentType,Visible);

{关联文档}
itemindex := 1;
worddoc.connectto(wordapp.documents.item(itemindex));

{turn spell checking of because it takes a long time if enabled and slows down winword}
wordapp.options.checkspellingasyoutype := false;
wordapp.options.checkgrammarasyoutype := false;
end;

procedure TFbgFrm.CloseWord;
var
savechanges, originalformat, routedocument: olevariant;
begin
savechanges := wddonotsavechanges;
originalformat := unassigned;
routedocument := unassigned;
try
wordapp.quit(savechanges, originalformat, routedocument);
WordDoc.Disconnect;
WordApp.Quit;
wordapp.disconnect;
except
//on e: exception do
//begin
//showmessage(e.message);
//wordapp.disconnect;
//end;
end;
end;
/////////////////////////////
主要就是用户在这个界面上可以多次打印报表,好象如果用户不手动关闭WORD就没有问题,
可以多次打印,否则再次打印时就会出错。

 
试试:
procedure TFbgFrm.printzl;
begin
try
CloseWord;
wordapp.connect;
except
Application.MessageBox('打开Word出错,请确认已经装了Word','系统提示',MB_OK);
Exit;
end;
wordapp.visible := true;
wordapp.caption := '评估报告';
OpenWord(GetDir+'/评估报告.dot');
end;
 
还是不行,只要是手工关闭了,再次执行打印时就会弹出“RPC服务器无法使用”的错误,
你试过吗?可以发个给DEMO给我吗?
主要是因为我限制不了不让用户去手动关闭WORD呀!
 
手工关闭,点击Button2,没问题。(但调试时会弹出“RPC服务器无法使用”的错误,不要理会。)
procedure TFbgFrm.Button2Click(Sender: TObject);
begin
printzl;
end;
 
to linsb专家:
再想请教一个问题,怎么样将tstrings替换word里指定的字符串。
var
val:string;
tstr:=tstringlist.create;
tstr.add(1.dfdf) ;
tstr.add(2.dfdf) ;
val:=tstr.text;
replacestr('aa',val)
在word中时,老是在第二行前面显示一个框,是怎么回事呀?





 
>>val:=tstr.text;????
val:=tstr;
 
我是想tstr将里面的所有行输出到word里面,并且排列跟tstr里面一样。
 
顶部