Link to home
Start Free TrialLog in
Avatar of BobJK
BobJK

asked on

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

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 http://support.microsoft.com/kb/244167/EN-US/:

"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.
Avatar of novitiate
novitiate

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

_novi_
Avatar of BobJK

ASKER

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.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of novitiate
novitiate

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BobJK

ASKER

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

ASKER

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

LPDISPATCH pDisp;
// 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.