Code: Select all
SRT>UseExcel
//Put this VBSTART..VBEND block at top of script to declare the functions once
VBSTART
Dim xlApp
Dim xlBook
'Opens the Excel file in Excel
Sub OpenExcelFile(filename)
Set xlApp = CreateObject("Excel.Application")
xlApp.visible = true
Set xlBook = xlApp.Workbooks.open(filename)
end sub
'Closes the Excel file later
Sub CloseExcel
xlApp.quit
Set xlApp = Nothing
End Sub
'Retrieves a cell value from the specified worksheet
Function GetCell(Sheet,Row,Column)
Dim xlSheet
Set xlSheet = xlBook.Worksheets(Sheet)
GetCell = xlSheet.Cells(Row,Column).Value
End Function
'Sets specified cell of specified worksheet
Function SetCell(Sheet,Row,Column,NewValue)
Dim xlSheet
Set xlSheet = xlBook.Worksheets(Sheet)
xlSheet.Cells(Row,Column).Value = NewValue
End Function
VBEND
End>UseExcel
Code: Select all
VBSTART
Sub SortExcelFile(file,sort_column)
Set xlApp = CreateObject("Excel.Application")
xlApp.Workbooks.Open(file)
xlApp.ActiveWorkbook.ActiveSheet.Range("A1").Sort xlApp.ActiveWorkbook.ActiveSheet.Range(sort_column),,,,,,,0
xlApp.ActiveWorkBook.Save
xlApp.ActiveWorkBook.Close false
xlApp.quit
End Sub
VBEND
I don't know VB Script very well, but I tried to edit the subroutine & include it in the single script at the top as follows ...
Sub SortExcelFile(filename,sort_column)
xlBook.ActiveSheet.Range("A1").Sort
xlBook.Range(sort_column),,,,,,,0
xlBook.Save
End Sub
... and then call the routines with ...
VBRun>OpenExcelFile,c:\filer\tosort.xls
VBRun>SortExcelFile,c:\filer\tosort.xls,A1
Nothing happens, so I guess my syntax needs some work! If one of you who knows VB Script can help fix this, I suspect others can also use this in their programs too.