Submitted by PhilEddies on Wed, 12/09/2009 - 11:59administrator | 4563 points
The below script is an example of how to create, populate and format an excel document from a VBS script.
The script is commented, but please feel free to comment if you have any quires etc
'Bind to the Excel objectSet objExcel = CreateObject("Excel.Application")'Create a new workbook.
objExcel.Workbooks.Add'Select the first sheet
Sheet = 1'Bind to worksheet.Set objSheet = objExcel.ActiveWorkbook.Worksheets(Sheet)'Name the worksheet
objSheet.Name = "VBS_Excel_Example"'Set the save location
strExcelPath = "c:\Vbs_Excel_Example.xls"'--------------------------------------------------------'Populate the worksheet with data'--------------------------------------------------------' objSheet.Cells(row, column).Value = "Whatever"'Add some titles to row 1
objSheet.Cells(1, 1).Value = "Name"'Row 1 Column 1 (A)
objSheet.Cells(1, 2).Value = "Description"'Row 1 Column 2 (B)
objSheet.Cells(1, 3).Value = "Something Else"'Row 1 Column 3 (C)'Add some data using a loopFor row = 2to10
objSheet.Cells(row, 1).Value = "Item " & row & " Name"
objSheet.Cells(row, 2).Value = "Item " & row & " Description"
objSheet.Cells(row, 3).Value = "Item " & row & " Something Else"Next'--------------------------------------------------------' Format the spreadsheet'--------------------------------------------------------'Put the first row in bold
objSheet.Range("A1:C1").Font.Bold = True'Change the font size of the first row to 14
objSheet.Range("A1:C1").Font.Size = 14'Freeze the panes
objSheet.Range("A2").Select
objExcel.ActiveWindow.FreezePanes = True'Change column A and B to use a fixed width
objExcel.Columns(1).ColumnWidth = 20
objExcel.Columns(2).ColumnWidth = 30'Change column C to autofit
objExcel.Columns(3).AutoFit()'Change the background colour of column A to a light yellow
objExcel.Columns(1).Interior.ColorIndex = 36'Change the font colour of column C to blue
objExcel.Columns(3).Font.ColorIndex = 5'--------------------------------------------------------' Save the spreadsheet and close the workbook'--------------------------------------------------------
objExcel.ActiveWorkbook.SaveAs strExcelPath
objExcel.ActiveWorkbook.Close'Quit Excel
objExcel.Application.Quit'Clean UpSet objSheet = NothingSet objExcel = Nothing
Hi,
I'm having problems viewing your site using the Opera browser, for some reason the font is very small (almost unreadable) Any ideas why?
Many thanks
Loz
- hp 6735s
Comments
The below table may help when using the colorindex property.
I will take a look, which version of Opera are running and on which OS?
I have just tried Opera 10.61 on Windows 7 that seems ok.
Is it just this site the font looks small on?
Add new comment