Script example

The following example uses objects from the Microsoft Excel Object Library to add a worksheet and embed a chart in a new workbook, RESM.XLS.

local Excel.Application app;
local Excel.Workbook workbook;
local Excel.Worksheet worksheet;
local Excel.Range cellRange;
local Excel.Chart chart;

{If Excel is running, use it. Otherwise, create a new instance.}
app = COM_GetObject("Excel.Application");
if(app = null) then
	app = new Excel.Application();
end if;

{Make Excel visible.}
app.Visible = true;

{Create a new workbook.}
workbook = app.Workbooks.Add();

{Retrieve the first worksheet in the workbook.}
worksheet = workbook.Worksheets.Item[1];

{Set the values of the cells in the first row in the table.}
cellRange = worksheet.Range["A1"];
cellRange.Value = "Agent ID";
cellRange = worksheet.Range["B1"];
cellRange.Value = "Agent Name";
cellRange = worksheet.Range["C1"];
cellRange.Value = "March Sales";
cellRange = worksheet.Range["D1"];
cellRange.Value = "April Sales";
cellRange = worksheet.Range["E1"];
cellRange.Value = "May Sales";

{Add a record to the worksheet.}
cellRange = worksheet.Range["A2"];
cellRange.Value = "10001";
cellRange = worksheet.Range["B2"];
cellRange.Value = "Angela Barbariol";
cellRange = worksheet.Range["C2"];
cellRange.Value = "$400,000";
cellRange = worksheet.Range["D2"];
cellRange.Value = "$200,000";
cellRange = worksheet.Range["E2"];
cellRange.Value = "$100,000";

{Add a new chart.}
chart = app.Charts.Add();

{Use the chart wizard to create a 3D pie chart.}
cellRange = worksheet.Range["C2:E2"];
chart.ChartWizard(cellRange, Excel.xl3DPie, none, none, 0, 0, false, "Sales Totals");

{Set the chart location.}
chart.Location(Excel.xlLocationAsObject, str(worksheet.Name));

{Set the chart to the active chart.}
chart = app.ActiveChart;

{Prevent the SaveAs method from displaying any warning messages.}
app.DisplayAlerts = false;

{Save the workbook.}
workbook.SaveAs("C:\RESM\RESM.XLS");

{Clear the variables.}
clear chart;
clear cellRange;
clear worksheet;
clear workbook;
clear app;

 


Documentation Feedback