unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Buttons, Word2000, OleServer;
type
TForm1 = class(TForm)
Word2000: TWordApplication;
WordDocument1: TWordDocument;
WordFont1: TWordFont;
WordParagraphFormat1: TWordParagraphFormat;
OD: TOpenDialog;
btnOpen: TBitBtn;
ed_PwdWrite: TLabeledEdit;
ed_PwdRead: TLabeledEdit;
procedure Word2000DocumentBeforeClose(Sender: TObject; var Doc,
Cancel: OleVariant);
procedure btnOpenClick(Sender: TObject);
private
{ Private declarations }
FFileName:String;
procedure OpenWord(FFileName,PwdWrite,PwdRead:OleVariant);
procedure SaveWord(FFileName,PwdWrite,PwdRead:OleVariant);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.OpenWord(FFileName, PwdWrite, PwdRead: OleVariant);
//pwdwrite=可写密码 ,pwdRead=只读密码
var
FWordFile:OleVariant;
FCommandBarName:OleVariant;
FCommandBar:OleVariant;
FShapeName:OleVariant;
FMoveCount,FExtend,Funit:OleVariant;
begin
Word2000.Disconnect;
try
Word2000.Connect;
except
ShowMessage('RPC远程接口出错!强烈建议去除杀毒软件的嵌入模式。');
Word2000.Disconnect;
Abort;
end;
try
//------------------------------WORD 2000------------------------
Word2000.Caption:='';
Word2000.Documents.OpenOld(FFileName,EmptyParam,EmptyParam,EmptyParam,
PwdRead,EmptyParam,EmptyParam,PwdWrite,EmptyParam,EmptyParam);
Word2000.Visible:=True;
Word2000.ShowMe;
Word2000.WindowState:=wdWindowStateMaximize;
except
on E:Exception do
begin
ShowMessage('出错了,可能需要关闭所有的非法程序...'+#13+'错误号码:'+E.Message);
Word2000.Disconnect;
Exit;
end;//on:E
end;
end;
procedure TForm1.SaveWord(FFileName, PwdWrite, PwdRead: OleVariant);
var
SaveChanges,OriginalFormat,RouteDocument:OleVariant;
FileName,FileFormat:OleVariant;
NoReset,Password:OleVariant;
begin
NoReset:=False;
//保护文档密码
//Word2000.Selection.Document.Protect(wdAllowOnlyComments,NoReset,Password);
SaveChanges:=wdDoNotSaveChanges;
OriginalFormat:=Unassigned;
RouteDocument:=Unassigned;
FileName:=MyFrame.WordFile;
FileFormat:=EmptyParam;
Word2000.Selection.Document.SaveAs(FileName,FileFormat,EmptyParam,PwdRead,EmptyParam,
PwdWrite,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam);
try
Word2000.Quit(SaveChanges,OriginalFormat,RouteDocument);
Word2000.Disconnect;
except
on e: Exception do
begin
ErrMessage('软件远程接口错误!'+#13+'错误号码:'+e.Message);
Word2000.Disconnect;
end;
end;
end;
procedure TForm1.Word2000DocumentBeforeClose(Sender: TObject; var Doc,
Cancel: OleVariant);
begin
SaveWord(FFileName,ed_PwdWrite.Text,ed_PwdRead.Text);
end;
procedure TForm1.btnOpenClick(Sender: TObject);
begin
if OD.Execute then
begin
FFileName:=OD.FileName;
OpenWord(FFileName,ed_PwdRead.Text,ed_PwdWrite.Text);
end;
end;
end.
word200就是wordapplication.OD是OpenDialog.
测试通过,