Back to Papers and Articles | |
Using Excel As a Reporting Engine Copyright 1999 Paragon Corporation ( May 26, 1999) |
|
Excel can make a great reporting engine if you have a basic understanding of the Excel Object Model.
Excel can be used as a reporting engine in situations where a standard reporting engine is inadequate. Here are some situations:
In this article, we'll go over the basics of Excel Automation with VBA and delve into some neat capabilities. We'll cover setting cell values, cell formulas, and number formats, and then get into more intricate things like drawing borders, shading columns, and setting cell tooltips. In a later article, we will query an Access database and render the results in Excel. Excel Object Model - Key ObjectsThe Key Objects in Excel starting from the top of the hierarchy and working down are
Creating the Excel Spreadsheet in VBAAn Excel spreadsheet can be generated from any VBA-enabled product such as all the Office 97 applications or Visual Basic 5/6. These tools have an intellisense feature (auto code fill) which becomes enabled if you reference the Excel VBA library. Once you reference the VBA library you can create Excel spreadsheets and instances of Excel using the modern "New" keyword approach. Without referencing the Excel VBA library, you lose the auto sense functionality and must resort to using the CreateObject() OLE construct. For simplicity, we will use the more modern reference approach in our examples.Step 1 - Reference the "Microsoft Excel 8.0 Object Library" when you are in the VBA module design mode. This is usually found under Tools - References. Step 2 - Create a VBA module and declare a global object as an Excel.Application like so:
We declare an Excel application at the global level so that when we create the application in a subroutine or function, it does not lose scope when the subroutine or function is completed.
Step 3 - Create the function or subroutine. This would look something like this:
The example above creates an instance of an Excel application, creates a workbook object in that application window, sets some properties of Cell A1, and sets the formula of Cell A2 to display the current date and time. Note that we can use Excel specific constants such as xlCenter. If we had
not first referenced the Excel Object Library, then we would have had to specify
the integer equivalent of the constant, which is -4108 for
xlCenter . This is another benefit of using library references.
Merging Cells - To merge cells together into a single cell, use the Merge method on a range object. For example
appXl.ActiveSheet.Range("A1:C1").Merge
would merge cells A1, B1, C1 into a single cell in the active sheet of the appXl application we defined previously.
Borders -
To draw a border around a cell, use the BorderAround property of a cell object. For example
Page Setup
| |
Back to Papers and Articles | |
|