Excel Automation Using Visual C++

E

Einar Værnes

Hello, I'm trying to set up an Excel worksheet from MCVS, as described in
the samples found in
http://download.microsoft.com/download/office2000dev/sample/2/WIN98/EN-US/OFFAUTMN.EXE

I can successfully connect to EXCEL and put data into the cells as
described,
but if I try to do a Range::Sort on the worksheet I get problems.
In the header file excel8.h, in class Range there is a metod:
void Sort(
const VARIANT& Key1,
long Order1,
const VARIANT& Key2,
const VARIANT& Type,
long Order2,
const VARIANT& Key3,
long Order3, long Header,
const VARIANT& OrderCustom,
const VARIANT& MatchCase,
long Orientation,
long SortMethod,
const VARIANT& IgnoreControlCharacters,
const VARIANT& IgnoreDiacritics,
const VARIANT& IgnoreKashida);

I can not find any documentation on how to use this function anywhere,
and always gets an error message: "Sort method of Range class failed"
if I call it in this way:
...
...
oRange = oSheet.GetRange(COleVariant("A1:S100"),vOpt); // OK
oRange.Select();
// OK

oRange.Sort(
// FAIL : Sort method of Range class failed
COleVariant("D1"),
long("xlAscending"),
COleVariant("E1"),
COleVariant(long("xlSortValues"), VT_I4),
long("xlAscending"),
COleVariant("F1"),
long("xlAscending"),
long("xlYes"),
COleVariant(long(1), VT_I4),
COleVariant((long) FALSE, VT_BOOL),
long("xlTopToBottom"),
long("xlStroke"),
COleVariant((long) TRUE, VT_BOOL),
COleVariant((long) TRUE, VT_BOOL),
COleVariant((long) TRUE, VT_BOOL));

Can anyone give me a clue on how to make this work?
I'm using VC++ 6.0 and Excel 2002.
 
O

onedaywhen

Here's a link to the MSDN Excel VBA help for the Sort method:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/off2000/html/xlmthsort.asp

And this is from my Excel 2003 help file:

"Example
This example sorts the range A1:C20 on Sheet1, using cell A1 as the
first sort key and cell B1 as the second sort key. The sort is done in
ascending order by row, and there are no headers. This example assumes
there is data in the range A1:C20.

Sub SortRange1()
Worksheets("Sheet1").Range("A1:C20").Sort _
Key1:=Worksheets("Sheet1").Range("A1"), _
Key2:=Worksheets("Sheet1").Range("B1")
End Sub

This example sorts the region that contains cell A1 (the active
region) on Sheet1, sorting by the data in the first column and
automatically using a header row if one exists. This example assumes
there is data in the active region, which includes cell A1. The Sort
method determines the active region automatically.

Sub SortRange2()
Worksheets("Sheet1").Range("A1").Sort _
Key1:=Worksheets("Sheet1").Columns("A"), _
Header:=xlGuess
End Sub"

From the above you should be able to deduce that using
COleVariant("D1") is not what's required, rather you need to use a
Range object for each key argument.

--
 

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

Top