您的位置:首页 > 其它

使用单文档视图结构把Word嵌入到VC程序中

2011-05-10 09:50 393 查看
(使用VC6+Office2003)

1.用向导创建MFC应用程序.

2.选择单或多文档,并在复合文档类型中选择container(容器)

3.添加关于word的引用-打开ClassWinszrd的AddClass的from a type library. 选择C:/Program Files/Microsoft Office/OFFICE11下的msword.olb(为方便,把所有对象都选上)

4.修改CntrItem.h文件中类声明的继承父类,由默认的 COleClientItem 改为 COleDocObjectItem

5.为C×××CntrItem添加下面共有成员函数

public:

LPDISPATCH GetIDispatch();

实现如下:

LPDISPATCH C×××CntrItem::GetIDispatch()

{

//The this and m_lpObject pointers must be valid for this function
//to work correctly. The m_lpObject is the IUnknown pointer to
// this object.
ASSERT_VALID(this);
ASSERT(m_lpObject != NULL);
LPUNKNOWN lpUnk = m_lpObject;
//The embedded application must be running in order for the rest
//of the function to work.
Run();
//QI for the IOleLink interface of m_lpObject.
LPOLELINK lpOleLink = NULL;
if (m_lpObject->QueryInterface(IID_IOleLink,(LPVOID FAR*)&lpOleLink) == NOERROR){
ASSERT(lpOleLink != NULL);
lpUnk = NULL;
//Retrieve the IUnknown interface to the linked application.
if(lpOleLink->GetBoundSource(&lpUnk) != NOERROR){
TRACE0("Warning: Link is not connected!/n");
lpOleLink->Release();
return NULL;
}
ASSERT(lpUnk != NULL);
}
//QI for the IDispatch interface of the linked application.
LPDISPATCH lpDispatch = NULL;
if(lpUnk->QueryInterface(IID_IDispatch, (LPVOID FAR*)&lpDispatch)!=NOERROR){
TRACE0("Warning: does not support IDispatch!/n");
return NULL;
}
//After you verify that it is valid, return the IDispatch
//interface to the caller.
ASSERT(lpDispatch != NULL);
return lpDispatch;

}

6.修改×××View.cpp文件中函数OnInitialUpdate()为

void C×××View::OnInitialUpdate()

{

CView::OnInitialUpdate();

// TODO: remove this code when final selection model code is written

m_pSelection = NULL; // initialize selection

EmbedAutomateWord();

}

7.为C×××View.cpp增加下面公有函数

public:

void EmbedAutomateWord();

实现如下:

void C×××View::EmbedAutomateWord()

{

//Change the cursor so that the user knows that something exciting is going
//on.
BeginWaitCursor();
C×××CntrItem* pItem = NULL;
TRY{
//Get the document that is associated with this view, and be sure that it is
//valid.
C×××Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
//Create a new item associated with this document, and be sure that it is
//valid.
pItem = new C×××CntrItem(pDoc);
ASSERT_VALID(pItem);
// Get the Class ID for the Word document.
// This is used in creation.
CLSID clsid;
if(FAILED(::CLSIDFromProgID(L"Word.document",&clsid)))
//Any exception will do. You just need to break out of the
//TRY statement.
AfxThrowMemoryException();
// Create the Word embedded item.
if(!pItem->CreateNewItem(clsid))
//Any exception will do. You just need to break out of the
//TRY statement.
AfxThrowMemoryException();
//Make sure that the new CContainerItem is valid.
ASSERT_VALID(pItem);
// Start 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 the last inserted item.
pDoc->UpdateAllViews(NULL);
//Query for the dispatch pointer for the embedded object. In
//this case, this is the Word document.
LPDISPATCH lpDisp;
lpDisp = pItem->GetIDispatch();
//Add text to the embedded Word document.
// CDocument wdDoc;
// CRange wdRange;
//set CDocument0 wdDoc to use lpDisp, the IDispatch* of the
//actual document.
// wdDoc.AttachDispatch(lpDisp);
//Get a CRange object for the document.
//wdRange = wdDoc.Range(COleVariant( (long)DISP_E_PARAMNOTFOUND, VT_ERROR ),
// COleVariant( (long)DISP_E_PARAMNOTFOUND, VT_ERROR ) );
//Fill the range with the string "Hello, World!"
//wdRange.put_Text( "Hello, World!" );
//wdRang.
}
//Clean up if something went wrong.
CATCH(CException, e){
if(pItem != NULL){
ASSERT_VALID(pItem);
pItem->Delete();
}
AfxMessageBox(IDP_FAILED_TO_CREATE);
}
END_CATCH
//Set the cursor back to normal so the user knows exciting stuff
//is no longer happening.
EndWaitCursor();

}

8.编译运行.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: