S.l.e!ep.¢%

像打了激速一样,以四倍的速度运转,开心的工作
简单、开放、平等的公司文化;尊重个性、自由与个人价值;
posts - 1098, comments - 335, trackbacks - 0, articles - 1
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

MFC 调用Automation(一)

Posted on 2009-10-03 23:55 S.l.e!ep.¢% 阅读(782) 评论(0)  编辑 收藏 引用 所属分类: Office Automation
看到的是这篇文章
http://www.vckbase.com/document/viewdoc/?id=1186
  调用word
http://www.vckbase.com/document/viewdoc/?id=648    office 插件开发
http://tech.163.com/05/0201/10/1BGGKDV90009159Q.html ATL开发 office插件

Create an automation project

  1. With Microsoft Developer Studio, start a new "MFC AppWizard (exe)" project named "AutoProject."
  2. In step 1 of the MFC AppWizard, choose "Dialog Based" for the application type and then click Finish.
  3. Click ClassWizard on the View menu (or press CTRL+W).
  4. Select the Automation tab. Click Add Class and choose "From a type library." Navigate to select the object library for the application you wish to automate (for this example, if you are automating Excel 97, choose the Microsoft Excel 8.0 Object Library; the default location is C:\Program Files\Microsoft Office\Office\Excel8.olb).
    Excel 2003. XL5EN32.OLB
  5. NOTE: The list box in the Confirm Classes dialog box contains all of the IDispatch interfaces (which are virtually identical to classes) in the Microsoft Excel type library. In the lower half of the dialog box you will see that an Implementation file named Excel8.cpp contains generated class wrappers derived from ColeDispatchDriver(), and the appropriate declaration header file is named Excel8.h. (For Excel 2002 and Excel 2003, the files are named Excel.cpp and Excel.h.)
  6. Click OK to close the MFC ClassWizard dialog box.
  • Add the following code to the CAutoProjectApp::InitInstance() function, which loads and enables the COM services library:
          BOOL CAutoProjectApp::InitInstance()
          {
             if(!AfxOleInit())  // Your addition starts here
             {
                AfxMessageBox("Could not initialize COM dll");
                return FALSE;
             }                 // End of your addition
    
             AfxEnableControlContainer();
          .
          .
          .
    
          }
    					
  • Add the following line to the #include statements at the top of the AutoProject.cpp program file:
          #include <afxdisp.h>
    					
  • Add the include statement for excel8.h after the include statement for stdafx.h at the top of the AutoProjectDlg.cpp program file:
          #include "stdafx.h"
          #include "excel8.h" // excel.h in the case of Excel 2002 and Excel 2003.
    					
  • Add automation code to the CAutoProjectDlg::OnRun() so that it appears as shown below:
          void CAutoProjectDlg::OnRun()
          {
              _Application app;  // app is the Excel _Application object
    
              // Start Excel and get Application object...
             if(!app.CreateDispatch("Excel.Application"))
             {
                AfxMessageBox("Couldn't start Excel.");
             }
             else
             {
                //Make Excel Visible and display a message
              app.SetVisible(TRUE);
              AfxMessageBox ("Excel is Running!");
             }
          }
    					

             


  • 只有注册用户登录后才能发表评论。
    网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理