I been having this weired embedded excel problem. I am trying to create a commandbar control in embedded excel with some buttons in it. It is able to create everything when I am creating an Excel.Application. However, I am not able to create any commandbar or button or anything when I am creating Excel.sheet / embedded excel. I am using the following turorial: http://support.microsoft.com/kb/q194906/ , which definetly works in case of Excel.Application. But the code below doesn't work.
TRY
{
//Get the document associated with this view, and be sure that it is
//valid.
CSG3GDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// Create a new item associated with this document, and be sure that it is valid.
pItem = new CSG3GCntrItem(pDoc);
ASSERT_VALID(pItem);
// Get a Class ID for the Excel sheet. This is used in creation.
// Create the Excel embedded item.
if(!pItem->CreateNewItem(clsid))
AfxThrowMemoryException();
// Make sure the new CContainerItem is valid.
ASSERT_VALID(pItem);
// Start the server to edit the item.
pItem->DoVerb(OLEIVERB_SHOW, this);
/// save Control pointer
m_pSelection = pItem;
LPDISPATCH lpDisp = pItem->GetIDispatch();
_Application ap; ap.AttachDispatch(lpDisp);
ap = ap.GetApplication();
pExcelEvHandle = new CExcelEvHandle(this);
pExcelEvHandle->AddRef();
pExcelEvHandle->AttachToSource(ap.m_lpDispatch);
Col = r.GetColumn();
HRESULT hr;
VARIANT vResult;
char buf[1024]; // General purpose message buffer
// Convenient values declared as ColeVariants.
COleVariant covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
long lCount = 0;
CommandBarControls oNewControls;
_CommandBarButton oNewButton2, oButton, oNewButton1;
CommandBar oNewBar;
//build a new commandbar for excel and add the commandbar to xl collection
hr = ap.m_lpDispatch->GetIDsOfNames(IID_NULL, &strCBs, 1, LOCALE_SYSTEM_DEFAULT, &dispID);
if (FAILED(hr))
{
sprintf(buf,"Failed to GetIDsOfNames() ... Error = %08lx",
(long)hr);
AfxMessageBox(buf,MB_SETFOREGROUND);
}
//Get a dispatch pointer to commandbars. Use that of running application's existing menu
//confuguration. "vresult" is a VARIANT. It is declared above.
ap.InvokeHelper(dispID, //commandbars in this case
DISPATCH_METHOD | DISPATCH_PROPERTYGET,
VT_VARIANT, //type of return value
(void*)&vResult, //address of var reciving IDispatch of CmdBrs
NULL); //Pointer to parameters string
_CommandBars cbs(vResult.pdispVal); //construct the commandBars object and attach the IDispatch
//pointer to it
lCount = cbs.GetCount(); // Word has 92!!??
// MSOffice reconfigures for each
// user-application.
//CString tmp; tmp.Format("LCount=%d", lCount); AfxMessageBox(tmp);
vResult.pdispVal = cbs.GetActiveMenuBar(); //returns a LPDISPATCH pointer of the commandbar object that
//represents the active menu bar in the container application; that is MS office configuration
CommandBar oBar(vResult.pdispVal); // Construct a new
// CommandBar object
// & attach the LPDispatch
// of the active menu bar
VARIANT vName;
vName.vt = VT_BSTR;
vName.bstrVal = SysAllocString(L"Groups Command Bar"); //Variant for the name of new bar
oNewControls = oBar.GetControls();
VARIANT vPosition;
vPosition.vt = VT_I2;
vPosition.iVal = 1; //4 = Floating; 0 = Left;
//Variant for position of new bar
oBar = (CommandBarPopup)cbs.Add(vName, vPosition, covFalse, covTrue);
//CommandBar oNewBar;
//AfxMessageBox("Now adding new bar to cbs collection");
try {
oNewBar = cbs.Add(vName, //const Variant Name = Groups Command Bar
vPosition, //const Variant Position = At top
covFalse, //const variant (replace) Menubar
covTrue); //const variant temporary
It would be better if you could use code tags ...and also if the code doesn't work then you should upload a sample project that we can try to figure it out why doesn't work.
Regards,
Gili
Please use code tags [code] [/code]
We would change the world, but God won't give us the sourcecode..
Undocumented futures are fun and useful....
_________
Gili
Attached is the code for EmbeddedExcelView.h and EmbeddedExcelView.cpp and in function OnFileInsertexcelcontrol32772() is where I am the problem, when i try to add any type of controls (i.e. commandbars, commandbarbutton, etc) into excel sheet. So just follow the article on the following site: http://support.microsoft.com/kb/q184663/ to create an embedded excel. Then make sure to add mso9.h and mso9.cpp into your project to create any kind of office menus/toolbars/commanbars, etc. Follow the article on the following site: http://support.microsoft.com/kb/q194906/ in step 2 to add mso9.h/mso9.cpp. I am not able to attach the whole project since some of the files are really big. Hopefully someone can figure this problem out for me soon.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.