[Go] How to create an Automation project using MFC and a type library

Source: Internet
Author: User
Tags microsoft outlook

This article transferred from: http://www.cnblogs.com/zhoug2020/archive/2012/04/01/2429064.html

the summary article details how to automate applications such as Microsoft Office that support COM. More information The following section describes how to create an MFC project. Using Microsoft Excel For example, you can use the first 8 steps for any project, modify 9-15 steps are used for different applications. Create an Automation project1. In Microsoft Developer Studio, create"MFC AppWizard (EXE)"Project, named"AutoProject." 2. In the 1th step of the MFC Application Wizard, select"Dialog Based"Application type and point"Complete"The created class is displayed in the Project Information dialog box that you created: Application:cautoprojectappinchAutoProject.h and AutoProject.cppDialog:CAutoProjectDlginchAutoProject.h and AutoProjectDlg.cpp Click OK to complete the project creation. 3. Visual Studio Design Editing area opens dialog box"Idd_autoproject_dialog", modify it in the following two steps. 4. Delete the static control (idc_static) and the Cancel button (IDCANCEL)5. Change the OK button to"Idrun", the description should read"Run."Close the Autoproject.rc dialog box design interface. 6. Click ClassWizard in the View menu (or press CTRL +W)7. Select the message map label, select Idrun in the Object ID list box, and in the Message list box, select"bn_clicked", click Add function and Name"OnRun", click OK to close the ClassWizard hint: This step defines the AutoProjectDLG.h in the"OnRun ();"function, and the message handler function Cautoprojectdlg::onrun () is added to the AutoProjectDLG.cpp. 8. Click ClassWizard in the View menu (or press CTRL +W)9. Select the automation tag, click Add Class and select"From a type library"Browse and select the object library that you want to automate (for example, if you automate Excel the, select Microsoft Excel8.0Object Library, which is located by default in C:\Program Files\Microsoft Office\office\excel8.olb). If you automate Microsoft Excel -, select Microsoft Excel that is located in C:\Program Files\Microsoft Office\office\excel9.olb9.0Object Library. If you automate Microsoft Excel 2002 and Microsoft Office Excel2003, the object library is included in Excel.exe, and Office 2002 Excel.exe is C:\program Files\Microsoft Office\Office10\Excel.exe by default, office The Excel.exe of 2003 is located by default in C:\program Files\Microsoft Office\office11\excel.exe. After selecting the appropriate object library, click Open, select All classes in the class confirmation list, and click OK. Tip: The list box in the Class confirmation dialog box contains all the IDispatch interfaces (consistent with the class) in the Microsoft Excel type library. Below the dialog you can see the executable file named Excel8.cpp, which contains the encapsulated class derived from COleDispatchDriver, and the header file is Excel8.h (for Excel 2002 and Excel2003, file name is Excel.cpp and Excel.h)Ten. Click OK to close the MFC ClassWizard dialog box OneAdd the following code in the Cautoprojectapp::initinstance () function to load the COM support library: BOOL cautoprojectapp::initinstance () {if(! AfxOleInit ())//Your addition starts here{AfxMessageBox ("Could not initialize COM DLL"); returnFALSE; }     //End of your additionAfxEnableControlContainer ();      .      . .}  A. Add # include lines at top of AutoProject.cpp #include <afxdisp.h> -. Add the include #include to the excel8.h at the top of the AutoProjectDlg.cpp stdafx.h"stdafx.h"#include"excel8.h" //Excel.h in the case of Excel 2002 and Excel 2003.  -. In Cautoprojectdlg::onrun (), add the Automation code shown belowvoidCautoprojectdlg::onrun () {_application app; //app is an Excel _application object//start Excel and get the Application object      if(!app. CreateDispatch ("Excel.Application") {AfxMessageBox ("couldn ' t start Excel."); }      Else      {          //make Excel visible and then display the messageapp.          SetVisible (TRUE); AfxMessageBox ("Excel is running!"); }}  the. Compile and run the project. Run Result: Microsoft Excel will be called when you click the Run button in the dialog box. Closes the message box so that the Auto_excel dialog box is activated. Microsoft Excel exits when the Cautoprojectdlg::onrun () function ends and the application variable leaves the scope. Additional instructions when you add a class from a type library in your project (according to the 9 steps above), you will notice that many classes have been added to the project. In ClassView you can double-click a class to see the definition of the class in Excel8.cpp. If you need to verify the return value or change the execution of the function, you need to get the definition of the function, whenever you change the function definition, remember to modify the definition in Excel8.h. When you do this, make sure you change the correct function definition; sometimes multiple classes contain functions of the same name, such as Getapplication. You can apply these methods to other automation programs by automating the Microsoft Excel narrative above.   The following table contains the names of the Microsoft Office application type libraries. Application (application) type libraries (type library)--------------------------------------------------------Microsoft Access themsacc8.olb Microsoft Jet Database3.5DAO350.dll Microsoft Binder theMsbdr8.olb Microsoft Excel theExcel8.olb Microsoft Graph theGraph8.olb Microsoft Office theMso97.dll Microsoft Outlook theMsoutl97.olb Microsoft PowerPoint theMsppt8.olb Microsoft Word theMsword8.olb Microsoft Access -msacc9.olb Microsoft Jet Database3.51DAO360.dll Microsoft Binder -Msbdr9.olb Microsoft Excel -Excel9.olb Microsoft Graph -Graph9.olb Microsoft Office -Mso9.dll Microsoft Outlook -Msoutl9.olb Microsoft PowerPoint -Msppt9.olb Microsoft Word -Msword9.olb Microsoft Access2002Msacc.olb Microsoft Excel2002Excel.exe Microsoft Graph2002Graph.exe Microsoft Office2002MSO.dll Microsoft Outlook2002Msoutl.olb Microsoft PowerPoint2002Msppt.olb Microsoft Word2002msword.olb Microsoft Office Access2003msacc.olb Microsoft Office Excel2003Excel.exe Microsoft Graph2003Graph.exe Microsoft Office2003MSO.dll Microsoft Office Outlook2003msoutl.olb Microsoft Office PowerPoint2003msppt.olb Microsoft Office Word2003Msword.olb Tips: In addition to Dao350.dll, Dao360.dll, and Microsoft OfficeTen(MSO.dll), the type library is located by default at: C:\Program Files\Microsoft Office\Office (Office2002Path is C:\...\office10,office2003Path is C:\...\OFFICE11), Dao350.dll/dao360.dll is located C:\Program Files\Common Files\Microsoft Shared\Dao by default. Office2002The MSO.dll default C:\Program files\common Files\Microsoft Shared\Office10, Office2003the MSO.dll is located in C:\Program Files\Common Files\Microsoft Shared\OFFICE11==========================================================================================Excel1. Introduce class # #"CApplication.h"#include"CFont0.h"#include"CWorkbook.h"#include"CWorkbooks.h"#include"CRange.h"#include"CWorksheet.h"#include"CWorksheets.h"2. Main interface bool Cxxx::create (void){    if(!m_app. CreateDispatch (L"Excel.Application") {AfxMessageBox (L )"Could not start Excel."); returnFALSE;   } m_app.put_displayalerts (VARIANT_FALSE);   M_app.put_usercontrol (FALSE); returnTRUE;}voidCXXX::D Estroy (void){     //Closeworkbook ();M_app.     Quit (); M_app. ReleaseDispatch ();}intCxxx::openworkbook (CString strpath) {Try{lpdispatch lpdisp; Lpdisp= M_app.get_workbooks ();//Get an IDispatch pointerASSERT (LPDISP); M_books.  AttachDispatch (LPDISP); //Attach The IDispatch pointer to the books object. //Open the documentLpdisp=M_books.          Open (strpath, m_covoptional, M_covoptional, M_covoptional, M_covoptional, m_covoptional, M_covoptional, M_covoptional, M_covoptional, M_covoptional, M_covoptional, M_covoptional, M_covoptional, M_covOptional, M_c         ovoptional);         ASSERT (LPDISP); //Set CWorkbook to use Lpdisp, the idispatch* of the actual workbook.M_book.          AttachDispatch (LPDISP); M_sheets=m_book.get_worksheets (); }    Catch(coledispatchexception*e) {TRACE (_t ("[Exception]openworkbook EXCEPTION coledispatchexception!!! \ n")); TCHAR msg[2048]; E->geterrormessage (MSG,1024x768); E-Delete (); MessageBox (0, MSG, TEXT ("Error"), MB_OK |mb_iconerror); return-1; }     return 0;} voidCxxxx::closeworkbook (void) {M_sheet.  ReleaseDispatch (); M_sheets.  ReleaseDispatch (); M_book.  ReleaseDispatch (); M_books.  Close (); M_books. ReleaseDispatch ();}intCxxx::readdexceldata (CString strpath) {if( -1==Openworkbook (strpath))    {Closeworkbook (); return-1; }if( !Parsecausheet ())    {Closeworkbook (); return-1;  } closeworkbook (); return 0;}voidCexcelop::P Arsesheet (void) {CRange range;  CRange UsedRange;  COleVariant Val;  CString Strrangeid,strkeyword;  CString Strrangepos; intindex =0; M_sheet= M_sheets.get_item (COleVariant ( Short)(1)));//M_sheets is indexed starting from 1UsedRange =M_sheet.get_usedrange (); Range=usedrange.get_rows (); intnrowcnt = (int) Range.get_count ();  for(inti =1; I <= nrowcnt; i++) {..... }return;}

[Go] How to create an Automation project using MFC and a type library

Related Article

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.