Technical Document

Home
Back

Controlling the Outlook session using OLE

Tip by:
Mahesh.Thatavarthi
CPD Associate
E-Mail: Mahesh.Thatavarthi@hhss.state.ne.us

Recently, I had to control an Outlook session from Powerbuilder application. I searched for the OLE control in the PB object browser and didn't find any controls (like OLE controls for Microsoft Word/Excel). I tried in the following way and it worked fine. It's very simple too. The following code examples explain how to use OLE automation to create, retrieve properties of Microsoft Outlook 97 mail messages, appointments and Contacts from Powerbuilder.

Listing 1

ole_outlook = Create OLEObject
//Connect to Outlook session using 'Outlook.Application'
li_return = ole_outlook.ConnectToNewObject("outlook.application")
//Check for the return code
If li_return <> 0 Then
Messagebox("Error",li_return)
Destroy ole_outlook
Return
Else
MessageBox("Success", "Connected")
End If
Creating and Sending a New Mail Item
Create an OLEObject and connect to Outlook as shown in Listing1. Use the following code to send a mail item. The argument to the 'CreateItem' function specifies the type of item that is going to be created.
'0' Mail Item
'1' Appointment
'2' Contact
'3' Task

Listing2

OLEObject ole_item, ole_attach
//Creates a new mail Item
ole_item = ole_outlook.CreateItem(0)
//Set the subject line of message
ole_item.Subject = "A new attachement for you"
//Body of mail message
ole_item.Body = "Here is a new attachment for you :"+Char(13)
//Recipient(s) Use a semicolon to separate multiple recipients
ole_item.To = "MaheshThatavarthi"
ole_attach = ole_item.Attachments
ole_attach.add(complete path of the file)
ole_item.Display //displays the message
ole_item.Send //sends the message

Retrieving the Contents of a Folder

To access one of the default folders (such as InBox, Calendar, Sent Items, Deleted Items, and Tasks) use the 'GetDefaultFolder' function. The argument to this function specifies the folder name.

'3' DeletedItems
'5' Sent Items
'6' Inbox
'9' Calendar
'10' Contact
'13' Task

The following example shows how to list the subject and bodylines of the InBox folder Items.
Create an OLEObject and connect to Outlook as shown in Listing1.

Listing 3

OLEObject ole_namespace, ole_folder
Long ll_limit
Integer li_loop
//Create the namespace object
ole_namespace = ole_outlook.GetNameSpace("MAPI")
//Argument as '6' specifies InBox folder
ole_folder = ole_namespace.GetDefaultFolder(6)
//Get the number of items in the folder
ll_limit = ole_folder.Items.Count
For li_loop = 1 To ll_limit
//Display the subject and body of the mail message
MessageBox("Subject:"+String(ole_folder.Items(li_loop).Subject), "Body:"+String(ole_folder.Items(li_loop).Body))
Next

Adding a New Appointment

This example illustrates how you can add a new appointment to the Microsoft Outlook Calendar folder. Note the similarity between the example creating a new mailitem and this example.
Create an OLEObject and connect to Outlook as shown in Listing1.

Listing 4

OLEObject ole_item
//Argument as '1' creates an appointment
ole_item = ole_outlook.CreateItem(1)
//Appointment's start time
ole_item.Start = DateTime(Today(), Now())
//Appointment's end time
ole_item.End = DateTime(Today(), RelativeTime(Now(),3600))
ole_item.Subject = "This is a test appointment"
ole_item.Location = "Meeting Hall2"
ole_item.ReminderSet = True
//Set Reminder to 15 minutes before the start of the appointment
ole_item.ReminderMinutesBeforeStart = 15
ole_item.Save //Save the appointment

Adding a New Contact

The following example illustrates how you can add a new Contact to the MS Outlook Contacts folder. This code is also similar to the code for creating new Appointments and MailItems, as previously illustrated:
Create an OLEObject and connect to Outlook as shown in Listing1.

Listing 5

//Argument as '2' creates a Contact Item
ole_item = ole_outlook.CreateItem(2)
//First Name is 'Mahesh'
ole_item.FirstName = "Mahesh"
//Last Name is 'Thatavarthi'
ole_item.LastName = "Thatavarthi"
ole_item.HomeTelephonenumber = "123-456-7890"
ole_item.HomeAddressStreet = "123 Xyz St."
//City for Home Address
ole_item.HomeAddressCity = "AnyCity"
//Postal code for the home address
ole_item.HomeAddressPostalCode = "98765"
ole_item.Save //Saves the Contact

To retrieve the Contact information, modify the code in Listing 3 and get the properties into string variables.

NOTE: Don't forget to destroy all the valid OLEObjects and calling the 'DisConnectObject' function before closing the application. For further information refer any Outlook documentation or visit the websites http://www.wopr.com/ or http://www.outlookexchange.com/