Creating Objects

You can use CreateObject to start an application as an OLE automation server and create an instance of that application object's class. An object is an instance, or an example, of a particular class. (See the sidebar That Obscure Object of Desire in Chapter 11 of Developing Applications with Microsoft Office for more information on classes.) The syntax for the CreateObject function is as follows:


Dim objVariable As Object
Set objVariable = CreateObject("ApplicationName.ObjectType")

ApplicationName.ObjectType is the class of object that you're creating. For example, the following code creates a Microsoft Excel application object (another instance of Microsoft Excel running) in Access Basic, VBA for Microsoft Excel, or Visual Basic:


Function MakeAnotherXL()
  Dim objXL As Object
  Set objXL = CreateObject("Excel.Application")
  objXL.Visible = True
End Function

In this example, the line objXL.Visible = True unhides the instance of Microsoft Excel launched by the CreateObject function and leaves users there. Unless you're using OLE automation to deliver data to users in Microsoft Excel—for example, by generating a report in Microsoft Excel—you should leave the Microsoft Excel object hidden (its normal state) and close the Microsoft Excel object when you're done with it. Not only will your integrated business applications run faster, but closing OLE automation objects frees up system resources.

Here's sample Access Basic code that creates a Word application object. To leave users in Word, declare the object variable (in this case, objWord) as a global variable (When you declare variables local to a procedure, they're no longer valid at the procedure's end). The following code, which declares objWord in the declarations section, leaves users in Word.


Function MakeWordAppObject()
    Set objWord = CreateObject("Word.Basic")
End Function

The following table shows the application names and object types for the two OLE automation servers included with Office--Microsoft Excel and Word. Although Word exposes only one class of objects to OLE automation (the Word.Basic class) Microsoft Excel exposes three: Excel.Application, Excel.Sheet, and Excel.Chart.

Application Names and Object Types for OLE Automation Servers

Application Name

Object Type

Microsoft Excel

Application

Sheet

Chart

Word

Basic


Each of these classes behaves somewhat differently, and each is suited to particular purposes. Here's a brief summary of when to use each of the classes that Microsoft Excel exposes to OLE automation:

Sample Code: The MakeXLObject Function

The MakeXLObject() function shown below uses the Excel.Sheet class to create an instance of an Excel worksheet. It then populates it with data from an Access database and saves the sheet for later use. "Later use" can be use by the Access application that created it, or by users themselves. This function, written in Access Basic, saves the worksheet as a standard Microsoft Excel workbook (CSTMRPT.XLS) which contains a single sheet. Here are a few things worth noting about the code (which starts on the next page):

  • All the VBA for Microsoft Excel code is non-bold, so it's easy to spot.

  • When you launch Microsoft Excel through OLE automation, it's completely invisible. In fact, it doesn't even appear on the Task List. But if you monitor your free resources, it's obvious that it is open. To close Microsoft Excel, use the following code:

    objVariable.Application.[Quit]

    Set objVariable = Nothing

    Put Quit in the preceding code in square brackets so that the OLE automation client that's creating the Microsoft Excel object, such as Access, associates it with Microsoft Excel and not with the client's own application object. Also, although it's not strictly necessary to use the second line (and the keyword Nothing is undocumented in Access Basic), it's good practice. You'll see why when you experiment with making the various classes of Microsoft Excel objects visible. For example, to make the Microsoft Excel.Sheet class visible, you have to declare the object variable as a global variable. Of course, if you do this, the object variable remains in memory at the end of the function... unless you set it to Nothing.