Microsoft Office Excel 2007 is a COM application and can be automated by using a COM object library that is installed with Office Excel and available to Visual Basic 2005. If you've had experience writing VBA Office Excel macros, many of the objects that are used in automation will be familiar to you, because VBA uses the same object library. Visual Basic 2005 is a fantastic system for creating applications that exploit COM Interop and automation; thus, the process for automating Office Excel is easier than ever before.
To assist with understanding the automation objects, Visual Basic 2005 includes F1 help for most of the objects in the Office Excel 2007 library. The only limitation is that the help library is one-version-behind—targeting Office Excel 2003. While writing the sample code for this article, we found it useful to refer to the latest online documentation, which can be found in the MSDN Library by clicking here.
To automate Office Excel, the basic process is to follow this set of steps:
-
Create an Office Excel application object.
-
Create a new workbook, or open an existing workbook.
-
Update the cells and content.
-
Print or save the workbook.
The Office Excel object model has about 250 objects; in practice, however, there are only four objects with which you should really be familiar.
Application Object
The Application object represents the Office Excel 2007 application itself, and creating an Application object is the first step in automation. When you create an Application object, a new instance of Office Excel is started—enabling you to manipulate workbooks programmatically. During the process of automation, Office Excel can be made visible or kept invisible. As you will see in the sample, we choose to keep Office Excel visible when users click a button, so that they can watch Office Excel performing an action on screen. We keep Office Excel invisible when e-mailing or automating Office Excel in the background. Only one instance of the application object has to be created, and you can use this single instance to manipulate multiple workbooks.
Workbook Object
After creating the Application object, the next step is to create or load a workbook. This step is necessary, because Office Excel does not create a blank workbook when it starts. The Workbook object represents a workbook in Office Excel, and it is the primary document type that you will create, open, and save.
Worksheet Object
Every workbook contains one or more worksheets. A worksheet is the actual document—represented as a familiar Office Excel grid into which you can enter content. The Worksheet object represents an Office Excel worksheet, and it is the primary object for entering and reading content from Office Excel.
Cell Object
Every sheet contains cells, which are the editable boxes in a worksheet in which information is stored. Cells are referenced by row and column. In the Office Excel user interface, rows are numbered 1, 2, 3, and so on; and columns are lettered A, B, C, and so on. In the Office Excel programming model, both rows and columns are referenced as numbers that start from 1,1 at the top left corner of a worksheet. Interestingly, in the Office Excel user interface, a cell location is referred to by column, then row; whereas in the programming model, the location is referred to by row, then column.
For example, in Figure 1, the selected cell would be referred to as B3 in the user interface, but 3,2 in the programming model.
Figure 1
Office Excel 2007 supports sheets with up to 1,048,576 rows and 16,384 columns, which means that you can store more than 17 billion values in a single sheet.
The Cell object represents a cell in Office Excel. You can write to, read from, and set up formulas in a cell by using automation, just as you would by using the Office Excel application itself.