Automating Excel 2007 in C++ by Importing the Excel 2007 Type Library

July 21, 2009 by Jeff Gibeau Leave a reply »

When I started trying to write automations for Excel 2007 using C++, I ran into problems right up front. I was trying to use #import to get to the type library for Excel 2007, and was importing what I thought was the correct file. The following was written for a C++ application in Visual Studio 2008 (VS2008), automating Excel 2007.

  1. The Excel Type Library is not contained in XL5EN32.OLB as you might expect, it is contained in excel.exe
  2. Error #2: Even importing the correct file, errors were being raised.

    //The Following Import gives you the error Error	1	error C2504: '_IMsoDispObj' : base class undefined
    #import "C:\Program Files\Microsoft Office\Office12\EXCEL.EXE"
    

    To correctly import the Excel type library, two more references are needed: MSO.DLL and VBE6EXT.OLB

    #import "C:\Program Files\Common Files\Microsoft Shared\OFFICE12\mso.dll" no_implementation raw_interfaces_only
    #import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB" no_implementation raw_interfaces_only
    #import "C:\Program Files\Microsoft Office\OFFICE12\excel.exe" no_implementation raw_interfaces_only
    
  3. While this solved the problem of the _IMsoDispObj, it raised a few more issues.
    warning C4003: not enough actual parameters for macro 'RGB'
    warning C4003: not enough actual parameters for macro 'DialogBoxW'
    

    Finally I had the solution, renaming the objects in question that seemed to be redefined elsewhere.

    #import "C:\Program Files\Common Files\Microsoft Shared\OFFICE12\MSO.DLL" no_implementation rename("RGB", "ExclRGB") rename("DocumentProperties", "ExclDocumentProperties") rename("SearchPath", "ExclSearchPath")
    #import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB" no_implementation
    #import "D:\Program Files\Microsoft Office\OFFICE12\EXCEL.EXE" rename("DialogBox", "ExclDialogBox") rename("RGB", "ExclRGB") rename("CopyFile", "ExclCopyFile") rename("ReplaceText", "ExclReplaceText")
    

So in conclusion, in order to import the Excel 2007 Type Library in C++ and use it in your automations, 3 files must be imported, MSO.DLL, VBE6EXT.OLB, and EXCEL.EXE. On top of those three files, various objects must be renamed due to already being defined. In my case it was the DialogBox and RGB, though it may differ on each project depending on what is defined. Here is the final code needed to import the type library.

#import "C:\Program Files\Common Files\Microsoft Shared\OFFICE12\MSO.DLL" no_implementation rename("RGB", "ExclRGB") rename("DocumentProperties", "ExclDocumentProperties") rename("SearchPath", "ExclSearchPath")
#import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB" no_implementation
#import "D:\Program Files\Microsoft Office\OFFICE12\EXCEL.EXE" rename("DialogBox", "ExclDialogBox") rename("RGB", "ExclRGB") rename("CopyFile", "ExclCopyFile") rename("ReplaceText", "ExclReplaceText")

Useful Links:

Advertisement

One Response

  1. Lars says:

    Thanks for the note was just what I needed. Have been sitting for ages messing around with those freaking Excel com objects and various importers. This was just what I needed.

Leave a Reply