Outlook 98 Objects Supplement

[Previous] [Next]

The Application object in the Outlook object library is the topmost object in the hierarchy, as shown in Figure C-1. All other Outlook objects are either created directly from the Application object or are child objects of it. When you write code, you will find that you will use the Outlook Application object extensively, as it provides important functionality, which the following list describes:

  • It provides one-step creation of any of the built-in Outlook types such as Contact, Task, and Mail items.
  • It allows you to obtain the currently active user interface elements, such as the current Outlook window that the user is displaying and the current Outlook window containing the folder the user is viewing.
  • It provides an entry point for you to access the data stored inside of Microsoft Exchange Server by using the Outlook objects.
  • Its status as the topmost object enables you to create or reference the other Outlook objects in your application.

click to view at full size.

Figure C-1 The Outlook 98 object hierarchy.

The Built-In Item and Application Objects

The code you write in the Microsoft Visual Basic Script Edition (VBScript) environment in Outlook is automatically passed both a pre-created Application object and an Item object that correspond, respectively, to the currently active instance of Outlook and the currently active instance of the form. This automatic passing of the objects frees you from having to write code to create them. Rather than attempt to use the CreateObject function in Outlook to create another instance of Outlook, I strongly recommend that you use the Application object that is automatically passed to your VBScript code.

Since these objects are already created for your application, you can quickly use their methods and properties in your VBScript code. For example, you can change the subject or body of the currently displayed item without having to search the current folder for the item, as shown in the following code:

     Sub CommandButton1_Click         Item.Subject = "This is using the built-in Item object"         Item.Body = "This makes writing code that uses the Item " & _             "object easier"         Importance = 2 'High         'Notice how you do not have to include the keyword Item in          'the code since Outlook automatically realizes that you are          'referring to the current item.  However, it's good practice          'to include the explicit Item keyword to make your code more          'readable.     End Sub 

The implicit Item object and its methods and properties correspond to the type of Outlook item the form is based on. For example, the properties and methods for an implicit Item in a Contact form are very different from those for a Message form. Be aware of this when developing your applications.

The following code sample shows you how to use some of the properties of the built-in Application object. Note that unlike the Item object, the built-in Application object requires you to explicitly place the word Application before any of its methods or properties:

     Sub CommandButton1_Click         Msgbox Application.Version         Application.Quit     End Sub 

Application Object Properties

The Application object contains many properties, but the only interesting property to a developer is the Assistant property. The Assistant property returns an object that represents the Microsoft Assistant, such as the paper clip or the Microsoft Office logo assistant. You can in turn use the object library of the Assistant object to create customized messages and animations for the assistant in your application. The assistant is a great way to add friendly messages to your application, especially if a lot of people in your organization use the assistant regularly. The following code snippet shows how to use the Assistant object to display the Subject and Body properties of a message:

     Sub CommandButton1_Click         Set oBalloon = Application.Assistant.NewBalloon         oBalloon.Heading = Item.Subject         oBalloon.Text = Item.Body         oBalloon.Show     End Sub 

NOTE
To get more information on the object model for the Assistant object, use the Object Browser to view the Assistant object in the Office library.

Application Object Methods

The methods of the Application object include ActiveExplorer, ActiveInspector, CreateItem, CreateItemFromTemplate, CreateObject, GetNameSpace, and Quit. As a developer, you will probably use all of these methods at some point in your Outlook development projects. This next sections describe each of these methods in detail.

ActiveExplorer Method

In Outlook, the Explorer object corresponds to an Outlook window that shows the contents of a folder. The ActiveExplorer method returns to your application the topmost Explorer object. In this case, the ActiveExplorer method returns the currently active Outlook window. If there is no currently active Outlook window, this method returns Nothing. Use the ActiveExplorer method in your Outlook application when you need to retrieve or change an element in the Outlook user interface. The following code sample shows how to use the ActiveExplorer method:

     Sub CommandButton1_Click         Set oActiveExplorer = Application.ActiveExplorer         Msgbox oActiveExplorer.CurrentFolder.Name         oActiveExplorer.Display     End Sub 

ActiveInspector Method

Similar to the ActiveExplorer method, the ActiveInspector method returns an active Outlook user-interface object. Instead of returning the active Outlook window as the ActiveExplorer method does, the ActiveInspector method returns the active window that contains an Outlook item. If there is no currently active Outlook window that contains an item, this method returns Nothing. You would use this method in your Outlook application when you need to retrieve or change an element in the user interface for an Outlook item, such as a mail message.

The following code snippet shows how to use the ActiveInspector method:

          Sub CommandButton1_Click         Set oActiveInspector = Application.ActiveInspector         msgbox "The currently active item is " & _             oActiveInspector.CurrentItem.Subject         txtMessage = "It was created using the following editor: "         Select Case oActiveInspector.EditorType             case 1                 txtEditor = "Plain text"             case 2                 txtEditor = "HTML"             case 3                 txtEditor = "Rich Text"             case 4                 txtEditor = "Microsoft Word"         end select         msgbox txtMessage & txtEditor     End Sub 

CreateItem Method

The CreateItem method allows you to quickly create a standard Outlook item without traversing the Outlook object hierarchy. You are limited to creating only the standard items. However, if the type of item you want to create is stored in an .oft (Outlook template) file, you can use the CreateItemFromTemplate method (described in the next section) to quickly create a new item based on the Outlook template. The following code fragment shows you how to quickly create a new item using the CreateItem method:

     Sub CommandButton1_Click         Set oNewMail = Application.CreateItem(0)    'olMailItem         ONewMail.Subject = "This is a test"         ONewMail.Recipients.Add("thomriz@microsoft.com")         ONewMail.Display     End Sub 

You cannot use the predefined constants in VBScript in an Outlook form. Instead, you need to use the numeric values that those constants define. The following table lists the items you can create with the CreateItem method and the items' numeric values:

Mail Item 0
Appointment 1
Contact 2
Task 3
Journal 4
Note 5
Post 6

CreateItemFromTemplate Method

The CreateItemFromTemplate method allow you to specify an .oft file to use as the basis for your new item. The return value for this method is a new object that represents your item. This method also takes an optional parameter in which you can specify the folder where you want to create the new item. If you do not specify a folder for this argument, the default folder for the item type is used. The following example shows you how to use one of the built-in Outlook templates as the template for the CreateItemFromTemplate method:

     Sub CommandButton1_Click         TxtTemplatePath = "C:\Program Files\Microsoft Office\" & _             "Templates\Outlook"         Set oNewItem = Application.CreateItemFromTemplate( _             txtTemplatePath & "\flame.oft")         ONewItem.Display     End Sub 

CreateObject Method

The CreateObject method might be one of the most important methods that you can learn in the Outlook object library, especially if you want to extend your Outlook applications with other types of Automation objects such as Microsoft Excel, Microsoft Collaboration Data Objects (CDO), or ActiveX controls. In order to create an instance of an object with the CreateObject method, you need to pass as a parameter the ProgID that identifies the object class. For example, to create an instance of Microsoft Word, you would use "Word.Application" as the ProgID. For Microsoft Excel, you would use "Excel.Application". Even if an instance of the application is already running on the user's machine, the CreateObject method will create a new instance of the application.

If you want to take advantage of an already running instance of an application, you should instead use the intrinsic VBScript GetObject function. By using the GetObject function, you avoid creating a new instance of the application and are instead returned an object that points at the currently running instance of your requested application.

The following two samples are taken from the Account Tracking sample application, which is discussed in detail in Chapter 7 of the book. Each of these samples create new objects by using the CreateObject method. The first one shows creating an instance of Excel and automating Excel to create charts. The second one shows creating an instance of CDO in an Outlook form so that the unique functionality of CDO can be leveraged in the Outlook environment. Just a quick note on this—the Outlook objects and the CDO objects are not mutually exclusive. You can use Outlook objects in a CDO application as well as use CDO objects in an Outlook application. There are probably many scenarios where you will want to do this, especially when working with CDO, where your application is using some of the special Outlook items that CDO does not have objects for. In Chapter 7 of the book, when you look at the Account Tracking application, you can see creating objects that connect to a Microsoft Access database from your Outlook form and use the database as source for the values in your forms.

     Sub FindAddress(FieldName, Caption, ButtonText)         'This procedure shows automating CDO from Outlook         On Error Resume Next         Set oCDOSession = application.CreateObject("MAPI.Session")         oCDOSession.Logon "", "", False, False, 0         txtCaption = Caption         if not err then              set orecip = oCDOSession.addressbook (Nothing, _                 txtCaption, True, True, 1, ButtonText, "", "", 0)         end if         if not err then             item.userproperties.find(FieldName).value = _                 orecip(1).Name         end if         oCDOSession.logoff         oCDOSession = Nothing     End Sub     Sub cmdCreateSalesChart_Click         'This procedure shows automating Excel from Outlook         Set oExcel = Item.Application.CreateObject("Excel.Application")         'Notice how you can also use the syntax Item.Application to          'get the Application object         oExcel.Visible = True         oExcel.Workbooks.Add         Set oSheet = oExcel.Workbooks(1).Worksheets("Sheet1")         'Set the Title for the Worksheet         oSheet.Activate         set oSheetTitle = oSheet.Range("A1")         oSheetTitle.Value = item.Subject & " Sales Summary"         oSheetTitle.Font.Bold = -1         oSheetTitle.Font.Size = 18         oSheetTitle.Font.Name = "Arial"         'This is only part of the procedure for the rest please see          'the Account Tracking application 

GetNameSpace Method

The GetNameSpaceMethod returns a NameSpace object of the specified type. Today, the only supported type of namespace is MAPI, so the only syntax that you can use to call this method is shown in the next code sample. The NameSpace object is discussed in more detail later in this supplement.

     Sub CommandButton1_Click         Set oNameSpace = Application.GetNameSpace("MAPI")         Set oFolder = oNameSpace.PickFolder     End sub 

Quit Method

The Quit method provides you with the ability to force Outlook to exit, log the current user off, and close all the windows associated with the Outlook session. The following code sample shows you how to call the Quit method in your application:

     Sub CommandButton1_Click         'Display a Yes/No Question msgbox         TxtResponse =(Msgbox "Are you sure you want to quit?", _             36,"Quit Outlook?")         If TxtResponse = 6 then        'Yes             Application.Quit         End if     End Sub 



Programming Microsoft Outlook and Microsoft Exchange
Programming Microsoft Outlook and Microsoft Exchange, Second Edition (DV-MPS Programming)
ISBN: 0735610193
EAN: 2147483647
Year: 2000
Pages: 184

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net