Open Word file with MFC implementation

Source: Internet
Author: User

When you open a Word file with MFC, you need to import the. olb file.
Select from a Type Library by View/classwizard->add class on the vc++6.0 toolbar ... Locate the required library file import.
The path used in this example is:
C:\Program Files\Microsoft Office\office11\msword. OLB.
(1), shown in Figure (2):


Figure (1) Import Msword.olb file

Figure (2) Press "ctlr+ left mouse button" to import the corresponding class, such as _application, Documents, _document

The detailed steps are as follows:
1. In vc++6.0, create a new dialog-based MFC AppWizard (EXE) named: Openword, (3), figure (4), as shown:


Figure (3) Creating an MFC project: Openword

Figure (4) Select the "type of application" as: Dialog based
2. For resource files, add three controls to the dialog box: a static text control, an edit box control, and a button control. (5) Shown:

Figure (5) adding three controls

Click "View"/"ClassWizard" on the toolbar to import the corresponding Msword.olb file, as shown in figure (1) above, figure (2).
4. Double-click the "Open" button in the dialog box to add a response function as shown in Onbutopen (), (6):


Figure (7) Add response function for "open" button Onbutopen ()

The code is as follows:

voidCopenworddlg:: Onbutopen() {//Todo:add your control notification handler code hereCFileDialog Dlg (TRUE,NULL,NULL, ofn_hidereadonly|Ofn_overwriteprompt,"All Files (*.doc) |*.doc| |", AfxGetMainWnd ());//Construct File Open dialog boxCString strpath;//DECLARE variables    if(DLG.DoModal ()==IDOK)//Determine if the "open" button is pressed{strpath=Dlg.GetPathName ();//Get file pathM_path.SetWindowText (strpath);//Show file path        //word Applications_application app;        Documents docs; _document Doc;//Initialize connectionApp.CreateDispatch ("Word. Application "); CComVariant A (_t (strpath)), B (false), C (0), D (true); Docs.AttachDispatch (App.GetDocuments ()); Doc.AttachDispatch (docs.ADD (&A&B&C&d));//DisplayApp.SetVisible (true);//Release EnvironmentDoc.ReleaseDispatch (); Docs.ReleaseDispatch (); App.ReleaseDispatch (); }}

4. Associate a control-type variable for the edit box: M_path, (8):


Figure (8) For the edit box idc_edit1 associated with a control type variable: M_path
5. Import Msword.h and atlbase.h in the OpenWorldDlg.h header file

#include "msword.h"#include <atlbase.h>

(9) Shown:


Add two references in XXXDlg.h
6. Initialize the COM library. Add the following statement to the application class InitInstance:

::CoInitialize(NULL);

(10) Shown:


Figure (10) in XXX.cpp's InitInstance () function, add the initialization statement for the COM library
The XXX.cpp here refers to the project name. cpp File: OpenWord.cpp
7. The effect is as follows:

Figure (11) Open Word file with MFC

Open Word file with MFC implementation

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.