VC ++ operation Word

Source: Internet
Author: User

Tool: vc ++ 6.0.

Keywords: c ++, MFC.

Requirement: system installation ms office 2003.

Process:

Preparations:

1. Create an MFC (EXE) project: MFC AppWizard (exe). The project is in the form of a dialog box (for testing ).

2. Create a CLASS on the Class VIEW page: Class Name: CWordOffice, CLASS type: Class type: Generic Class.

3. in the toolbar, find view> class wizard> Automation> add class> from a tye library, and select MSWORD In the offfice installation directory. OLB, select all the listed class names, and click OK.

4. Finally, here isWordapp.rar (XP + VC6 + Word2010)

 

 

After the preparation is complete, the following code is provided:

CWordOffice. the code written manually in h is as follows (automatically generated is not included): # include "atlbase. h "# include" msword. h "class CWordOffice {private: _ Application m_wdApp; Documents m_wdDocs; _ Document m_wdDoc; Selection m_wdSel; Range m_wdRange; InlineShapes limit; InlineShape limit; public: CWordOffice (); virtual ~ CWordOffice (); public: // operation // ************************ create a new document ********** * ********************************* BOOL CreateApp (); // Create a new WORD application BOOL CreateDocuments (); // Create a new WORD document collection BOOL CreateDocument (); // Create a new WORD document BOOL Create (); // create a new WORD application and create a new file void ShowApp (); // display the WORD file void HideApp (); // hide a Word document ********** * ********************************** BOOL OpenDocument (CS Tring fileName); // open an existing document. BOOL Open (CString fileName); // create a new WORD application and Open an existing document. BOOL SetActiveDocument (short I); // sets the currently active document. // ********************** Save the document ************** * ***************************** BOOL SaveDocument (); // The document is opened and saved. BOOL SaveDocumentAs (CString fileName); // the document is created and saved. BOOL CloseDocument (); void CloseApp (); ************* * ************************** void WriteText (CString szText ); // write the text void WriteNewLineText (CString szText, int nLineCount = 1) at the current cursor. // change the number of lines to void WriteEndLine (CString szText ); // write the text void WholeStory () at the end of the document; // select the full file content void Copy (); // Copy the text content to the clipboard void InsertFile (CString fileName ); // write all the content of the local file to the cursor of the current document. ************* * ************************** Void InsertShapes (CString fileName ); // insert an image at the current cursor position // ************************** hyperlink insert operation **** *********************************** void InsertHyperlink (CString fileLink ); // The hyperlink address, which can be a relative path .}; The handwritten code in CWordOffice. cpp is as follows: CWordOffice () {} CWordOffice ::~ CWordOffice () {COleVariant vTrue (short) TRUE), vFalse (short) FALSE), vOptional (long) DISP_E_PARAMNOTFOUND, VT_ERROR); // m_wdDoc.Save (); m_wdApp.Quit (vFalse, // SaveChanges. vTrue, // OriginalFormat. vFalse // RouteDocument .); // release the memory Application resource m_wdInlineShape.ReleaseDispatch (); reset (); // m_wdTb.ReleaseDispatch (); m_wdRange.ReleaseDispatch (); m_wdSel.ReleaseDispatch (); // m_wdFt. ReleaseDispatch (); m_wdDoc.ReleaseDispatch (); then ();} // operate BOOL CWordOffice: CreateApp () {if (FALSE = m_wdApp.CreateDispatch ("Word. application ") {AfxMessageBox (" Application creation failed. Make sure that word 2000 or later is installed! ", MB_ OK | MB_ICONWARNING); return FALSE;} return TRUE;} BOOL CWordOffice: CreateDocuments () {if (FALSE = CreateApp () {return FALSE ;} m_wdDocs.AttachDispatch (m_wdApp.GetDocuments (); if (! M_wdDocs.m_lpDispatch) {AfxMessageBox ("events creation failed! ", MB_ OK | MB_ICONWARNING); return FALSE;} return TRUE;} BOOL CWordOffice: CreateDocument () {if (! M_wdDocs.m_lpDispatch) {AfxMessageBox ("Documents is empty! ", MB_ OK | MB_ICONWARNING); return FALSE;} COleVariant varTrue (short (1), VT_BOOL), vOptional (long) DISP_E_PARAMNOTFOUND, VT_ERROR ); CComVariant Template (_ T (""); // The document Template CComVariant NewTemplate (false), DocumentType (0), Visible; m_wdDocs.Add (& Template, & NewTemplate, & DocumentType, & Visible); // get the document variable m_wdDoc = m_wdApp.GetActiveDocument (); if (! M_wdDoc.m_lpDispatch) {AfxMessageBox ("Document Acquisition failed! ", MB_ OK | MB_ICONWARNING); return FALSE;} // get the selection variable m_wdSel = m_wdApp.GetSelection (); if (! M_wdSel.m_lpDispatch) {AfxMessageBox ("An error occurred while obtaining the Select statement! ", MB_ OK | MB_ICONWARNING); return FALSE;} // get the Range variable m_wdRange = m_wdDoc.Range (vOptional, vOptional); if (! M_wdRange.m_lpDispatch) {AfxMessageBox ("failed to get Range! ", MB_ OK | MB_ICONWARNING); return FALSE;} return TRUE;} BOOL CWordOffice: Create () {if (FALSE = CreateDocuments () {return FALSE ;} return CreateDocument ();} void CWordOffice: ShowApp () {m_wdApp.SetVisible (TRUE);} void CWordOffice: HideApp () {m_wdApp.SetVisible (FALSE);} BOOL CWordOffice :: openDocument (CString fileName) {if (! M_wdDocs.m_lpDispatch) {AfxMessageBox ("Documents is empty! ", MB_ OK | MB_ICONWARNING); return FALSE;} COleVariant vTrue (short) TRUE), vFalse (short) FALSE), vOptional (long) DISP_E_PARAMNOTFOUND, VT_ERROR ), vZ (short) 0); COleVariant vFileName (_ T (fileName); // get the document variable m_wdDoc.AttachDispatch (m_wdDocs.Open (vFileName, // FileName vTrue, // Confirm Conversion. vFalse, // ReadOnly. vFalse, // AddToRecentFiles. vOptional, // PasswordDocument. vOptional, // Pas SwordTemplate. vOptional, // Revert. vOptional, // WritePasswordDocument. vOptional, // WritePasswordTemplate. vOptional, // Format. // Last argument for Word 97 vOptional, // Encoding // New for Word 2000/2002 vOptional, // Visible // The following four parameters are required by word2003. The current version is word2000. VOptional, // OpenAndRepair vZ, // DocumentDirection wdDocumentDirection LeftToRight vOptional, // NoEncodingDialog vOptional) // Close Open parameters); // Close AttachDispatch if (! M_wdDoc.m_lpDispatch) {AfxMessageBox ("Document Acquisition failed! ", MB_ OK | MB_ICONWARNING); return FALSE;} // get the selection variable m_wdSel = m_wdApp.GetSelection (); if (! M_wdSel.m_lpDispatch) {AfxMessageBox ("An error occurred while obtaining the Select statement! ", MB_ OK | MB_ICONWARNING); return FALSE;} // get the Range variable m_wdRange = m_wdDoc.Range (vOptional, vOptional) for all DOC; if (! M_wdRange.m_lpDispatch) {AfxMessageBox ("failed to get Range! ", MB_ OK | MB_ICONWARNING); return FALSE;} return TRUE;} BOOL CWordOffice: Open (CString fileName) {if (FALSE = CreateDocuments () {return FALSE ;} return OpenDocument (fileName);} BOOL CWordOffice: SetActiveDocument (short I) {COleVariant vIndex (_ T (I), vOptional (long) DISP_E_PARAMNOTFOUND, VT_ERROR ); m_wdDoc.AttachDispatch (m_wdDocs.Item (vIndex); m_wdDoc.Activate (); if (! M_wdDoc.m_lpDispatch) {AfxMessageBox ("Document Acquisition failed! ", MB_ OK | MB_ICONWARNING); return FALSE;} // get the selection variable m_wdSel = m_wdApp.GetSelection (); if (! M_wdSel.m_lpDispatch) {AfxMessageBox ("An error occurred while obtaining the Select statement! ", MB_ OK | MB_ICONWARNING); return FALSE;} // get the Range variable m_wdRange = m_wdDoc.Range (vOptional, vOptional) for all DOC; if (! M_wdRange.m_lpDispatch) {AfxMessageBox ("failed to get Range! ", MB_ OK | MB_ICONWARNING); return FALSE;} HideApp (); return TRUE;} BOOL CWordOffice: SaveDocument () {if (! M_wdDoc.m_lpDispatch) {AfxMessageBox ("Document Acquisition failed! ", MB_ OK | MB_ICONWARNING); return FALSE;} m_wdDoc.Save (); return TRUE;} BOOL CWordOffice: SaveDocumentAs (CString fileName) {if (! M_wdDoc.m_lpDispatch) {AfxMessageBox ("Document Acquisition failed! ", MB_ OK | MB_ICONWARNING); return FALSE;} COleVariant vTrue (short) TRUE), vFalse (short) FALSE), vOptional (long) DISP_E_PARAMNOTFOUND, VT_ERROR ); COleVariant vFileName (_ T (fileName); m_wdDoc.SaveAs (vFileName, // VARIANT * FileName vOptional, // VARIANT * FileFormat vOptional, // VARIANT * LockComments vOptional, // VARIANT * Password vOptional, // VARIANT * AddToRecentFiles vOptional, // VARIANT * WritePasswor D vOptional, // VARIANT * implements vOptional, // VARIANT * SaveFormsData vOptional, // VARIANT * implements vOptional, vOptional, vOptional, vOptional, vOptional); return TRUE;} BOOL CWordOffice: CloseDocument () {COleVariant vTrue (short) TRUE), vFalse (short) FALSE ), vOptional (long) DISP_E_PARAMNOTFOUN D, VT_ERROR); m_wdDoc.Close (vFalse, // SaveChanges. vTrue, // OriginalFormat. vFalse // RouteDocument.); m_wdDoc.AttachDispatch (m_wdApp.GetActiveDocument (); if (! M_wdDoc.m_lpDispatch) {AfxMessageBox ("Document Acquisition failed! ", MB_ OK | MB_ICONWARNING); return FALSE;} // get the selection variable m_wdSel = m_wdApp.GetSelection (); if (! M_wdSel.m_lpDispatch) {AfxMessageBox ("An error occurred while obtaining the Select statement! ", MB_ OK | MB_ICONWARNING); return FALSE;} // get the Range variable m_wdRange = m_wdDoc.Range (vOptional, vOptional) for all DOC; if (! M_wdRange.m_lpDispatch) {AfxMessageBox ("failed to get Range! ", MB_ OK | MB_ICONWARNING); return FALSE;} return TRUE;} void CWordOffice: CloseApp () {COleVariant vTrue (short) TRUE), vFalse (short) FALSE ), vOptional (long) DISP_E_PARAMNOTFOUND, VT_ERROR); m_wdDoc.Save (); m_wdApp.Quit (vFalse, // SaveChanges. vTrue, // OriginalFormat. vFalse // RouteDocument .); // release the memory Application resource m_wdInlineShape.ReleaseDispatch (); m_wdInlineShapes.ReleaseDispatch (); // m_wdTb.ReleaseDispatch (); Merge (); // Merge (); m_wdDoc.ReleaseDispatch (); merge (); m_wdApp.ReleaseDispatch ();} void CWordOffice: WriteText (CString szText) {m_wdSel.TypeText (szText);} void CWordOffice: WriteNewLineText (CString szText, int nLineCount/* = 1 */) {int I; if (nLineCount <= 0) {nLineCount = 0 ;}for (I = 0; I <nLineCount; I ++) {Vertex () ;}writetext (szText) ;}void CWordOffice: WriteEndLine (CString szText) {m_wdRange.InsertAfter (szText);} void CWordOffice: WholeStory () {response () ;}void CWordOffice: Copy () {response () ;}void CWordOffice: InsertFile (CString fileName) {COleVariant vFileName (fileName), vTrue (short) TRUE), vFalse (short) FALSE), vOptional (long) DISP_E_PARAMNOTFOUND, VT _ ERROR), vNull (_ T ("");/* void InsertFile (LPCTSTR FileName, VARIANT * Range, VARIANT * ConfirmConversions, VARIANT * Link, VARIANT * Attachment ); */configure (fileName, vNull, vFalse);} void CWordOffice: InsertShapes (CString fileName) {COleVariant vTrue (short) TRUE), vFalse (short) FALSE), vOptional (long) DISP_E_PARAMNOTFOUND, VT_ERROR); m_wdInlineShapes = m_wdSel.GetInlineShape S (); m_wdInlineShape = fill (fileName, vFalse, vTrue, vOptional);} void CWordOffice: InsertHyperlink (CString fileLink) {COleVariant vAddress (_ T (fileLink )), vSubAddress (_ T (""); Range aRange = m_wdSel.GetRange (); Hyperlinks vHyperlinks (aRange. getHyperlinks (); vHyperlinks. add (aRange, // Object, required. Converts a text or image to a hyperlink. VAddress, // Variant type, optional. The address of the specified link. This address can be an email address, Internet address, or file name. Note that Microsoft Word does not check the correctness of the address. VSubAddress, // Variant type, optional. Name of the location in the target file, such as bookmarks, named regions, or slide numbers. VAddress, // Variant type, optional. The text displayed when the mouse pointer is placed on the specified hyperlink can be used as a "screen prompt. The default value is Address. VAddress, // Variant type, optional. The text of the specified hyperlink. The value of this parameter replaces the text or image specified by Anchor. VSubAddress // Variant type. Optional. The name of the frame or window in which the specified hyperlink is to be opened. ); VHyperlinks. releaseDispatch ();} test code: Add a button in the dialog box and add the following code to the event Method: void CWordOperateDlg: OnButtonWord () {AfxEnableControlContainer (); afxOleInit (); CWordOffice wd; wd. createDocuments (); wd. openDocument ("d: \ a.doc"); wd. showApp (); // display word}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.