Friday, April 2, 2010

VBScript - Creating Pie Charts in Microsoft Excel

VBScript - Creating Pie Charts in Microsoft Excel


Const xlDataLabelsShowPercent = 3

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Add()

Set objWorksheet = objWorkbook.Worksheets(1)

objWorksheet.Cells(1,1) = "Operating System"
objWorksheet.Cells(2,1) = "Windows Server 2003"
objWorksheet.Cells(3,1) = "Windows XP"
objWorksheet.Cells(4,1) = "Windows 2000"
objWorksheet.Cells(5,1) = "Windows NT 4.0"
objWorksheet.Cells(6,1) = "Other"

objWorksheet.Cells(1,2) = "Number of Computers"
objWorksheet.Cells(2,2) = 145
objWorksheet.Cells(3,2) = 487
objWorksheet.Cells(4,2) = 211
objWorksheet.Cells(5,2) = 41
objWorksheet.Cells(6,2) = 56

Set objRange = objWorksheet.UsedRange
objRange.Select

Set colCharts = objExcel.Charts
colCharts.Add()

Set objChart = colCharts(1)
objChart.Activate

objChart.ChartType = 70
objChart.Elevation = 30
objChart.Rotation = 80

objChart.ApplyDataLabels xlDataLabelsShowPercent

objChart.PlotArea.Fill.Visible = False
objChart.PlotArea.Border.LineStyle = -4142

objChart.SeriesCollection(1).DataLabels.Font.Size = 14
objChart.SeriesCollection(1).DataLabels.Font.ColorIndex = 2

objChart.ChartArea.Fill.ForeColor.SchemeColor = 49
objChart.ChartArea.Fill.BackColor.SchemeColor = 23
objChart.ChartArea.Fill.TwoColorGradient 1,1

objChart.ChartTitle.Font.Size = 24
objChart.ChartTitle.Font.ColorIndex = 2

objChart.Legend.Shadow = True

[Via]