VC ++以编程方式添加excel OLEObject?

时间:2013-08-21 16:02:31

标签: visual-c++ excel-interop createoleobject

我在使用Interop库将VCObject添加到VC ++中的Excel工作表时遇到问题。 编译器是VC ++ 2010 express。 Interop库适用于Office 2007。

这是宣言:

#define XL Microsfot::Office::Interop::Excel
...
XL::Application^ xlApp;
XL::Workbook^ xlWb;
XL::Worksheet^ xlWs;

以下是控制excel的代码:

xlApp = gcnew XL::ApplicationClass();
xlWb = xlApp->Workbooks->Add(Type::Missing);
xlWs = safe_cast<XL::Worksheet^>(xlApp->ActiveSheet);

xlApp->Visible = true;

xlWs->Cells[1, 1] = "OMG I can put stuff in cells no problem";

//this line generates an error C2227: left of '->Add' must point to class/struct/union/generic type
xlWs->OLEObjects->Add("somefile.someext", false, false);

我在excel中进行了宏录制以添加OLEObject,这是它生成的VB代码:

ActiveSheet.OLEObjects.Add(Filename:="C:\somefile.someext", Link:=False, DisplayAsIcon:=False).Select

所以在VB中有一个OLE方法的Add方法,但在C ++中没有? 我做错了什么?

1 个答案:

答案 0 :(得分:0)

我明白了!

XL::OLEObjects^ xlObjs = safe_cast<XL::OLEObjects^>(xlWs->OLEObjects(Type::Missing));
xlObjs->Add(Type::Missing, "somefile.someext", false, false, Type::Missing, Type::Missing, Type::Missing, 0, 0, 20, 20);