The Primary Office Application Object ModelsNow that we can reliably detect, start, control and shut down other Office applications, the final piece of the puzzle is to learn each application's object model. This part of the chapter provides an introduction to the main objects within each object model, demonstrating some typical uses within Excel-based applications.All the Office applications have a top-level Application object, which is the object we get a reference to when creating new instances of the application. From the Application object, we drill down to the other objects that provide the application's functionality.All the examples in this section can be found on the CD in the \Concepts\Ch18Controlling Other Office Applications folder. The workbook Ch18Examples.xls contains all the example code and a data sheet to represent the results of some analysis. Access and Data Access ObjectsIt's actually quite rare to automate Access itself from an Excel application. We can easily manipulate the data in an Access (Jet) database outside of Access using ActiveX Data Objects, as described in Chapter 13 Programming with Databases, and there's little reason to use Access forms instead of VBA userforms. However, Access is much better than Excel for creating continuous data-driven reports, with its sorting and grouping and separate group, page and report headers and footers. ApplicationEach instance of Access has a single database, which we open using Application.OpenCurrentDatabase and close using Application.CloseCurrentDatabase. The CurrentDb object exposes a Data Access Objects (DAO) Database object, which we can use to manipulate the structure of the tables and queries in the database. Most of the other properties of the Application object provide information about the state of the application, such as which tables the user has open, and are very rarely relevant when controlling Access from Excel. DAO.DatabaseThe DAO Database object that we get from Application.CurrentDb provides programmatic access to the structure of the database, via the TableDefs, QueryDefs and Relations collections. The most commonly used of these is the TableDefs collection, through which we can access the properties of the database tables. In many situations, we may have an access table linked to a separate data source, such as an Excel workbook or SQL Server table and will need to change the table's link information prior to running a report. DoCmdMost automation of Access is done through the DoCmd object, which provides programmatic access to most of Access' menus, including deleting tables, importing data and running reports. ExampleThe procedure in Listing 18-11 runs an Access report based on data in an Excel workbook. The Access database ReportOnExcelData.mdb contains a single table, tblExcelData, to link to the Excel workbook, a query to sort the data and a report, rptExcelData, to run. The procedure creates an instance of Access, opens the database, updates the table's connection information and runs the report, leaving it displayed onscreen.This code can be found in the MAccess module of the Ch18Examples.xls workbook and the database containing the linked table and report is called ReportOnExcelData.mdb. Listing 18-11. Running an Access Report Using Excel Data
WordWord is often automated from Excel when we need to populate a Word document from data in Excelsuch as a monthly report that contains some data analyzed in Excel. ApplicationAs well as the usual properties to control the application itself, the Word Application object has a Documents collection that we use to create, open and access Word documents. DocumentThe Document object provides all the information about a Word document, akin to Excel's Workbook object. BookmarkEach bookmark within a document is included in the Document.Bookmarks collection and exposed as a Bookmark object. Bookmarks enable us to easily identify elements of text within a document. RangeA Range is a contiguous area in a document, identified by its start and end points. Many Word objects (such as Paragraph and Bookmark) have a Range property that returns the area enclosed by the object. We can populate a bookmark by setting the text of its Range. One issue with doing this is that setting the text in a bookmark deletes the bookmark. To set a bookmark's text, we have to store the bookmark's range, set the text of the range, then re-create the bookmark, as shown in Listing 18-12. ExampleSurvey results are very often analyzed in Excel and published as a Word document. This is usually achieved by creating a Word template for the survey results, identifying each insertion point as a bookmark, then copying the data from the Excel workbook to the Word document using VBA. It is quite common in corporate surveys to create a document specific to each of the respondents, where each report is essentially the same, but with that respondent's results and rankings. Listing 18-12 shows a very simple example of this, where we loop through all the divisions in a company, analyzing the data and producing a document for each.This code can be found in the MWord module of the Ch18Examples.xls workbook and the document template is called SalaryReport.dot. Listing 18-12. Populating a Word Template from Excel Data
PowerPoint and MSGraphPowerPoint is usually used in a similar way to Wordpopulating pre-prepared presentations with data from Excel. ApplicationAs well as the usual properties to control the application itself, the PowerPoint Application object has a Presentations collection that we use to create, open and access PowerPoint presentations. PresentationThe Presentation object provides all the information about a PowerPoint presentation, akin to Excel's Workbook object. SlideThe Slide object provides the information about a slide within a presentation, akin to Excel's Worksheet object. When automating PowerPoint, it helps to give each slide a meaningful name, which can be done by selecting the slide and running the following statement from the PowerPoint VBE's Immediate window:
ShapeThe Shape object is the same as Excel's Shape object and is a drawing object on a Slide, which can be a container for text boxes, lines, pictures or embedded objects such as charts. A shape can be given a meaningful name by selecting it and running the following statement from the PowerPoint VBE's Immediate window:
ChartsPowerPoint charts are provided by the MSGraph object model, which is a version of Excel's charting engine, modified to remove the worksheet links. As such, most Excel charting code will work on a PowerPoint chart. However, it is quite common to prepare the charts within Excel, copy them to the clipboard and paste them as pictures in PowerPoint. ExampleThe procedure in Listing 18-13 updates a PowerPoint presentation with data from an Excel spreadsheet. It updates both text in a bulleted list and the source data for an embedded chart.This code can be found in the MPowerPoint module of the Ch18Examples.xls workbook and the presentation we're updating is called Salary Presentation.ppt. Listing 18-13. Populating a PowerPoint Presentation from Excel Data
OutlookOutlook behaves quite differently to the rest of the Office applications. It only allows one instance to be open at any time and doesn't use the 'document' concept. Instead, it stores all its data in a single data file, represented by a Namespace object. The data file is internally structured as multiple folders that each contain a specific category of information, such as e-mails, contacts, appointments and so on. ApplicationThe Outlook Application object provides access to the data store through the GetNamespace property and allows us to easily create new data items (e-mails, contacts, appointments and so on) using the CreateItem method. NamespaceThe Namespace object represents an Outlook data store. Outlook was originally designed to support multiple types of data store, but only one was ever implemented. That is called the MAPI data store and is retrieved using Application.GetNamespace("MAPI"). The Namespace object acts as a container for all the Outlook folders, enabling us to navigate the entire folder hierarchy. It also provides the GetDefaultFolder() property to access each of the top-level folders. MAPIFolderThe MAPIFolder object represents a single Outlook folder, such as the Inbox, Contacts or Calendar folder. It has a Folders property that returns a collection of child folders, enabling us to drill down and an Items property that returns a collection of all the individual items (e-mails, contacts and so on) contained in the folder. AppointmentItem, ContactItem, DistributionListItem, JournalItem, MailItem, NoteItem, PostItItem and TaskItemThese objects represent the individual items within an Outlook folder. We can access them through the Items collection of a MAPIFolder and create them by using either the Add method of an Items collection or the CreateItem method of the Application object. In either case, we get an empty object of the appropriate type which we populate and save to the data store. ExampleThe procedure shown in Listing 18-14 retrieves all the holidays for a specified year from the default Outlook Calendar, displaying them in an Excel worksheet. Listing 18-14. Retrieving Holiday Dates from the Outlook Calendar
Further ReadingThe Office Developer Center on the MSDN Web site should be your first point of call to learn about programming the Office applications. Start at ![]() |