Automation with Excel in VC++ - Range::Replace works in 97 but not XP

Posted on 2005-04-10
Last Modified: 2008-01-09
I've used Automation to format an Excel worksheet, using the Excel 8 type libray so I can target Office versions 97 and later (or so I thought).  I've used the Range::Replace method and when running on a system with Office XP we get the ole error 8002000e Invalid number of parameters, because the XP version of the Replace function takes 8 parameters, whereas the 97 version takes 10.

Quoting from

"Office applications that are tested and compiled with earlier versions of a type library continue to work, unchanged, when end users of your product upgrade their Office installations to a more recent version. In this way Office provides backward compatibility." Apparently not.

Early or late binding is no help here - they've broken the interface. As far as I can see I'll have to pick one version of Office to support - or am I missing something?

Any help is appreciated.
Question by:BobJK
    LVL 8

    Expert Comment

    You can use IDispatch interface to know about the function and then invoke accordingly.


    Author Comment

    Excuse my ignorance - the function always has the same DispID - how would I "know" about the function through IDispatch?

    Also, assuming this is possible, wouldn't I have to do it for every method I want to invoke, for all current and future versions of Excel? Sounds impossible to maintain.

    LVL 8

    Accepted Solution




    you don't need use pDisp->invoke for method call, use above to get the information and do as you do it normally.

    yeah it would be difficult to maintaing all version, you might want to write adapters for known versions, rest ~_~

    as of now this is what is coming to mind, will let you know if there are any other better solutions.


    Author Comment

    Thanks for the quick response _novi_, that gets me out of the immediate trouble.

    Author Comment

    Sorry, I may have been a bit premature in accepting the answer. _novi_, are you referring to the dispatch interface for the Excel.Application object? If so, this does not expose the methods for the IRange interface. I tried calling QueryInterface on IUnknown to get the IRange interface without luck, ie

    // The guid for the Range dispatch interface
    CLSID IID_Range  = {0x00020846, 0x0, 0x0, {0xC0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x46}};
    pUnk->QueryInterface(IID_Range, (void**) &pDisp)

    I also tried calling it on the IDispatch interface, still no luck. The guid is definitely correct, I got it from browsing the type library in the ole viewer. Am I missing something obvious? Thanks in advance - if I need to submit this as a separate question I will.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
    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 how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    759 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

    11 Experts available now in Live!

    Get 1:1 Help Now