Microsoft Office 2000/Visual Basic Programmer's Guide   

Understanding the Application and NameSpace Objects

When you manipulate Outlook objects, you always start with the Application object. If you are using VBA in Outlook, there is a reference to the Outlook object library set by default. If you are using Automation to work with Outlook objects from another application, you must first set a reference to the Outlook 9.0 object library by using the References dialog box in the application you are working from. If you have set a reference to the Outlook 9.0 object library, you create a new instance of an Outlook Application object by using the New keyword as follows:

Dim olApp As Outlook.Application

Set olApp = New Outlook.Application

If you have not set a reference to the Outlook 9.0 object library, you must use the CreateObject function. There can only be one instance of Outlook available at one time. Therefore, when Outlook is not running, the New keyword (or the CreateObject function) creates a new, hidden, instance of Outlook. If an instance of Outlook is already running, then using the New keyword (or the CreateObject function) returns a reference to the running instance. For more information about using the New keyword or the CreateObject function, see Chapter 4, "Understanding Office Objects and Object Models."

You use the Application object's CreateItem method to create a new Outlook item. You access existing Outlook items by using the NameSpace object.

All the sample procedures discussed in this section use global object variables to represent the Application object and the NameSpace object. Each procedure first checks to see if the Application object variable has been created and, if not, calls the InitializeOutlook procedure to instantiate the global Application and NameSpace object variables. For example:

' Declare global Outlook Application and NameSpace variables.
' These are declared as global variables so that they need not
' be re-created for each procedure that uses them.
Public golApp          As Outlook.Application
Public gnspNameSpace   As Outlook.NameSpace

Function InitializeOutlook() As Boolean
   ' This function is used to initialize the global Application and
   ' NameSpace variables.
   
   On Error GoTo Init_Err
   
   Set golApp = New Outlook.Application    ' Application object.
   Set gnspNameSpace = golApp.GetNamespace("MAPI") ' Namespace object.
   
   InitializeOutlook = True

Init_End:
   Exit Function
Init_Err:
   InitializeOutlook = False
   Resume Init_End
End Function

The InitializeOutlook procedure is available in the modOutlookCode module in OutlookSamples.mdb in the ODETools\V9\Samples\OPG\Samples\CH05 subfolder on the Office 2000 Developer CD-ROM.

You use an olItemType constant as the CreateItem method's single argument to specify whether you want to create a new appointment, contact, distribution list, journal entry, mail message, note, posting to a public folder, or task. The CreateItem method returns an object of the type specified in the olItemType constant; you can then use this object to set additional properties of the item. For example, the following procedure creates a new mail message and sets the recipients, attachments, subject, and message text by using the information passed to the procedure as arguments:

Function CreateMail(astrRecip As Variant, _
                   strSubject As String, _
                   strMessage As String, _
                   Optional astrAttachments As Variant) As Boolean
   ' This procedure illustrates how to create a new mail message
   ' and use the information passed as arguments to set message
   ' properties for the subject, text (Body property), attachments,
   ' and recipients.

   Dim objNewMail            As Outlook.MailItem
   Dim varRecip              As Variant
   Dim varAttach             As Variant
   Dim blnResolveSuccess     As Boolean
   
   On Error GoTo CreateMail_Err
   
   ' Use the InitializeOutlook procedure to initialize global
   ' Application and NameSpace object variables, if necessary.
   If golApp Is Nothing Then
      If InitializeOutlook = False Then
         MsgBox "Unable to initialize Outlook Application " _
            & "or NameSpace object variables!"
         Exit Function
      End If
   End If

   Set golApp = New Outlook.Application
   Set objNewMail = golApp.CreateItem(olMailItem)
   With objNewMail
      For Each varRecip In astrRecip
         .Recipients.Add varRecip
      Next varRecip
      blnResolveSuccess = .Recipients.ResolveAll
      For Each varAttach In astrAttachments
         .Attachments.Add varAttach
      Next varAttach
      .Subject = strSubject
      .Body = strMessage
      If blnResolveSuccess Then
         .Send
      Else
         MsgBox "Unable to resolve all recipients. Please check " _
            & "the names."
         .Display
      End If
   End With
   
   CreateMail = True

CreateMail_End:
   Exit Function
CreateMail_Err:
   CreateMail = False
   Resume CreateMail_End
End Function

The preceding procedure also illustrates how to use a MailItem object's Recipients and Attachments properties to return the respective collection objects and then add one or more recipients or attachments to a mail message. The CreateMail procedure is available in the modOutlookCode module in OutlookSamples.mdb in the ODETools\V9\Samples\OPG\Samples\CH05 subfolder on the Office 2000 Developer CD-ROM.

For an example of a procedure you can use to create any new Outlook item, see the CreateNewOutlookItem procedure in the modOutlookCode module in OutlookSamples.mdb in the ODETools\V9\Samples\OPG\Samples\CH05 subfolder on the Office 2000 Developer CD-ROM.

You use the Application object's GetNameSpace method to instantiate an object variable representing a recognized data source. Currently, Outlook supports the "MAPI" message store as the only valid NameSpace object. To see an example of how to use the GetNameSpace method to create a NameSpace object variable, see the InitializeOutlook procedure earlier in this section.

If Outlook is not running when you create a NameSpace object variable, the user will be prompted for a profile if the user's mail services startup setting is set to Prompt for a profile to be used. Startup settings are on the Mail Services tab of the Options dialog box (Tools menu). You can use the NameSpace object's Logon method to specify a profile programmatically. Profiles are stored in the Windows registry under the \HKEY_CURRENT_USER\Software\Microsoft\Windows Messaging Subsystem\Profiles subkey.