Excel Sort Problem

M

Matthias Hurrle

Hi!

I get an "Unknown error 0x800A03EC" COM-error when I try the following:

{
// Sort Columns:
CComVariant _varEmpty;
CComVariant _varItem( 1 );
CComVariant Key1( _Range ); // (CString _Range = "A2")
CComVariant OrderCustom( 1 );
CComVariant MatchCase( false );

_varEmpty.vt = VT_ERROR;
_varEmpty.scode = DISP_E_PARAMNOTFOUND;

Excel::XlYesNoGuess _xlGuess = Excel::xlGuess;
Excel::XlSortOrder _XlSortOrder = Excel::xlAscending;
Excel::XlSortMethod _XlSortMethod = Excel::xlStroke;
Excel::XlSortOrientation _xlSortColumns = Excel::xlSortColumns;

// Get Cell:
Excel::RangePtr SortRange = _WorkSheetPtr->GetRange(CComVariant(_Range), CComVariant(_Range));

// Select Cell:
SortRange->Select();

// Catch ReturnValue of Sort:
CComVariant retvalSort;

// Start Sorting Columns
retvalSort = SortRange->Sort(
Key1, // const _variant_t & Key1, "A2"
_XlSortOrder, // enum XlSortOrder Order1, xlAscending
_varEmpty, // const _variant_t & Key2, ""
_varEmpty, // const _variant_t & Type, ""
_XlSortOrder, // enum XlSortOrder Order2, xlAscending
_varEmpty, // const _variant_t & Key3, ""
_XlSortOrder, // enum XlSortOrder Order3, xlAscending
_xlGuess, // enum XlYesNoGuess Header, xlGuess
OrderCustom, // const _variant_t & OrderCustom, 1
MatchCase, // const _variant_t & MatchCase, False
_xlSortColumns, // enum XlSortOrientation Orientation, xlSortColumns
_XlSortMethod); // enum XlSortMethod SortMethod ); xlStroke
}

Has anyone a clue, what is wrong with that?

The Conditions are:

IDE: VisualC++
Excel Version: Excel9 (Office 2000)
Excel-access: via #import created excel9.tlh


TIA for help,

Matthias
 
M

Matthias Hurrle

I get an "Unknown error 0x800A03EC" COM-error when I try the following:
[..]
CComVariant Key1( _Range ); // (CString _Range = "A2")

// Start Sorting Columns
retvalSort = SortRange->Sort(
Key1, // const _variant_t &Key1
[..]);

The solution is not trivial:
Key1 has to be a Range-Object - kind of this:

Excel::RangePtr _Key1 = UsedRange->GetRange(CComVariant("A2"),
CComVariant("A2"));

But that is not all: by definition, Key1 has to be of type VARIANT.
This is the main problem. A simple type-conversion like the following
_never_ works:

CComVariant Key1( _Key1 );

CComVariant and _variant_t have an operator bool and thus they convert
the IDispatch Interface into a VT_BOOL;

So one has to explicitely assign IDispatch to the VARIANT:

IDispatch* pSrc = NULL;
_Key1->QueryInterface(IID_IDispatch, (void**)&pSrc);
CComVariant Key1(pSrc);
pSrc->Release();


That's it!

Matthias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top