Office automation through VC++ using MFC Class from TypeLib results into large number of compilation errors

When trying to Automate Excel, Word, etc using VC++ (MFC based application) in Visual Studio 2010 we generally perform the following steps –

 

1-Go to Visual Studio

2-Click New Project

3-Expand Visual C++ tree node

4-Choose MFC

5-Select MFC Application

6-Enter project name, click OK

7-Choose a dialog based project

8-Click Finish

9-The bare bone project is created

 

In order to add an MFC Class from Typelib, we do the following –

10-Right click project -> Add -> Class

11-Select “MFC Class from TypeLib” , say Add

12-In the list of available type libraries, choose Microsoft Excel 14.0 Object Library

13-Then choose the list of classes you are planning to use in
the project, eg, _Application, _Workbook etc

Once you are done, you will see the relevant header files
being added – eg- CApplication.h, CWorkbook.h etc

Now in order to automate, you will be required to include
the appropriate header file in your projects cpp file, eg, TestDlg.cpp

Eg, #include "CApplication.h"

      #include "CWorkbook.h"

  

Now begins the trouble -

As soon as you include the respective header files, you will
be presented with a huge number of errors (300+ errors) being thrown during
compilation.

Some errors may appear like –

error C2011: 'Picture' : 'struct' type redefinition 

syntax error : identifier 'MsoRGBType'

syntax error : '<L_TYPE_raw>'

etc, etc, etc

 

In order to resolve these mean errors, you need to do a small step –

Go to the top of each header file and locate the following
line

#import "C:\\Program Files(x86)\\Microsoft Office\\Office14\\EXCEL.EXE" no_namespace

 

Comment this line for each Excel(Office) related header files individually in order to get rid of the compilation errors.