您的位置:首页 > 其它

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

2014-12-03 21:52 447 查看
网上查询了很多文章,最后可以总结成,由于Word对象需要用OLE容易,最好的方法就是用视图/文档的模式,因为向导就能帮你创建很多线程的代码,下面是处理简例,具体功能还需研究添加。



(使用VC6+Office2003)/(使用VC6+Office2010)



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



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




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

PS:Office2010是E:\Program Files\Microsoft Office\Office14\msword.olb,E盘为office安装目录,










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



5.为CxxxCntrItem添加下面共有成员函数

public:

LPDISPATCH GetIDispatch();
实现如下:

LPDISPATCH CxxxCntrItem::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.修改xxxView.cpp文件中函数OnInitialUpdate()为

void CxxxView::OnInitialUpdate()

{

CView::OnInitialUpdate();



// TODO: remove this code whenfinal selection model code is written

m_pSelection =NULL; // initialize selection

EmbedAutomateWord();

}

7.为CxxxView.cpp增加下面公有函数

public:

void EmbedAutomateWord();
实现如下:

void CEmbed_WordView::EmbedAutomateWord()

{

//Change the cursor so that theuser knows that something exciting is going

//on.

BeginWaitCursor();

CEmbed_WordCntrItem*pItem = NULL;

TRY{

//Get thedocument that is associated with this view, and be sure that itis

//valid.

CEmbed_WordDoc* pDoc =GetDocument();

ASSERT_VALID(pDoc);

//Create a newitem associated with this document, and be sure that it is

//valid.

pItem = new CEmbed_WordCntrItem(pDoc);

ASSERT_VALID(pItem);

// Get the ClassID for the Word document.

// This is usedin creation.

CLSID clsid;

if(FAILED(::CLSIDFromProgID(L"Word.document",&clsid)))

//Anyexception will do. You just need to break out of the

//TRYstatement.

AfxThrowMemoryException();

// Create theWord embedded item.

if(!pItem->CreateNewItem(clsid))

//Anyexception will do. You just need to break out of the

//TRYstatement.

AfxThrowMemoryException();

//Make sure thatthe new CContainerItem is valid.

ASSERT_VALID(pItem);

// Start theserver to edit the item.

pItem->DoVerb(OLEIVERB_SHOW,this);

// As anarbitrary user interface design, this sets the

// selection tothe last item inserted.

m_pSelection =pItem; // Setselection to the last inserted item.

pDoc->UpdateAllViews(NULL);

//Query for thedispatch pointer for the embedded object. In

//this case, thisis the Word document.

//LPDISPATCH lpDisp;

//lpDisp = pItem->GetIDispatch();

//Add text to theembedded Word document.

// CDocumentwdDoc;

// CRangewdRange;

//set CDocument0wdDoc to use lpDisp, the IDispatch* of the

//actualdocument.

// wdDoc.AttachDispatch(lpDisp);

//Get a CRangeobject for the document.

//wdRange =wdDoc.Range(COleVariant( (long)DISP_E_PARAMNOTFOUND, VT_ERROR),

// COleVariant( (long)DISP_E_PARAMNOTFOUND, VT_ERROR ) );

//Fill the rangewith 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 theuser knows exciting stuff

//is no longer happening.

EndWaitCursor();



}


8.编译运行.



转自:http://blog.sina.com.cn/s/blog_4b3c1f950100ifzl.html

ps:上面只是一个简单的讲述,对msword.olb的操作均未涉及,下篇文章再说
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: