Replacing CreateObject with Strongly Typed Variables and the Keyword New

When you declare variables as type Object, as you did in step 4 of the preceding example, Visual Basic doesn't know what the exact type of object is until it tries to create it the first time. Declaring objects as the generic Object type has benefits, but more often you'll want to declare an object variable as the specific type of object that it represents.

Using Strongly Typed Variables

When you declare an object variable as a specific type, your code runs faster and the Visual Basic Editor can help reduce the number of errors in your code. One way the Editor can do this is with the Auto List Members feature, which displays all of the properties and methods supported by the object. This feature lets you see easily whether the option you're trying to use actually exists. However, you must take an additional step before using the Auto List Members feature with objects that aren't part of the application containing your Visual Basic code: you must make sure that the object library containing the object that you want to reference is loaded into the Editor.

Load the PowerPoint Object Library

  1. Switch to the Visual Basic Editor of Excel.
  2. On the Tools menu, click References.
  3. Click to view at full size.

    The References dialog box displays a list of every object library currently registered on your system. In addition to the object library for the application containing your Visual Basic code (in this example, Excel), three object libraries are always referenced by default:

    Visual Basic individually saves the items selected in the list of Available References for each Visual Basic for Applications project. This means that setting a reference to a particular object library won't create the same reference in every Visual Basic for Applications project you have open.

  4. Scroll down the list, select the check box next to the Microsoft PowerPoint 9.0 Object Library, and click OK.

    You have set a reference to the Microsoft PowerPoint 9.0 Object Library for your current Visual Basic project. Second, you now have access to all the PowerPoint objects, methods, and properties, and when you work with an object variable that is declared as a PowerPoint object type, the Auto List Members drop-down list appears when you enter your Visual Basic code. Third, when you add a reference to an object library, it appears in the Libraries drop-down list in the Editor's Object Browser so you can browse through the object model and conduct member searches if necessary. For more information about the Object Browser, see "Learning the Members of the Object Model" in Chapter 1.

  1. Create a new procedure by typing Sub SetReferences and pressing ENTER.
  2. Add the following line:
  3. Dim appPPT As PowerPoint.Application
    

    Right after you type the word As and a space, the Auto List Members drop-down list appears and PowerPoint is listed (scroll through the list or type pow to see it). The Auto List Members drop-down list appears again right after you type the period (.) after the word "PowerPoint". Continue to type the word Application; "Application" will appear selected in the Auto List Members drop-down list. Once you enter it, this line declares the variable appPPT as an Application object of PowerPoint.

TIP
When the Auto List Members drop-down list appears and you type the first few letters of the member name you need, Visual Basic automatically selects an item in the list that matches what you type. You can then press TAB to complete your statement with the selected item and then close the drop-down list.

  1. Add the following two lines:
  2. Set appPPT = CreateObject ("PowerPoint.Application")
    appPPT.Visible = True
    

    Right after you type the period (.) after the variable name appPPT, the Auto List Members drop-down list appears and displays a list of the properties and methods belonging to PowerPoint. Thus, when you declare a variable as a specific type, Visual Basic provides you with a list of the object's property and method members. You don't have to remember which properties and methods the object supports because the Auto List Members drop-down list displays them.

NOTE
The Auto List Members drop-down list also appears when you type the equal sign (=) to set the value of a property. In this case, the Auto List Members drop-down list displays the possible values you can assign to the Visible property, including two additional True values, msoCTrue and msoTrue, which are the numeric values 1 and -1, respectively. You don't need to worry about the uses of the values now, and any of the True values (including True itself) makes the window visible. More specifically, you can use "True" in the place of "msoTrue" or "False" in the place of "msoFalse." They are both equivalent.

  1. Place the cursor in the procedure and press F5. When you run this procedure, Visual Basic compiles your project and checks your code's syntax, ensuring that you have valid values assigned to variables. Once Visual Basic compiles your code, it runs slightly faster than if you didn't declare your object variables as a specific type.
  2. Exit PowerPoint without saving changes.

Using the Keyword New

Dim, Sub, End, and so on are keywords, words recognized as part of the Visual Basic programming language. These keywords are essential to your programming efforts because they're the ones that control the way Visual Basic interprets your instructions. The keyword New provides a way of creating an instance of an object besides using the CreateObject function.

When you use the CreateObject function, you must first declare an object variable and then use the Set statement to assign the object instance to the object variable. If you use New when declaring an object variable, you don't have to do that. Once an object is declared in a Dim statement that includes the keyword New, a new instance of the object is created on the first reference to it in code.

NOTE
The New keyword can be used to declare only object variables and not any other data type such as String, Integer, or Long.

Create a New Application Object

  1. In the Visual Basic Editor of Excel, in the same code module, add and run the following procedure:
  2. Sub UsingKeywordNew()
        Dim appPPT As New PowerPoint.Application
        MsgBox appPPT.Path
    End Sub
    

    Visual Basic creates a new instance of the PowerPoint Application object using this syntax, but it's not visible. The message box line where you display the path of the PowerPoint application is the first reference to the PowerPoint Application object you declared in the second line.

    After the Dim statement, you don't need the statement Set appPPT = CreateObject ("PowerPoint.Application"). The use of the New keyword in the Dim statement indicates that a new instance of the PowerPoint Application object is available but won't load until the first reference is made to it or to one of its members.

  3. Click OK to close the message box.