A
age
Unregistered / Unconfirmed
GUEST, unregistred user!
用delphi开发组件目的将word文档生成html文件,如下方法在NT下能正常执行,但在
win2000下提示错为:“错误:Word可能没有安装”,即执行wordapp.connect出错。
但同样该方法在win2000下用应用程序的方式又可以正常执行。万望高手赤脚。
procedure TUpload.GenerateHtml(DocFileName, OutputFile: OleVariant;
DeleteIt: WordBool);
var WordApp:TWordApplication;
WordDoc:TWordDocument;
ItemIndex :OleVariant;
ConfirmConversions, ReadOnly, AddToRecentFiles,
PasswordDocument, PasswordTemplate, Revert,
WritePasswordDocument, WritePasswordTemplate, Format: OleVariant;
SaveChanges, OriginalFormat, RouteDocument,FileFormat: OleVariant;
begin
FErrStr :='';
if pos('.doc',DocFilename)=0 then
begin
FErrStr:='错误:请确认输入文件是否是word文件!' ;
exit;
end;
if not FileExists(DocFileName) then
begin
FErrStr:='错误:请确认输入文件是否存在!' ;
exit;
end;
try
WordApp:=TWordApplication.Create(nil);
WordDoc:=TWordDocument.Create(nil);
except
FErrStr:='错误:创建Word程序时出错!';
exit;
end;
try
try
Wordapp.Connect;
except
FErrStr:='错误:Word可能没有安装!';
raise;
//exit;
end;
WordApp.Visible := false; //不显示word程序
WordApp.Caption := 'Delphi automation - '+DocFileName;
ConfirmConversions := False;
ReadOnly := False;
AddToRecentFiles := False;
PasswordDocument := '';
PasswordTemplate := '';
Revert := True;
WritePasswordDocument := '';
WritePasswordTemplate := '';
Format := wdOpenFormatDocument;
//打开word文档
WordApp.Documents.Open( DocFileName, ConfirmConversions,
ReadOnly, AddToRecentFiles, PasswordDocument, PasswordTemplate,
Revert, WritePasswordDocument, WritePasswordTemplate, Format );
//Assign WordDocument component
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;
//另存为
try
FileFormat:=8; //html文档
worddoc.SaveAs(OutPutFile,fileformat); //生成文档
except
FErrStr:='错误:保存word文档为Htm时出错!';
WordDoc.Disconnect ;
WordApp.Disconnect;
end;
SaveChanges := WdDoNotSaveChanges;
OriginalFormat := UnAssigned;
RouteDocument := UnAssigned;
try
WordDoc.Close ;
WordDoc.Disconnect ;
WordApp.Quit(SaveChanges, OriginalFormat, RouteDocument);
WordApp.Disconnect;
except
FErrStr:='错误:word文档关闭时错!';
WordApp.Disconnect;
end;
if DeleteIt then
begin
//FErrStr:='原文件将北删除');
DeleteFile(DocFileName);
end;
finally
worddoc.free;
wordapp.Free;
end;
end;
win2000下提示错为:“错误:Word可能没有安装”,即执行wordapp.connect出错。
但同样该方法在win2000下用应用程序的方式又可以正常执行。万望高手赤脚。
procedure TUpload.GenerateHtml(DocFileName, OutputFile: OleVariant;
DeleteIt: WordBool);
var WordApp:TWordApplication;
WordDoc:TWordDocument;
ItemIndex :OleVariant;
ConfirmConversions, ReadOnly, AddToRecentFiles,
PasswordDocument, PasswordTemplate, Revert,
WritePasswordDocument, WritePasswordTemplate, Format: OleVariant;
SaveChanges, OriginalFormat, RouteDocument,FileFormat: OleVariant;
begin
FErrStr :='';
if pos('.doc',DocFilename)=0 then
begin
FErrStr:='错误:请确认输入文件是否是word文件!' ;
exit;
end;
if not FileExists(DocFileName) then
begin
FErrStr:='错误:请确认输入文件是否存在!' ;
exit;
end;
try
WordApp:=TWordApplication.Create(nil);
WordDoc:=TWordDocument.Create(nil);
except
FErrStr:='错误:创建Word程序时出错!';
exit;
end;
try
try
Wordapp.Connect;
except
FErrStr:='错误:Word可能没有安装!';
raise;
//exit;
end;
WordApp.Visible := false; //不显示word程序
WordApp.Caption := 'Delphi automation - '+DocFileName;
ConfirmConversions := False;
ReadOnly := False;
AddToRecentFiles := False;
PasswordDocument := '';
PasswordTemplate := '';
Revert := True;
WritePasswordDocument := '';
WritePasswordTemplate := '';
Format := wdOpenFormatDocument;
//打开word文档
WordApp.Documents.Open( DocFileName, ConfirmConversions,
ReadOnly, AddToRecentFiles, PasswordDocument, PasswordTemplate,
Revert, WritePasswordDocument, WritePasswordTemplate, Format );
//Assign WordDocument component
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;
//另存为
try
FileFormat:=8; //html文档
worddoc.SaveAs(OutPutFile,fileformat); //生成文档
except
FErrStr:='错误:保存word文档为Htm时出错!';
WordDoc.Disconnect ;
WordApp.Disconnect;
end;
SaveChanges := WdDoNotSaveChanges;
OriginalFormat := UnAssigned;
RouteDocument := UnAssigned;
try
WordDoc.Close ;
WordDoc.Disconnect ;
WordApp.Quit(SaveChanges, OriginalFormat, RouteDocument);
WordApp.Disconnect;
except
FErrStr:='错误:word文档关闭时错!';
WordApp.Disconnect;
end;
if DeleteIt then
begin
//FErrStr:='原文件将北删除');
DeleteFile(DocFileName);
end;
finally
worddoc.free;
wordapp.Free;
end;
end;