分享

TOleContainer

 quasiceo 2013-12-25

TOleContainer-CreateObjectFromFile、Run、DoVerb、SaveToDocument  

2010-12-25 19:15:54|  分类: Delphi |字号 订阅

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;

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多