Tuesday, August 24, 2010

Write to Excel Cell with specified Colour using vb script

The function below can be used to write data to an Excel sheet.
The ReqColourIndex can be between to 1 to 56


Function WriteToExcelSheet(FileName,SheetName,RowNum,ColNum,DataToWrite,ReqColourIndex)



Dim ExcelApp,ExcelBook,RtnValue
RtnValue = False

If (Utilities.FileExists(FileName)) Then
Set ExcelApp = Sys.OleObject("Excel.Application")
ExcelApp.DisplayAlerts = False
Set ExcelBook = ExcelApp.Workbooks.Open(FileName)
Set sheet = ExcelBook.Sheets(SheetName)
sheet.Cells(RowNum,ColNum) = DataToWrite
sheet.Cells(RowNum,ColNum).Font.ColorIndex = ReqColourIndex
Call ExcelBook.SaveAs(FileName)
ExcelBook.close()
Set ExcelBook = Nothing
Set ExcelApp = Nothing
RtnValue = True
Else
Log.Error(FileName &" does not exist")
End If

WriteExcelSheet = RtnValue

End Function


//Example


sub TestWriteExcelSheet
call writeExcelSheet("C:\1.xls","Sheet1",2,3,"Routing passed",32)
End sub

No comments:

Post a Comment