Workbook object

The Workbook object allows you to work with a workbook. The Workbooks collection contains all of the currently open workbooks in an Excel application.

Creating a new workbook

Use the Add method to create a new workbook and add it to the Workbooks collection.

{Add a new workbook to the Workbooks collection.}
workbook = app.Workbooks.Add();

Opening an existing workbook

Use the Open method to open an existing workbook and add it to the Workbooks collection.

{Open an existing workbook.}
workbook = app.Workbooks.Open("C:\RESM\RESM.XLS");

Retrieving an open workbook

You can retrieve an open workbook from the Workbooks collection with the Item property by the number associated with the workbook.

{Retrieve the third workbook from the Workbooks collection.}
workbook = app.Workbooks.Item[3];

You can also retrieve workbooks by the name of the workbook.

{Retrieve the workbook named "RESM.XLS".}
workbook = app.Workbooks.Item["RESM.XLS"];

Saving a workbook

To save a new workbook, use the SaveAs method.

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

If you have previously saved a workbook, use the Save method to save the workbook.

{Save an existing workbook.}
workbook.Save();

You can use the Saved property to find out if a workbook has been saved. If the Saved property returns true, the workbook has been saved. Otherwise, it has not.

{If the workbook has not been saved, save it.}
if(not workbook.Saved) then
	workbook.Save();
end if;

You can also set the value of the Saved property. If you set the Saved property to true without saving a workbook, any message prompting you to save the workbook is suppressed.


Closing a workbook

Use the Close method to close a workbook.

{Close the workbook.}
workbook.Close();

You can also use the Close method to save a workbook before closing it. You must specify whether to save the workbook and where to save the workbook.

{Save changes and close the workbook.}
workbook.Close(true, "C:\RESM\RESM.XLS");


Documentation Feedback