ZHENG017 原作
(1)使用AppWizard创建一个新的MFC AppWizard(EXE)工程,命名为"office"
(2)选择单文档视图(SDI)结构,在第3步中需要选中Container,以提供容器支持,并且选中active document container 其它都为默认
(3)在View菜单中,选ClassWizard,选Automation选项卡,选Add Class,选择From a TypeLibrary, 在Office目录中选中Microsoft Word 97/2000 类型库Word8.olb或Word9.olb,选中application,document,_document。单击ok
(4)给COfficeCntrItem添加一方法。GetIDispatch()
其源码如下:
ASSERT_VALID(this);
ASSERT(m_lpObject != NULL);
LPUNKNOWN lpUnk = m_lpObject;
Run();
LPOLELINK lpOleLink = NULL;
if (m_lpObject->QueryInterface(IID_IOleLink, (LPVOID FAR*)&lpOleLink) == NOERROR)
{
ASSERT(lpOleLink != NULL);
lpUnk = NULL;
if (lpOleLink->GetBoundSource(&lpUnk) != NOERROR)
{
TRACE0("Warning: Link is not connected!\n");
lpOleLink->Release();
return NULL;
}
ASSERT(lpUnk != NULL);
}
LPDISPATCH lpDispatch = NULL;
if (lpUnk->QueryInterface(IID_IDispatch, (LPVOID FAR*)&lpDispatch)!=NOERROR)
{
TRACE0("Warning: does not support IDispatch!\n");
return NULL;
}
ASSERT(lpDispatch != NULL);
return lpDispatch;
(5)。在officeView.h添加#include "msword8.h"
(6)。修改void COfficeView::OnInsertObject(),源码如下:
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 (filename,clsid)) */
AfxThrowMemoryException();
ASSERT_VALID(pItem);
pItem->Activate (OLEIVERB_SHOW,this);
ASSERT_VALID(pItem);
m_pSelection = pItem; // set selection to last inserted item
pDoc->Updat