请问怎样调用Word? 我在做一个小MIS,要求实现类似文档管理的功能,打算在软件中打开Word,请问应该如何实现?生成的DOC文档是保存为doc还是存在数据库里合适? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我们常用的函数有两个,WinExec 和 ShellExecute。 1) 使用 WinExec 函数 (属于 WinProcs单元) · 声明形式 UNIT WinExec(LPCSTR lpCmdLine, UINT uCmdShow); [例] var SDir:string; SetLength(SDir,144); GetWindowsDirectory(PChar(SDir),144); SetLength(SDir,StrLen(PChar(SDir))); SDir:=SDir+'\notepad.exe'+' '+savedialog1.FileName; WinExec(PChar(SDir), SW_SHOWMAXIMIZED); 注意:如果 SDir 不是有效路径不会提示错误。 [例] winexec('command.com /c copy *.* c:\',SW_Normal); [例] winexec('start abc.txt'); 2)使用 ShellExecute 函数(属于ShellAPI单元) 它的几个参数: · hwnd:窗体的句柄; · lpOperation:打开程序执行的操作,共预留有"open"、"explore"、 "print"三种方式,此参数可以省略,此时将依据打开的文件(lpFile)的类型执行相应的操作,比如:如果lpFile为一文本文件,那么将会在与该文件相关联的程序中打开它 · lpFile:文件名; · lpParamerters:打开文件时所需的参数; · lpDirectory:文件名所在的路径,当然,一般来说,在Windows中登记过的程序(如WinWord)不必提供此参数; · nShowCmd:打开文件后程序窗体如何显示。 (1)运行可执行文件 [例] 以"记事本"为例 procedure TForm1.OpenBtnClick(Sender:TObject); begin ShellExecute(handle,'open','notepad.exe',nil,nil,SW_ShowNormal); end; 此外,ShellExeCute() 还可以进行链接网络。 [例] procedure TForm1.HttpClick(Sender: TObject); begin ShellExecute(handle,'open','http://liangming.163.net', nil,nil,SW_ShowNormal); end; 谢谢剑客,如果不直接打开Word呢?例如OLE怎么实现?(我没有这方面的经验,不知说清了没有。) unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls,ComObj;type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);var MSWord: Variant;beginMSWord := CreateOLEObject('Word.Application');MSWord.Documents.Open(FileName:='D:\Temp\temp.doc', ReadOnly:=True);MSWord.Visible := 1;MSWord.ActiveDocument.Range(Start:=0, End:=0);MSWord.ActiveDocument.Range.InsertAfter(Text:='myvc');MSWord.ActiveDocument.Range.InsertParagraphAfter;MSWord.ActiveDocument.Range.Font.Size := 72;MSWord.ActiveDocument.Range.Font.Name := 'Arial';end;end.word中插入字符的例子 如何判断某文件是否已打开 delphi的乐趣题 如何windows程序菜单中创建多层的快捷方式,高分送。 如何在B程序中获取A程序中的某些内容(如StringGrid中) 如何对adoquery查询到的数据进行操作? deleterecords的用法???? 如何双击ListBox中的文件名(如:D:\test.xls),即可打开并编辑该文件? delphi中的控件有没有事件提示 求助软件开发需求说明书、详细设计说明书例子 制作DELPHI的纯中文安装程序?! 设置字体颜色的问题? 请教关于ADSL和RAS的问题
· 声明形式 UNIT WinExec(LPCSTR lpCmdLine, UINT uCmdShow);
[例] var SDir:string;
SetLength(SDir,144);
GetWindowsDirectory(PChar(SDir),144);
SetLength(SDir,StrLen(PChar(SDir)));
SDir:=SDir+'\notepad.exe'+' '+savedialog1.FileName;
WinExec(PChar(SDir), SW_SHOWMAXIMIZED);
注意:如果 SDir 不是有效路径不会提示错误。
[例] winexec('command.com /c copy *.* c:\',SW_Normal);
[例] winexec('start abc.txt');
2)使用 ShellExecute 函数(属于ShellAPI单元)
它的几个参数:
· hwnd:窗体的句柄;
· lpOperation:打开程序执行的操作,共预留有"open"、"explore"、 "print"三种方式,此参数可以省略,此时将依据打开的文件(lpFile)的类型执行相应的操作,比如:如果lpFile为一文本文件,那么将会在与该文件相关联的程序中打开它
· lpFile:文件名;
· lpParamerters:打开文件时所需的参数;
· lpDirectory:文件名所在的路径,当然,一般来说,在Windows中登记过的程序(如WinWord)不必提供此参数;
· nShowCmd:打开文件后程序窗体如何显示。
[例] 以"记事本"为例
procedure TForm1.OpenBtnClick(Sender:TObject);
begin ShellExecute(handle,'open','notepad.exe',nil,nil,SW_ShowNormal); end;
此外,ShellExeCute() 还可以进行链接网络。
[例] procedure TForm1.HttpClick(Sender: TObject);
begin
ShellExecute(handle,'open','http://liangming.163.net', nil,nil,SW_ShowNormal);
end;
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls,ComObj;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
var MSWord: Variant;
begin
MSWord := CreateOLEObject('Word.Application');
MSWord.Documents.Open(FileName:='D:\Temp\temp.doc', ReadOnly:=True);
MSWord.Visible := 1;
MSWord.ActiveDocument.Range(Start:=0, End:=0);
MSWord.ActiveDocument.Range.InsertAfter(Text:='myvc');
MSWord.ActiveDocument.Range.InsertParagraphAfter;
MSWord.ActiveDocument.Range.Font.Size := 72;
MSWord.ActiveDocument.Range.Font.Name := 'Arial';
end;end.word中插入字符的例子