Solved

Excel 11.0 Automation in VC++

Posted on 2003-10-30
6
1,531 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

910 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

21 Experts available now in Live!

Get 1:1 Help Now