Late Binding

The following code creates an entry in the Outlook calendar. The code uses the late binding technique:

Sub MakeOutlookAppointment()

'Example of Outlook automation using late binding 'Creates an appointment in Outlook

Dim olApp As Object 'Reference to Outlook

Dim olAppointment As Object 'Reference to Outlook Appointment

Dim olNameSpace As Object 'Reference to Outlook NameSpace

Dim olFolder As Object 'Dummy reference to initialize Outlook

Const olAppointmentltem = 1 'Outlook intrinsic constants not available

Const olFolderlnbox = 6 'Outlook intrinsic constants not available

'Create link to Outlook

Set olApp = CreateObject("Outlook.Application")

Set olNameSpace = olApp.GetNamespace("MAPI")

Set olFolder = olNameSpace.GetDefaultFolder(olFolderlnbox)

Set olAppointment = olApp.Createltem(olAppointmentltem)

'Set details of appointment With olAppointment

.Subject = "Discuss Whitefield Contract" .Start = DateSerial(2007, 2, 26) + TimeSerial(9, 30, 0) .End = DateSerial(2007, 2, 26) + TimeSerial(11, 30, 0) .ReminderPlaySound = True .Save End With

'Release object variable Set olApp = Nothing

End Sub

The basic technique in programming another application is to create an object variable referring to that application. The object variable in this case is olApp. You then use olApp (as you would use the Application object in Excel) to refer to objects in the external application's object model. In this case, the Createltem method of Outlook's Application object is used to create a reference to a new Appointmentltem object.

You have also created references to the NameSpace object and the Inbox folder. This is not because you want to use these objects. It is a way of initializing Outlook that has been found to be effective. If you don't do this, errors can occur.

Because Outlook's intrinsic constants are not available in late binding, you need to define your own constants, such as olAppointmentltem here, or substitute the value of the constant as the parameter value. You go on to use the properties and methods of the Appointment object in the With...EndWith structure. Note the times have been defined using the DateSerial and TimeSerial functions to avoid ambiguity or problems in an international context. See Chapter 25 for more details on international issues.

By declaring olApp and olAppointment as the generic Object type, you force VBA to use late binding. VBA cannot resolve all the links to Outlook until it executes the CreateObject function.

The CreateObject input argument defines the application name and class of object to be created. Outlook is the name of the application and Application is the class. Many applications allow you to create objects at different levels in the object model. For example, Excel allows you to create WorkSheet or Chart objects from other applications, using Excel.WorkSheet or Excel.Chart as the input parameter of the CreateObject function.

It is good programming practice to close the external application when you are finished with it and set the object variable to Nothing. This releases the memory used by the link and the application.

If you run this macro, nothing will happen in Excel at all. However, open up Outlook, and in the Calendar you will find that the appointment has been added for the morning of February 26, as shown in Figure 19-1.

Figure 19-1
0 0

Post a comment