procedure TForm1.Button1Click(Sender: TObject); begin if SaveDialog1.Execute then begin if FileExists(SaveDialog1.FileName) then raise Exception.Create('File already exists. Cannot overwrite.') else OleContainer1.SaveAsDocument(SaveDialog1.FileName); end; end;
procedure TForm1.Button2Click(Sender: TObject); var Word,Bar:OleVariant; i:Integer; begin if OpenDialog1.Execute then begin //CreateObjectFromFile第二个参数:True显示为一个图标,False显示为文件 OleContainer1.CreateObjectFromFile(OpenDialog1.FileName,False); OleContainer1.Run; //启动OLE对象,如果这句没有会出现"Interface not Supported"错误提示 OleContainer1.DoVerb(ovShow); //让Word文档处于编辑状态 Word:=OleContainer1.OleObject.Application; //获取Word应用程序句柄 for i:=1 to Word.CommandBars.Count do begin Bar:=Word.CommandBars.Item[i]; // if Bar.Visible then ListBox1.Items.Add(VarToStr(Bar.Name)); end; Word.CommandBars['Standard'].Visible:=False; //隐藏”常用”工具条 end; end;
procedure TForm1.FormCreate(Sender: TObject); begin OpenDialog1.Filter:='Documents (*.doc)|*.Doc|Any File (*.*)|*.*'; SaveDialog1.Filter:='Documents (*.doc)|*.Doc|Any File (*.*)|*.*'; Button1.Caption:='Save File'; Button2.Caption:='Open File'; end; |
|