Excel 11.0 Automation in VC++

Can someone please send me a sample code for automating Excel 11.0 (Excel2003) using #import.

I made #import work for Excel 97 but #import "excel.exe" on Excel 11.0 is giving around 100 errors. Please let what is the #import statement for excel 11.0.

Thanks in Advance,
Who is Participating?
martynjpearsonConnect With a Mentor Commented:
Here's something to try :

#import "C:\Program Files\Common Files\Microsoft Shared\Office10\MSO.DLL"
#import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBa6\VBE6EXT.OLB"
#import "C:\Program Files\Microsoft Office\Office10\Excel.exe" rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL") rename("DocumentProperties", "DocumentPropertiesXL") no_dual_interfaces

I did this using MS Office XP which is what I have on my machine, and I have to say that I haven't actually tried using the automation - getting it through the compiler was the hurdle I was hoping to get over!

I came up with the other imports by using the OLE View tool that comes with Visual Studio, to find out which type libraries contained the missing interfaces.

Hope this helps
What are the errors? If you are #importing other things too, it could be that they need to be in different namespaces so that there aren't clashes where the same name is used twice.

Using MFC, I usually use Classwizard to create wrapper classes derived from COleDispatchDriver!
anjukkumarAuthor Commented:
A particular module was already developed using Excel 97.0 and here is the code that I used for Excel 97:

#import "C:\Program Files\Microsoft Office\Office\mso97.dll" no_namespace rename("DocumentProperties", "DocumentPropertiesXL")
#import "C:\Program Files\Common Files\Microsoft Shared\VBA\vbeext1.olb" no_namespace
#import "C:\Program Files\Microsoft Office\Office\excel8.olb" rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL") rename("DocumentProperties", "DocumentPropertiesXL") no_dual_interfaces

Now we are upgrading our machines to Office 2003. I agree that using MFC is simpler but it is easy to replace the above code to use Excel 2003, otherwise I have change the code in all the places if I use MFC.

Here is the #import I am using:
#import "C:\Program Files\Microsoft Office\Office11\excel.exe"

Here are sample errors:

c:\krishn\research\excelaatmtn\debug\excel.tlh(1617) : error C2146: syntax error : missing ';' before identifier 'GetRBGXL'
c:\krishn\research\excelaatmtn\debug\excel.tlh(1617) : error C2501: 'MsoRGBType' : missing storage-class or type specifiers
c:\krishn\research\excelaatmtn\debug\excel.tlh(1619) : error C2061: syntax error : identifier 'MsoRGBType'

BTW, all the errors (around 100) are from  excel.tlh.

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

anjukkumarAuthor Commented:
Thanks Marttn for your time.

Excel.tlh has the following:

// Cross-referenced type libraries:
//  #import "C:\Program Files\Common Files\Microsoft Shared\OFFICE11\MSO.DLL"
//  #import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB"

So I included these 2 lines to my code but I still have the same errors.

However, if I use #import with "raw_interfaces_only" like this:
#import "C:\Program Files\Microsoft Office\Office11\excel.exe"   raw_interfaces_only rename("Rectangle", "_Rectangle") rename("Arc", "_Arc") rename("IFilter","_IFilter") rename("RGB", "RBGXL") rename("DialogBox", "DialogBoxXL")  rename("DocumentProperties", "DocumentPropertiesXL") no_dual_interfaces

I get rid of all the errors in excel.tlh but I get several errors from my code as I am not using raw_interfaces in my code. Here is an example error for the code ==== m_pWorkbook = m_pXL->Workbooks->Add((long)xlWorksheet);=====:
c:\krish\work\databasewriter\xls\xlsdatabase.cpp(43) : error C2039: 'Workbooks' : is not a member of '_Application'
        c:\krish\work\databasewriter\debug\excel.tlh(4249) : see declaration of '_Application'

Here is the definition of _Application in excel.tlh with raw_interface_only (no excel.tli is generated)
struct __declspec(uuid("000208d5-0000-0000-c000-000000000046"))
_Application : IDispatch

If I don't use "raw_interface_only", then compiler generates excel.tli and also complete definition of _Application.

Please let me know what else I can try.

The documentation in the online help says...

The raw_interfaces_only attribute also causes the default prefix used in naming the non-property functions to be removed. Normally, the prefix is raw_. If this attribute is specified, the function names are directly from the type library.

So, I wonder if you can put this back by adding to the #import :


anjukkumarAuthor Commented:
Here is what worked for me:

#import "C:\Program Files\Common Files\Microsoft Shared\OFFICE11\MSO.DLL" no_namespace rename("DocumentProperties", "DocumentPropertiesXL")
#import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB" no_namespace
#import "C:\Program Files\Microsoft Office\Office11\excel.exe" rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL") rename("DocumentProperties", "DocumentPropertiesXL") no_dual_interfaces

Previously I was putting #import on excel.exe first instead of putting it after MSO.DLL and VBE6EXT.OLB.

Martyn, I accept your 2nd comment as solution/answer eventhough it is not perfectly correct.
All Courses

From novice to tech pro — start learning today.