Solved

Excel 11.0 Automation in VC++

Posted on 2003-10-30
6
1,529 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

707 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

18 Experts available now in Live!

Get 1:1 Help Now