(1) Creating a new MFC AppWizard (EXE) project using AppWizard, named "Office" (2) Select Single Document View (SDI) structure, you need to select Container to provide container support, and select ACTIVE Document Container all other
(3) In the View menu, select ClassWizard, select the Automation tab, select from a type, select from a type, select Microsoft Word 97/2000 Type Library Word8.OLB or WORD9.OLB in the Office Directory, select Application, Document, _Document. Click OK
(4) Add a method to COfficeCntritem. GetIDispatch ()
The source code is as follows:
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 ( LPOLLINK! = NULL); lpunk = null; if (LPOLINK-> getBoundSource (& lpunk)! = noerror) {trace0 ("Warning: Link is not connection! / 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). Add #include "mword8.h" in officeview.h.
(6). Modify void cofficeview :: OnInsertObject (), the source code is as follows:
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.CREATEM (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-> UpdateAllViews (NULL); // As an arbitrary user interface design, this sets The selection // to the last item inserted.
// Todo: Reimplement Selection As Appriate for your application
M_PSelection = Pitem; // set selection to last inserted item pdoc-> updateAllViews (null);} catch (cexception, e) {if (pitem! = null) {assert_valid (pitem); pitem-> delete ();} afXMESSAGEBOX (Idp_failed_to_create);} End_catch
ENDWAITCURSOR ();
(7) Overload id-file-save,
Void cofficeview :: onfilesave () {// Todo: add your command handler code here {lpdispatch lpdisp; lpdisp = m_pselection-> getidispatch ();
_Application app; _document mydoc; documents my;
MyDoc.attachdispatch (LPDISP, TRUE); app = mydoc.getApplication (); / * app.run ("macro3"); * / mydoc.activate (); bool password = mydoc.getHaspassword (); mydoc.setpassword (" "); Password = mydoc.gethaspassword (); Colevariant Vfalse; MyDoc.saveas (Colevariant (" C: //love.doc "), Vfalse, Vfalse, Colevariant ("), Vfalse, Colevariant ("" ", vfalse, vfalse, vfalse, vfalse, vfalse;} catch (cexception, e) {} end_catch} bulid, click insert object, and edit, and save.maybe run macro. Hard work has been working hard Others' experience is finalized. :)