uses comobj; var w:variant;w:=createoleobject('word.application'); w.document.open('c:\aa.doc');
Delphi 下有個demo看一看吧.很好的,可以打開,新建.............
var V: OleVariant; F : TIniFile; spath : string; // h:hwnd; begin { h := FindWindow(PChar('OpusApp'), nil); if H > 0 then begin end;} try F := TiniFile.Create(ExtractFilePath(Application.ExeName)+'tos.ini'); spath := F.ReadString('FAQ','PATH',''); if spath = '' then begin if not CreateDir('C:\FAQ') then begin ShowMessage('Created directory failed'); exit; end else F.WriteString('FAQ','PATH','C:\FAQ'); end; finally F.Free; end; OpenDialog1.InitialDir := spath; if OpenDialog1.Execute then begin try Screen.Cursor := crHourGlass; V := CreateOleObject('word.Application'); V.Visible := true; V.Caption := 'FAQ Document'; V.Documents.Add(EmptyParam, false,0,0); V.Documents.Open(OpenDialog1.FileName); Screen.Cursor := crDefault; except Screen.Cursor := crDefault; ShowMessage('Not found World,Microsoft world is Installed?'); exit; end; end else begin if Application.MessageBox('Are you want to create a new document?',Pchar(App_title),MB_YESNO)=IDNO then exit; try Screen.Cursor := crHourGlass; V := CreateOleObject('Word.Application'); V.Visible := true; V.Caption := 'FAQ Document'; V.Documents.Add(EmptyParam, false,0,0); V.Documents.add; Screen.Cursor := crDefault; except Screen.Cursor := crDefault; ShowMessage('Not found World,Microsoft world is Installed?'); exit; end; end; end;
procedure TForm1.Button1Click(Sender: Tobject); var MSWord: Variant; begin MSWord := CreateOLEObject('Word.Application');//连接Word MSWord.Documents.Open(FileName:='d:\test.doc', ReadOnly:=True);//打开外部Word文档 MSWord.Visible := 1;//是否显示文件编辑 end;
var w:variant;w:=createoleobject('word.application');
w.document.open('c:\aa.doc');
V: OleVariant;
F : TIniFile;
spath : string;
// h:hwnd;
begin
{ h := FindWindow(PChar('OpusApp'), nil);
if H > 0 then
begin
end;} try
F := TiniFile.Create(ExtractFilePath(Application.ExeName)+'tos.ini');
spath := F.ReadString('FAQ','PATH','');
if spath = '' then
begin
if not CreateDir('C:\FAQ') then
begin
ShowMessage('Created directory failed');
exit;
end
else F.WriteString('FAQ','PATH','C:\FAQ');
end;
finally
F.Free;
end;
OpenDialog1.InitialDir := spath;
if OpenDialog1.Execute then
begin
try
Screen.Cursor := crHourGlass;
V := CreateOleObject('word.Application');
V.Visible := true;
V.Caption := 'FAQ Document';
V.Documents.Add(EmptyParam, false,0,0);
V.Documents.Open(OpenDialog1.FileName);
Screen.Cursor := crDefault;
except
Screen.Cursor := crDefault;
ShowMessage('Not found World,Microsoft world is Installed?');
exit;
end;
end
else
begin
if Application.MessageBox('Are you want to create a new document?',Pchar(App_title),MB_YESNO)=IDNO then
exit;
try
Screen.Cursor := crHourGlass;
V := CreateOleObject('Word.Application');
V.Visible := true;
V.Caption := 'FAQ Document';
V.Documents.Add(EmptyParam, false,0,0);
V.Documents.add;
Screen.Cursor := crDefault;
except
Screen.Cursor := crDefault;
ShowMessage('Not found World,Microsoft world is Installed?');
exit;
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;