单文档嵌入word

来源:互联网 发布:mac 液体唇膏 试色 编辑:程序博客网 时间:2024/04/29 11:04
CFileDialog cFileDlg(TRUE, _T( "doc "),(LPCTSTR)NULL,   
OFN_ENABLESIZING|OFN_EXPLORER|OFN_PATHMUSTEXIST|OFN_LONGNAMES|
OFN_OVERWRITEPROMPT|OFN_PATHMUSTEXIST|OFN_CREATEPROMPT,
_T( "doc   Files   (*.doc)|*.doc|All   Files   (*.*)|*.*|| "),this); 

//获得要打开的文件 
if(cFileDlg.DoModal()!=IDOK)

return; 



CString strfilename = cFileDlg.GetPathName();
BeginWaitCursor();
COfficeCntrItem* pItem = NULL;
TRY
{
// Create new item connected to this document.
COfficeDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pItem = new COfficeCntrItem(pDoc);
ASSERT_VALID(pItem);

// Initialize the item from the dialog data.
/* if (!dlg.CreateItem(pItem))
AfxThrowMemoryException(); // any exception will do
ASSERT_VALID(pItem);*/
CLSID clsid; //
if(FAILED(::CLSIDFromProgID(L"Word.document",&clsid))) 
AfxThrowMemoryException(); 

// if(!pItem->CreateNewItem(clsid)) 
if(!pItem->CreateFromFile (strfilename,clsid)) 
AfxThrowMemoryException(); 
ASSERT_VALID(pItem); 

pItem->Activate (OLEIVERB_SHOW,this);



ASSERT_VALID(pItem);
m_pSelection = pItem; // set selection to last inserted item
pDoc->UpdateAllViews(NULL);
// As an arbitrary user interface design, this sets the selection
// to the last item inserted.

// TODO: reimplement selection as appropriate for your application

}
CATCH(CException, e)
{
if (pItem != NULL)
{
ASSERT_VALID(pItem);
pItem->Delete();
}
AfxMessageBox(IDP_FAILED_TO_CREATE);
}
END_CATCH

EndWaitCursor();



具体原码已经上传,可以去下载  

2 0
原创粉丝点击