Helpful Information
 
 
Category: Delphi Programming
excel sort from Delphi

I can't get a call to the excel SORT function to work; I've tried variations of the following, but am not sure how to
call the "key1" variant; and don't know what the "Type_" OleVariant is in the third function call.

IRange
function Sort(Key1: OleVariant; Order1: XlSortOrder; Key2: OleVariant; Type_: OleVariant;
Order2: XlSortOrder; Key3: OleVariant; Order3: XlSortOrder; Header: XlYesNoGuess;
OrderCustom: OleVariant; MatchCase: OleVariant; Orientation: XlSortOrientation;
SortMethod: XlSortMethod; out RHS: OleVariant): HResult; stdcall;

Excel Macro
Selection.Sort Key1:=Range("B2"), Order1:=xlAscending, Key2:=Range("C2") _
, Order2:=xlAscending, Key3:=Range("D2"), Order3:=xlAscending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

Excel Range
function Sort(Key1: OleVariant; Order1: XlSortOrder; Key2: OleVariant; Type_: OleVariant;
Order2: XlSortOrder; Key3: OleVariant; Order3: XlSortOrder; Header: XlYesNoGuess;
OrderCustom: OleVariant; MatchCase: OleVariant; Orientation: XlSortOrientation;
SortMethod: XlSortMethod): OleVariant; dispid 880;

Any Help ?

E is the Variant that represents the Excel instance:



E.Range['B2', E.ActiveCell.SpecialCells[xlLastCell]].Select;
E.Selection.Sort(E.Range['B2'], xlAscending, EmptyParam, E.Range['C2'], xlAscending, E.Range['D2'], xlAscending);



According to Microsoft (http://support.microsoft.com/default.aspx?scid=kb;en-us;229107) the documentation for Excel 97 is not correct, parameters Key2 and Type are switched. (it should be Type first, then Key2). You can use EmptyParam as Type for a normal sort, like I did in the sample code. (If the Excel macro doesn't specifiy a value, use EmptyParam)

This answer is a bit late, I posted this answer for documentation purposes.

Kind regards,

Jeroen Wernsen










privacy (GDPR)