M
Mardigin
CAn someone please help me understand why cbPop.Delete
(covTrue);
Causes my application to crash!
I get the following error message...
"Failed to create object. Make sure the object is entered
in the system registry."
However when I remove cbPop.Delete(covTrue); everything
works perfectly.
My aim is to remove the insert Command Bar Popup. I have
built an MDI application with a container to embed excel.
This function gets called from onInsert().
//////////////////// BEGIN CODE ///////////////////
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND,
VT_ERROR);
HRESULT hr;
DISPID dispID = 0;
LPDISPATCH lpDisp = 0;
VARIANT vResult; // A Struct with a pdispVal
member
long cCount = 0;
char buf[2048]; // General purpose message
buffers
OLECHAR *strCBs = L"CommandBars";
// Object declarations.
XL::_Application app; // The XL prefix specifies
the namerange.
XL::Workbooks oWorkbooks;
XL::_Workbook oBook;
XL::Worksheets oWorksheets;
XL::_Worksheet oSheet;
XL::Shapes oShapes;
XL::Shape oShape;
CBPReportCntrItem *pItem = NULL;
CBPReportDoc *pDoc = NULL;
CLSID clsid;
TRY
{
//Get the document associated with this view, and be sure
//it's valid.
pDoc = GetDocument();
ASSERT_VALID(pDoc);
//Create a new item associated with this document, and be
//sure it's valid.
pItem = new CBPReportCntrItem(pDoc);
ASSERT_VALID(pItem);
// Get Class ID for Excel sheet.
// This is used in creation.
if(FAILED:CLSIDFromProgID
(L"Excel.sheet",&clsid)))
{
AfxMessageBox("Failed to create
Excel.sheet");
//Any exception will do. We just
need to break out of the
//TRY statement.
AfxThrowMemoryException();
}
// Create the Excel embedded item.
if(!pItem->CreateNewItem(clsid))
{
AfxMessageBox("Failed to create
the excel embeded item");
//Any exception will do. We just need to break out of the
//TRY statement.
AfxThrowMemoryException();
}
//Make sure the new CContainerItem is valid.
ASSERT_VALID(pItem);
// Launch the server to edit the item.
pItem->DoVerb(OLEIVERB_SHOW, this);
// As an arbitrary user interface design, this sets the
// selection to the last item inserted.
m_pSelection = pItem; // set selection to last
inserted item
pDoc->UpdateAllViews(NULL);
//Query for the dispatch pointer for the embedded object.
//In this case, this is the Excel worksheet.
lpDisp = pItem->GetIDispatch();
oBook.AttachDispatch(lpDisp, TRUE);
oWorksheets = oBook.GetWorksheets();
oSheet = oWorksheets.GetItem(COleVariant((short)
1));
oSheet.Activate(); // Make the sheet selection work
CommandBars cmdBar = oBook.GetCommandBars
();
vResult.pdispVal = cmdBar.FindControl(
COleVariant((short)10),
// msoControlPopup type
//MsoOLEMenuGroup msoOleMenuGroupEdit,
COleVariant((long)30005),
covOptional,
covOptional);
sprintf(buf, "IDispatch pointer of the
msoControlPopup is %08lx",
(long)vResult.pdispVal);
AfxMessageBox(buf,MB_SETFOREGROUND);
// Construct a Popup Control object and
// Attach the IDispatch pointer of CommandBarPopup
// to that new control object.
CommandBarControl cbPop(vResult.pdispVal);
CString ccCaption = cbPop.GetCaption();
long iiType = cbPop.GetType();
long iiIndex = cbPop.GetIndex();
long iiId = cbPop.GetId();
long iiusage = cbPop.GetOLEUsage();
long iilong = cbPop.GetTop();
sprintf(buf,
"Caption of ControlPopup is %s\n"
"'Type' property of ControlPopup is %d\n"
"'Index' property of ControlPopup is %d\n"
"'Id' property of ControlPopup is %d\n"
"'OLEUsage' property of ControlPopup is %
d\n"
"'Top' property of ControlPopup is %d\n",
(LPCTSTR)ccCaption, iiType, iiIndex, iiId,
iiusage);
AfxMessageBox(buf,MB_SETFOREGROUND);
sprintf(buf, "Watch the %s menu pad disappear.",
(LPCTSTR)ccCaption);
AfxMessageBox(buf, MB_SETFOREGROUND);
//This does not visibly change the caption
//yet calling the getCaption returns "Boo"
cbPop.SetCaption("Boo");
AfxMessageBox(cbPop.GetCaption());
//Does not appear to do anything
cbPop.SetVisible(FALSE);
//Cause the application to throw an exception
cbPop.Delete(covTrue);
//////////////////// END CODE ///////////////////
AfxMessageBox results...
Caption of ControlPopup is &insert
'Type' property of ControlPopup is 10
'Index' property of ControlPopup is 4
'Id' property of ControlPopup is 30005
'OLEUsage' property of ControlPopup is 1
'Top' property of ControlPopup is 1243224
(covTrue);
Causes my application to crash!
I get the following error message...
"Failed to create object. Make sure the object is entered
in the system registry."
However when I remove cbPop.Delete(covTrue); everything
works perfectly.
My aim is to remove the insert Command Bar Popup. I have
built an MDI application with a container to embed excel.
This function gets called from onInsert().
//////////////////// BEGIN CODE ///////////////////
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND,
VT_ERROR);
HRESULT hr;
DISPID dispID = 0;
LPDISPATCH lpDisp = 0;
VARIANT vResult; // A Struct with a pdispVal
member
long cCount = 0;
char buf[2048]; // General purpose message
buffers
OLECHAR *strCBs = L"CommandBars";
// Object declarations.
XL::_Application app; // The XL prefix specifies
the namerange.
XL::Workbooks oWorkbooks;
XL::_Workbook oBook;
XL::Worksheets oWorksheets;
XL::_Worksheet oSheet;
XL::Shapes oShapes;
XL::Shape oShape;
CBPReportCntrItem *pItem = NULL;
CBPReportDoc *pDoc = NULL;
CLSID clsid;
TRY
{
//Get the document associated with this view, and be sure
//it's valid.
pDoc = GetDocument();
ASSERT_VALID(pDoc);
//Create a new item associated with this document, and be
//sure it's valid.
pItem = new CBPReportCntrItem(pDoc);
ASSERT_VALID(pItem);
// Get Class ID for Excel sheet.
// This is used in creation.
if(FAILED:CLSIDFromProgID
(L"Excel.sheet",&clsid)))
{
AfxMessageBox("Failed to create
Excel.sheet");
//Any exception will do. We just
need to break out of the
//TRY statement.
AfxThrowMemoryException();
}
// Create the Excel embedded item.
if(!pItem->CreateNewItem(clsid))
{
AfxMessageBox("Failed to create
the excel embeded item");
//Any exception will do. We just need to break out of the
//TRY statement.
AfxThrowMemoryException();
}
//Make sure the new CContainerItem is valid.
ASSERT_VALID(pItem);
// Launch the server to edit the item.
pItem->DoVerb(OLEIVERB_SHOW, this);
// As an arbitrary user interface design, this sets the
// selection to the last item inserted.
m_pSelection = pItem; // set selection to last
inserted item
pDoc->UpdateAllViews(NULL);
//Query for the dispatch pointer for the embedded object.
//In this case, this is the Excel worksheet.
lpDisp = pItem->GetIDispatch();
oBook.AttachDispatch(lpDisp, TRUE);
oWorksheets = oBook.GetWorksheets();
oSheet = oWorksheets.GetItem(COleVariant((short)
1));
oSheet.Activate(); // Make the sheet selection work
CommandBars cmdBar = oBook.GetCommandBars
();
vResult.pdispVal = cmdBar.FindControl(
COleVariant((short)10),
// msoControlPopup type
//MsoOLEMenuGroup msoOleMenuGroupEdit,
COleVariant((long)30005),
covOptional,
covOptional);
sprintf(buf, "IDispatch pointer of the
msoControlPopup is %08lx",
(long)vResult.pdispVal);
AfxMessageBox(buf,MB_SETFOREGROUND);
// Construct a Popup Control object and
// Attach the IDispatch pointer of CommandBarPopup
// to that new control object.
CommandBarControl cbPop(vResult.pdispVal);
CString ccCaption = cbPop.GetCaption();
long iiType = cbPop.GetType();
long iiIndex = cbPop.GetIndex();
long iiId = cbPop.GetId();
long iiusage = cbPop.GetOLEUsage();
long iilong = cbPop.GetTop();
sprintf(buf,
"Caption of ControlPopup is %s\n"
"'Type' property of ControlPopup is %d\n"
"'Index' property of ControlPopup is %d\n"
"'Id' property of ControlPopup is %d\n"
"'OLEUsage' property of ControlPopup is %
d\n"
"'Top' property of ControlPopup is %d\n",
(LPCTSTR)ccCaption, iiType, iiIndex, iiId,
iiusage);
AfxMessageBox(buf,MB_SETFOREGROUND);
sprintf(buf, "Watch the %s menu pad disappear.",
(LPCTSTR)ccCaption);
AfxMessageBox(buf, MB_SETFOREGROUND);
//This does not visibly change the caption
//yet calling the getCaption returns "Boo"
cbPop.SetCaption("Boo");
AfxMessageBox(cbPop.GetCaption());
//Does not appear to do anything
cbPop.SetVisible(FALSE);
//Cause the application to throw an exception
cbPop.Delete(covTrue);
//////////////////// END CODE ///////////////////
AfxMessageBox results...
Caption of ControlPopup is &insert
'Type' property of ControlPopup is 10
'Index' property of ControlPopup is 4
'Id' property of ControlPopup is 30005
'OLEUsage' property of ControlPopup is 1
'Top' property of ControlPopup is 1243224