简体   繁体   中英

VC++ programmatically add excel OLEObject?

I'm having trouble adding an OLEObject to an excel worksheet in VC++ using Interop libraries. Compiler is VC++ 2010 express. Interop libraries are for Office 2007.

Here's the declaration:

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

Here's the code to control 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);

I did a macro recording in excel to add an OLEObject and this is the VB code it generates:

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

So there is an Add method to OLEObjects in VB but not in C++? What am I doing wrong?

I got it!

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM