怎样在delphi 中打开word文件?即调用word! 给点代码吧! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1、如果知道WinWord的安装为止,那么使用WinExec函数!!!2、如果不知道WinWord的安装为止,那么使用ShellExecute函数!!!具体请参考MSDN!!! unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Word2000, Office2000, StdCtrls;type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); private procedure AppClose(Sender:TObject); public WordApp: TWordApplication; WordDoc: TWordDocument; end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.AppClose(Sender: TObject);begin Form1.Close;end;procedure TForm1.Button1Click(Sender: TObject);var Template,NewTemplate,DocumentType,Visible: OleVariant; OldCursor: TCursor; FileName,FileFormat,LockComments,Password, AddToRecentFiles,WritePassword,ReadOnlyRecommended, EmbedTrueTypeFonts,SaveNativePictureFormat, SaveFormsData,SaveAsAOCELetter: OleVariant;begin OldCursor:=Screen.Cursor; Screen.Cursor:=crHourglass; WordApp:=TWordApplication.Create(Application); WordApp.Visible:=True; WordApp.OnQuit:=AppClose; WordApp.Caption:='Delphi Control Word'; Template:=''; NewTemplate:=False; DocumentType:=wdNewBlankDocument; Visible:=True; if WordApp.Documents.Count=0 then WordApp.Documents.Add(Template,NewTemplate,DocumentType,Visible); WordDoc:=TWordDocument(WordApp.ActiveDocument); WordApp.Activate; FileName:='c:\Sample.doc'; FileFormat:=wdFormatDocument; LockComments:=False; Password:=''; AddToRecentFiles:=True; WritePassword:=''; ReadOnlyRecommended:=false; EmbedTrueTypeFonts:=False; SaveNativePictureFormat:=False; SaveFormsData:=False; SaveAsAOCELetter:=false; WordApp.ActiveDocument.SaveAs(FileName, FileFormat, LockComments, Password, AddToRecentFiles, WritePassword, ReadOnlyRecommended, EmbedTrueTypeFonts, SaveNativePictureFormat, SaveFormsData, SaveAsAOCELetter); WordApp.ActiveDocument.Save; WordApp.ActiveDocument.Saved:=True; Screen.Cursor:=OldCursor;end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);var SaveChanges, OriginalFormat, RouteDocument: OleVariant;begin SaveChanges:=wdPromptToSaveChanges; OriginalFormat:=wdWordDocument; RouteDocument:=False; if Assigned(WordApp) then try WordApp.Quit(SaveChanges, OriginalFormat, RouteDocument); WordApp.Free; except end;end;end. procedure TForm1.Button1Click(Sender: Tobject);var MSWord: Variant;beginMSWord := CreateOLEObject('Word.Application');//连接WordMSWord.Documents.Open(FileName:='d:\test.doc', ReadOnly:=True);//打开外部Word文档MSWord.Visible := 1;//是否显示文件编辑end; CreateOLEObject('Word.Application');//连接Word系统提示还没有定义!怎么引用它啊? CreateOLEObject('Word.Application');//连接Word系统提示还没有定义!怎么引用它啊? CreateOLEObject('Word.Application');//连接Word系统提示还没有定义!怎么引用它啊? 增加引用声明:在这个位置增加:implementation uses comobj; 给你点代码,这是我前几天在公司作的,希望与大家共勉。启动Word时用如下代码: begintry 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 ); 什么情况下使用多线程? 散分 我是delphi的初学者,请各位帮忙,Tedit控件,怎样控制它的输入值,只输入数字或只输入英数字,谢谢 急求DSpack安装问题 RichEdit怎样实现多次Undo 哪有可用的VF6.0下载 怎样得到目前屏幕上最上面的窗口,就是alt+printscreen能抓住的那个窗口。 用listview排序出现的问题 Delphi7安装时的问题 SQL语句中的路径"库名.表名"中,库名是怎样规定的? 请教大家一个比较大的问题! 一个很简单的问题,请帮手,在线等…
2、如果不知道WinWord的安装为止,那么使用ShellExecute函数!!!具体请参考MSDN!!!
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Word2000, Office2000, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
procedure AppClose(Sender:TObject);
public
WordApp: TWordApplication;
WordDoc: TWordDocument;
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.AppClose(Sender: TObject);
begin
Form1.Close;
end;procedure TForm1.Button1Click(Sender: TObject);
var
Template,NewTemplate,DocumentType,Visible: OleVariant;
OldCursor: TCursor; FileName,FileFormat,LockComments,Password,
AddToRecentFiles,WritePassword,ReadOnlyRecommended,
EmbedTrueTypeFonts,SaveNativePictureFormat,
SaveFormsData,SaveAsAOCELetter: OleVariant;begin
OldCursor:=Screen.Cursor;
Screen.Cursor:=crHourglass; WordApp:=TWordApplication.Create(Application);
WordApp.Visible:=True;
WordApp.OnQuit:=AppClose;
WordApp.Caption:='Delphi Control Word'; Template:='';
NewTemplate:=False;
DocumentType:=wdNewBlankDocument;
Visible:=True;
if WordApp.Documents.Count=0 then
WordApp.Documents.Add(Template,NewTemplate,DocumentType,Visible); WordDoc:=TWordDocument(WordApp.ActiveDocument);
WordApp.Activate;
FileName:='c:\Sample.doc';
FileFormat:=wdFormatDocument;
LockComments:=False;
Password:='';
AddToRecentFiles:=True;
WritePassword:='';
ReadOnlyRecommended:=false;
EmbedTrueTypeFonts:=False;
SaveNativePictureFormat:=False;
SaveFormsData:=False;
SaveAsAOCELetter:=false;
WordApp.ActiveDocument.SaveAs(FileName,
FileFormat,
LockComments,
Password,
AddToRecentFiles,
WritePassword,
ReadOnlyRecommended,
EmbedTrueTypeFonts,
SaveNativePictureFormat,
SaveFormsData,
SaveAsAOCELetter); WordApp.ActiveDocument.Save;
WordApp.ActiveDocument.Saved:=True; Screen.Cursor:=OldCursor;
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
SaveChanges, OriginalFormat, RouteDocument: OleVariant;
begin
SaveChanges:=wdPromptToSaveChanges;
OriginalFormat:=wdWordDocument;
RouteDocument:=False;
if Assigned(WordApp) then
try
WordApp.Quit(SaveChanges, OriginalFormat, RouteDocument);
WordApp.Free;
except
end;
end;end.
var MSWord: Variant;
begin
MSWord := CreateOLEObject('Word.Application');//连接Word
MSWord.Documents.Open(FileName:='d:\test.doc', ReadOnly:=True);//打开外部Word文档
MSWord.Visible := 1;//是否显示文件编辑
end;
系统提示还没有定义!怎么引用它啊?
系统提示还没有定义!怎么引用它啊?
系统提示还没有定义!怎么引用它啊?
在这个位置增加:
implementation
uses comobj;
启动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 );