Article ID: 137357
Article Last Modified on 11/21/2006
*******Begin Excerpt*******
switch (selType)
{
case linkToFile:
// link to file selected
ASSERT(m_szFileName[0] != 0);
bResult=pNewItem->CreateLinkFromFile(m_szFileName);
break;
case insertFromFile:
// insert file selected
ASSERT(m_szFileName[0] != 0);
bResult=pNewItem->CreateFromFile(m_szFileName);
break;
default:
// otherwise must be create new
ASSERT(selType == createNewItem);
bResult=pNewItem->CreateNewItem(m_io.clsid);
break;
}
*******End Excerpt*******
This code features a switch structure whose logic flow is controlled by
the selType set by the user interaction with the COleInsertDialog dialog
box.
/* Compile options needed : None
*/
void CMyView::OnInsertObject()
{
BeginWaitCursor();
CMyOleClientItem* pItem = NULL;
TRY
{
// Create new item connected to this document.
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pItem = new CMyOleClientItem(pDoc);
ASSERT_VALID(pItem);
// Get Class ID for Excel sheet
// This is used in creation
CLSID clsid;
This following line should be like this:
if(FAILED(::CLSIDFromProgID(OLESTR("Excel.Sheet"),&clsid)))
AfxThrowMemoryException();
if(FAILED(::CLSIDFromProgID("Excel.Sheet",&clsid)))
AfxThrowMemoryException();
// Create the Excel embedded item
if(!pItem->CreateNewItem(clsid))
AfxThrowMemoryException(); // any exception will do
ASSERT_VALID(pItem);
// Launch the server to edit the item.
pItem->DoVerb(OLEIVERB_SHOW, this);
ASSERT_VALID(pItem);
// As an arbitrary user interface design, this sets the
// selection to the last item inserted.
// TODO: reimplement selection as appropriate for your
// application
m_pSelection = pItem; // set selection to last inserted item
pDoc->UpdateAllViews(NULL);
}
CATCH(CException, e)
{
if (pItem != NULL)
{
ASSERT_VALID(pItem);
pItem->Delete();
}
AfxMessageBox(IDP_FAILED_TO_CREATE);
}
END_CATCH
EndWaitCursor();
}
Keywords: kbcode kbdlg kbhowto KB137357