Solved

Excel 11.0 Automation in VC++

Posted on 2003-10-30
6
1,534 Views
Last Modified: 2008-02-20
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,
Kris
0
Comment
Question by:anjukkumar
  • 3
  • 3
6 Comments
 
LVL 8

Expert Comment

by:martynjpearson
ID: 9656654
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!
0
 

Author Comment

by:anjukkumar
ID: 9657721
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.

Thanks,
Kris
0
 
LVL 8

Accepted Solution

by:
martynjpearson earned 500 total points
ID: 9658083
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
Martyn
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:anjukkumar
ID: 9658255
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.

Thanks,
Kris
0
 
LVL 8

Expert Comment

by:martynjpearson
ID: 9658348
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 :

raw_method_prefix("raw_")

Martyn
0
 

Author Comment

by:anjukkumar
ID: 9658374
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.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now