Delphi与c++builder操作Ole的区别

使用TOleContainer加载个word文档,隐藏缺省的会显示菜单栏和工具栏

1、c++builder方法

void __fastcall TForm1::Button2Click(TObject *Sender)
{
    OleContainer1->DestroyObject();

    OleContainer1->CreateObject("Word.Document", false);
    OleContainer1->DoVerb(ovInPlaceActivate);

    Variant vObj = OleContainer1->OleObject;
    int nCmdBarCount = vObj.OlePropertyGet("CommandBars").OlePropertyGet("Count");

    for (int i =0; i < nCmdBarCount; i++)
    {
        vObj.OlePropertyGet("CommandBars", i + 1).OlePropertySet("Enabled", false);
    }
}

2、Delphi方法

    nCmdBarCount := OLEObj.CommandBars.Count;
    for I := 0 to nCmdBarCount - 1 do
    begin
      OLEObj.CommandBars[i+1].Enabled := false;
    end;

即OlePropertyGet和OlePropertySet是c++builder专用的,Delphi中不能用。

但是TOleContainer针对Office2007总会有这样那样的问题,有点不尽人意,特别是在编辑Office文档时,如果只是显示使用TOleContainer还是可以的。

procedure TFm_Main.Button2Click(Sender: TObject);
var
  vObj:Variant;
  nCmdBarCount,i:integer;
  filePath:string;
begin
  try
    filePath := AppPath + '揭示板.xls';
    OleContainer1.DestroyObject();
    OleContainer1.CreateObjectFromFile(filePath,false);
    OleContainer1.DoVerb(ovInPlaceActivate);

    vObj := OleContainer1.OleObject;
    nCmdBarCount := vObj.CommandBars.Count;
    for i := 0 to nCmdBarCount - 1 do
      vObj.CommandBars[i+1].Enabled := false;
  except
    ShowMessage(Exception(ExceptObject()).Message);
  end;
end;


procedure TFm_Main.Button1Click(Sender: TObject);
var
  EApp:Variant;
  filePath:string;
begin
  EApp:=CreateOleObject('Excel.Application');
  filePath := AppPath + '揭示板.xls';
  Windows.SetParent(EApp.HWND,Panel19.Handle);
  EApp.Workbooks.Open(filePath);
  EApp.Visible:=true;
end;


参考:http://bbs.csdn.net/topics/320005950

http://bbs.csdn.net/topics/320118166

你可能感兴趣的:(Delphi)