(Use vc6 + office2003)
1. Use the Wizard to create an MFC application.
2. select single or multiple documents and select container (container) in the composite document type)
3. add reference about word-open the addclass from a Type Library of classwinszrd. select MSWord under C:/program files/Microsoft Office/office11. olb (select all objects for convenience)
4. Modify the inherited parent class of the class declaration in the cntritem. h file, from the default coleclientitem to coledocobjectitem
5. Add the following common member functions for C ××× cntritem
Public:
Lpdispatch getidispatch ();
The implementation is as follows:
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
// 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 = 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 = 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. Modify the oninitialupdate () function in the xxx view. cpp file
Void C ×××view: oninitialupdate ()
{
Cview: oninitialupdate ();
// Todo: Remove this code when final selection model code is written
M_pselection = NULL; // initialize Selection
Embedautomateword ();
}
7. Add the following public functions for C ××view. cpp.
Public:
Void embedautomateword ();
The implementation is as follows:
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;
If (failed (: clsidfromprogid (L "word.doc ument", & CLSID )))
// Any exception will do. You just need to break out of
// Try statement.
Afxthrowmemoryexception ();
// Create the word embedded item.
If (! Pitem-> createnewitem (CLSID ))
// Any exception will do. You just need to break out of
// 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
// 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
// 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. Compile and run.