调用各类文档(100分)

  • 主题发起人 主题发起人 yj
  • 开始时间 开始时间
Y

yj

Unregistered / Unconfirmed
GUEST, unregistred user!
如何在DELPHI程序中调用word文档及execl文挡,并能在delphi程序中进行编辑。
 
用OLEContainer控件
 
Delphi 5中提供了Server控件
用来与Office系列通过COM接口实现调用
具体可以去查VBA的帮助
 
Delphi5已经进行了封装;
以下是一个e.g:
Delphi自带了一个简单的Word例题,但功能太简单。通过查找VBA的说明,再对照Delphi的VCL,如下代码实现了基本的公文管理功能。

启动Word时用如下代码:
begin
try
Wordapplication.Connect;
except
MessageDlg('Word may not be installed', mtError, [mbOk], 0);
Abort;
end;
Wordapplication.Visible := True;
WordApplication.Caption := 'Delphi automation';
end;

关闭Word用如下代码。如果想保存Doc文件,请修改SaveChanges变量的内容:
var
SaveChanges, OriginalFormat, RouteDocument: OleVariant;
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打开一个指定的文件,需要先放置OpenDialog,然后调用WordApplication.Documents.Open:
var
ItemIndex :OleVariant;
FileName, ConfirmConversions, ReadOnly, AddToRecentFiles,
PasswordDocument, PasswordTemplate, Revert,
WritePasswordDocument, WritePasswordTemplate, Format: OleVariant;
begin
if not dlgOpen.Execute then
Exit;

{Open document}
FileName := dlgOpen.FileName;
ConfirmConversions := False;
ReadOnly := False;
AddToRecentFiles := False;
PasswordDocument := '';
PasswordTemplate := '';
Revert := True;
WritePasswordDocument := '';
WritePasswordTemplate := '';
Format := wdOpenFormatDocument;

WordApplication.Documents.Open( FileName, ConfirmConversions,
ReadOnly, AddToRecentFiles, PasswordDocument, PasswordTemplate,
Revert, WritePasswordDocument, WritePasswordTemplate, Format );

{Assign WordDocument component}
ItemIndex := 1;
WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex));

{Turn Spell checking of because it takes a long time if enabled and slows down Winword}
WordApplication.Options.CheckSpellingAsYouType := False;
WordApplication.Options.CheckGrammarAsYouType := False;
end;

让Word替换标记字符串要使用WordDocument.Range.Find.Execute,这里用Delphi替换了<#Name>:
var
FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike,
MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, Replace: OleVariant;
begin
FindText := '<#Name>';
MatchCase := False;
MatchWholeWord := True;
MatchWildcards := False;
MatchSoundsLike := False;
MatchAllWordForms := False;
Forward := True;
Wrap := wdFindContinue;
Format := False;
ReplaceWith := 'Delphi';
Replace := True;

WordDocument.Range.Find.Execute( FindText, MatchCase, MatchWholeWord,
MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward,
Wrap, Format, ReplaceWith, Replace );

end;

上面这4段代码完成了公文管理的基本功能,再把它和数据库结合起来,就可以开发一个与Lotus Notes类似的产品了。
 
接受答案了.
 
后退
顶部