如何在多文档、使用OLE内嵌Excel的情况下打开已经存在的xls文件?
我按msdn的提示想在OLE内嵌Excel的情况下打开已经存在的xls文件,老是出问题,我的代码如下,大家帮我看看好么
CDemoCntrItem* pItem = NULL;
TRY
{
//Get the document associated with this view, and be sure it's
//valid.
CDemoDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
//Create a new item associated with this document, and be sure
//it's valid.
pItem = new CDemoCntrItem(pDoc);
ASSERT_VALID(pItem);
// Get Class ID for Excel sheet.
// This is used in creation.
CLSID clsid;
if(FAILED(::CLSIDFromProgID(L"Excel.sheet",&clsid)))
//Any exception will do. We just need to break out of the
//TRY statement.
AfxThrowMemoryException();
// Create the Excel embedded item.
if(!pItem->CreateNewItem(clsid))
//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.
LPDISPATCH lpDisp;
lpDisp = pItem->GetIDispatch();
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
_Worksheet ws;
_Application app;
Workbooks books;
ws.AttachDispatch(lpDisp);
app = ws.GetApplication();
books = app.GetWorkbooks();
books.Open("D:\\traffic.xls",
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional);
}
到这里老是出Failed to create object. Make sure the object is entered in the system registry的错误,我用的是excel2000的库,刚接触ole,大家帮我看看好么。即使解决不了,告诉我怎么在多文档、使用OLE内嵌Excel的情况下打开已经存在的xls文件的其他方法也可以阿,谢谢了