Click here to Skip to main content
12,821,494 members (25,526 online)
Click here to Skip to main content
Add your own
alternative version


204 bookmarked
Posted 14 Oct 2000

Using MS Office in an MFC Application

, 4 Nov 2000
Rate this:
Please Sign up or sign in to vote.
Integrating MS Office in your MFC Application using ActiveX Document mode.
<!-- Link to source file download -->
  • Download source files - 46 Kb
  • <!-- Link to demo file download -->
  • Download demo project - 36 Kb
  • <!-- Article image -->

    Sample Image - xoffice.jpg

    <!-- Add the rest of your HTML here -->


    Once I was engaged in a project whose main features were the presence of a great amount of the typical forms of input and output generally associated with Office-type applications. The documents were to be filled from a data store, which the program would display via the use of views. It would be extremely desirable that the document template could understand these different data independently. Therefore, we made the decision to integrate Microsoft Office into our application in order to leverage the already built-in functionalty that we were seeking.

    To that extent, I present to you this article detailing exactly what steps are necessary in order to integrate Microsoft Office into your Visual C++/MFC applications.

    I should also note at this point that one constant problem that I have run into with regards to Office is the unstable nature of the product itself when using it in relation to an ActiveX Document. Therefore, as you look through this article (and the demo) you might see some less than efficient code. An example of a problem that I still have is that even after my application ends, Microsoft Office application remains in memory and can only be removed via the Task manager. If you do solve this problem, please let me know so that I can update this article as well as my own code.

    Integrating MS Office

    Let's begin.

    1. Via the Visual C++ AppWizard, generate a new MDI application called XOffice. On the third step, it will be necessary to select the Container radio button as well as the Active Document Container checkbox.
    2. In addition, this application should be an Automation server. I have taken advantage of a way offered by Nick Hodapp in his article, Using ATL to Automate a MFC Application, which can be found here at CodeGuru. Please familiarize yourself with this example and execute all steps of program transformation to the automation server.
    3. Now we shall engage in integrating MS Office. Include a file Office.h with the contents into the project:
      <FONT color=green>// Office.h</FONT>
      <FONT color=blue>#define</FONT> Uses_MSO2000
      <FONT color=blue>#ifdef</FONT> Uses_MSO2000
      <FONT color=green>// for MS Office 2000</FONT>
      <FONT color=blue>#import </FONT>
    4. We shall create a new form, which we shall require further. Select the New Form option from the Insert menu.

      Enter CFormDemo into a field Name. Press the button New located near a Document field and then click the OK button, in the newly appeared form. And once again click the OK button.

      <!--#include virtual='/script/competitions/whereskent/kentface.asp'> -->We shall place three Edit Boxes and two Buttons on the new form. In ClassWizard we shall bind Edit Boxes to variables (accordingly CString m_str, double m_double, long m_long).

    5. We shall create handler of the following kind for Buttons:
      <FONT color=green>// FormDemo.cpp</FONT>
      <FONT color=blue>void </FONT>
      We shall add the following code to the beginning of XOfficeDoc.cpp file:
      <FONT color=green>// XOfficeDoc.cpp</FONT>
      <FONT color=blue>static </FONT>

      Now we are able to create MDI documents by pressing the buttons on the form.

    6. The MFC class ColeDocObjectItem provides the support for ActiveX documents. The class can do a lot already, but we also need to teach it to load the documents, which we set up.

      Make the following change into a class CXOfficeCntrItem:

      <FONT color=green>// CntrItem.h</FONT>
      <FONT color=blue>class</FONT>
      <FONT color=green>// CntrItem.cpp</FONT>
      CXOfficeCntrItem::CXOfficeCntrItem(CXOfficeDoc* pContainer,LPCTSTR templ)
          : COleDocObjectItem(pContainer), m_isCreate(<FONT color=blue>false</FONT>
    7. And the last thing to do is to make changes in CXOfficeDoc and CXOfficeView classes for ActiveX document display.
      <FONT color=green>// XOfficeDoc.h</FONT>
      <FONT color=blue>class</FONT>
      <FONT color=green>// XOfficeDoc.cpp</FONT>
      : m_ctrl(0)
      BOOL CXOfficeDoc::OnNewDocument()
      <FONT color=blue>    if </FONT>
      <FONT color=green>// XOfficeView.cpp</FONT>
      <FONT color=blue>void </FONT>

      So, now we are able to load ActiveX documents automatically. It is quite not bad already.

      Pay attention to the fact that the procedures of preservation and loading of our documents work normally too, saving thus the contents of the initial template document. The truth is that we don't need it at all. The only thing that does not work is Print Preview. I was not able to understand it therefore, if someone manages to do it I shall be very obliged to find out about it first.

    8. Now we shall teach CXOfficeDoc class to store and to load only our data and not to ask questions about any data change of ActiveX document itself. For this purpose we shall add methods of OnOpenDocument and SaveModified and bring in the following changes with the help of ClassWizard:
      <FONT color=green>// XOfficeDoc.cpp</FONT>
      <FONT color=blue>void </FONT>
      <FONT color=green>// CntrItem.cpp</FONT>
      <FONT color=blue>void </FONT>
    9. The following step shall be the reception of the IDispatch interface of ActiveX document. Let's bring in the following changes to CXOfficeCntrItem class:
      <FONT color=green>// CntrItem.h</FONT>
      <FONT color=blue>#include </FONT>

      I did not want to present the text of the appropriate methods, as it would have taken too much space. They can be looked up in the source texts of the program. It shall be necessary also to bring in the alterations to CXOfficeDoc and CXOfficeView classes listed below.

      <FONT color=green>// CXOfficeView.cpp</FONT>
      <FONT color=blue>void </FONT>
      <FONT color=green>// CXOfficeDoc.cpp</FONT>
      <FONT color=blue>void </FONT>
    10. Now it's high time to make our automation server intelligent. For this purpose we shall define ActiveDocument and IsActiveDocument properties for codeb>IApplication interface and also PStr, PDouble and PLong properties for IDocument interface.

      Is easy to do with the help of ATL Wizard.

      • Workspace->Class View->IApplication->Right button->Add Property
      • Workspace->Class View->IDocument->Right button->Add Property

      The realization of methods can be looked up in the source texts.

    11. The files XOffice.doc and XOffice.xls are the examples of Word and Excel documents. In Word the document initialization of field occurs in event Document_New, which is obviously called from the program. The value of fields is given to the named bookmarks. In Excel the document initialization of cells is made in event Workbook_Activate. It is not quite convenient, but I have tried a good deal of variants and have defined this one (Workbook_Activate) as the best and most stable. As I have already said, the direct call of macros from VBA leaves Excel in memory even when the program is finished.


    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    About the Author

    Igor Tkachev
    United States United States
    No Biography provided

    You may also be interested in...


    Comments and Discussions

    GeneralMy vote of 4 Pin
    huangyunfeng31-Oct-13 19:48
    memberhuangyunfeng31-Oct-13 19:48 
    Questionhow about office 2007? Pin
    rick_012124-Feb-11 21:25
    memberrick_012124-Feb-11 21:25 
    AnswerRe: how about office 2007? Pin
    Kindy Chen19-Apr-12 6:59
    memberKindy Chen19-Apr-12 6:59 
    GeneralRe: how about office 2007? Pin
    yakobom12-Aug-12 22:30
    memberyakobom12-Aug-12 22:30 
    QuestionHow to save the Word files as .doc files?? Pin
    Ravikant16-Nov-10 21:36
    memberRavikant16-Nov-10 21:36 
    QuestionHow to open MS word 2003 in MFC Pin
    sandeeprattu29-Sep-09 22:26
    membersandeeprattu29-Sep-09 22:26 
    GeneralUnhandled exception at 0x7c235a5f Pin
    nguyenhuy160713-Apr-09 19:54
    membernguyenhuy160713-Apr-09 19:54 
    GeneralRe: Unhandled exception at 0x7c235a5f Pin
    chominkyu6-Aug-09 23:15
    memberchominkyu6-Aug-09 23:15 
    GeneralHelp me Pin
    nguyenhuy160713-Apr-09 19:52
    membernguyenhuy160713-Apr-09 19:52 
    GeneralRe: Help me Pin
    chominkyu6-Aug-09 23:15
    memberchominkyu6-Aug-09 23:15 
    Questionhow to export/extract image in word doc? Pin
    jauming12-Apr-09 5:22
    memberjauming12-Apr-09 5:22 
    Questionproblem when using microsoft 2007. what to include??? Pin
    nattyting12-Jan-09 3:51
    membernattyting12-Jan-09 3:51 
    Questionwould you like to give me an example which can open an office file with password? Pin
    chinalinuxworld4-Dec-08 14:23
    memberchinalinuxworld4-Dec-08 14:23 
    GeneralErrors Pin
    Koundinya5-Nov-07 5:43
    memberKoundinya5-Nov-07 5:43 
    GeneralErrors Pin
    Koundinya5-Nov-07 5:39
    memberKoundinya5-Nov-07 5:39 
    GeneralRe: Errors Pin
    namele559-Jul-09 11:00
    membernamele559-Jul-09 11:00 
    GeneralI made port for VS2005 to this project, but... Pin
    Anderson Luís5-Feb-07 12:35
    memberAnderson Luís5-Feb-07 12:35 
    GeneralHelp me... Pin
    Vu Ngoc Quyen30-Oct-06 19:42
    memberVu Ngoc Quyen30-Oct-06 19:42 
    GeneralIs posible to do it in a Dialog Base App Pin
    Jose Ignacio Garcia10-Feb-06 0:17
    memberJose Ignacio Garcia10-Feb-06 0:17 
    QuestionMSWord Automation Too slow in MFC Pin
    Sachinpatole28-Nov-05 18:30
    memberSachinpatole28-Nov-05 18:30 
    GeneralUpperCase and Lower Case Pin
    bizulk14-Sep-05 6:00
    memberbizulk14-Sep-05 6:00 
    GeneralPrint Preview Pin
    Crulex26-May-05 17:15
    memberCrulex26-May-05 17:15 
    GeneralRe: Print Preview Pin
    Magnus22-Nov-05 8:17
    memberMagnus22-Nov-05 8:17 
    GeneralRe: Print Preview Pin
    Crulex22-Nov-05 8:57
    memberCrulex22-Nov-05 8:57 
    GeneralRe: Print Preview Pin
    Luize_Lee27-Aug-06 17:35
    memberLuize_Lee27-Aug-06 17:35 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    Permalink | Advertise | Privacy | Terms of Use | Mobile
    Web02 | 2.8.170308.1 | Last Updated 5 Nov 2000
    Article Copyright 2000 by Igor Tkachev
    Everything else Copyright © CodeProject, 1999-2017
    Layout: fixed | fluid