Sunday, September 5, 2010

Working with Excel Object

'Create an Microsoft Excel Object using VBScript
Set ExcelObject = CreateObject("Excel.Application")
ExcelObject.visible = True
ExcelObject.WorkBooks.Add 'Adds a workbook to an excel object
ExcelObject.Sheets(1).Cells(1,1).value = "Text in the Cell" 'Writes a text to a particular cell in the excel object

'Open Microsoft file using VBScript
Set ExcelObject = CreateObject("Excel.Application")
ExcelObject.visible = True
ExcelObject.Workbooks.Open("c:\excelsheet.xls",Default, False)
ExcelObject.Sheets(1).Cells(1,1).value = "Text to be entered"
ExcelObject.Activeworkbook.SaveAs("d:\excelsheet.xls") 'Using Save As

'Save the Excel Workbook
ExcelObject.Activeworkbook.save

'Get the Number of Rows used in the Excel sheet
RowCount = ExcelObject.ActiveWorkbook.Sheets(1).UsedRange.Rows.count
msgbox "Number of rows used in the excel sheet "& RowCount

'Get the Number of Columns used in the Excel sheet

ColumnCount = ExcelObject.ActiveWorkbook.Sheets(1).UsedRange.Columns.count
msgbox "Number of columns used in the excel sheet "&ColumnCount

'Change the sheet name of the Excel workbook
ExcelObject.Activeworkbook.Sheets(1).Name = "NameOftheFirstSheet"
ExcelObject.Activeworkbook.Sheets(2).Name = "NameoftheSecondSheet"

'Get the Sheet Name of the Excel workbook
FirstSheetName = ExcelObject.Activeworkbook.Sheets(1).Name
msgbox "Name of the first sheet"&FirstSheetName
SecondSheetName = ExcelObject.Activeworkbook.Sheets(2).Name

msgbox "Name of the second sheet"&SecondSheetName

No comments:

Post a Comment