Friday, November 11, 2011

The NameSpace Object


The NameSpace Object

In Outlook, the NameSpace object represents the MAPI message store. The NameSpace object provides methods for logging on or off Outlook, referencing a default folder, and returning objects directly by ID. In addition, the NameSpace object provides access to a variety of methods and properties that are not normally available with the Application object.
For a complete list and description of the methods, properties, and events for the NameSpace object, see Microsoft Outlook Visual Basic Reference Help.
Open the NameSpace object item in the VBScript Samples folder to work directly with this code in Outlook.

NameSpace Object Methods

This section covers the GetDefaultFolder method and the dial method of the NameSpace object.

Returning a Default Folder

You can use the GetDefaultFolder method of the NameSpace object to access folders in the root folder, also known as the Mailbox. To reference a folder in the Mailbox, you can either specify a numeric value as the argument in the GetDefaultFolder method or copy the olDefaultFolders constants from the Outlook Constants item in the VBScript Samples folder and paste them into your code. The table lists these numeric values.
The following example uses the GetDefaultFolder method of the NameSpace object to return the Contacts folder and then display it:
Sub CommandButton1_Click
    Set MyFolder = Application.GetNameSpace("MAPI").GetDefaultFolder(10)
    MyFolder.Display
End Sub
FolderValue
Deleted Items
3
Outbox
4
Sent Items
5
Inbox
6
Calendar
9
Contacts
10
Journal
11
Notes
12
Tasks
13
Drafts
16

Dialing a Phone Number

The Dial method is new to Outlook 2002. If you supply a ContactItem as the argument to the Dial method, you will display the Outlook automatic phone dialer for that contact. The following code example uses the Dial method for the first ContactItem in your Contacts folder:
Sub DialPhone_Click()
    On Error Resume Next
    Dim objContactsFolder, objContactItem
    Const olFolderContacts = 10
    Set objContactsFolder = _
        Application.GetNamespace("MAPI") _
        .GetDefaultFolder(olFolderContacts)
    Set objContactItem = objContactsFolder.Items(1)
    If objContactItem Is Nothing Then
        MsgBox "Could not find a contact to dial." _
            , vbInformation
    Else
        Application.GetNamespace("MAPI").Dial (objContactItem)
    End If 
End Sub

Properties of the NameSpace Object

The NameSpace object provides two properties that you use quite often. These are the CurrentUser and Folders properties.

Returning the Name of the Current User

You can use the CurrentUser property of the NameSpace object to return the name of the currently logged-on user. This example shows the current user’s name in the message box when the CommandButton1 control is clicked:
Sub CommandButton1_Click
    Set MyNameSpace = Application.GetNameSpace("MAPI")
    MsgBox MyNameSpace.CurrentUser
End Sub

Referencing a Folder Collection

You can use the Folders property of the NameSpace object to reference the collection of folders in the MAPI NameSpace. The following example displays the number of subfolders in the Building Microsoft Outlook 2002 Applications .pst file:
Sub ReferenceAFolderCollection_Click
    Set MyNameSpace = Application.GetNameSpace("MAPI")
    set objFolder = _
        MyNameSpace("Building Microsoft Outlook 2002 Applications")
    Set colFolders = objFolder.Folders
    MsgBox "There are " & colFolders.Count & " subfolders" _
        & vbCr & "in " & objFolder.Name, vbInformation
End Sub

Selecting a Folder

You can use the PickFolder method of the NameSpace object to return a MAPIFolder object. The PickFolder method displays a dialog box for the user to select a folder from all available folders in the current profile. The following example displays the Select Folders dialog box and also displays an alert dialog box if the user clicks Cancel. If the user selects a folder, then the folder is displayed in an Explorer window.
Sub PickAFolder_Click()
On Error Resume Next
Set MyNameSpace = Application.GetNameSpace("MAPI")
Set objFolder = MyNameSpace.PickFolder
    If objFolder Is Nothing then
        MsgBox "User Pressed Cancel!", vbInformation
    Else
        objFolder.Display
    End If
End Sub
Figure 11.4 - This Warning dialog box appears when you attempt to access the CurrentUser property of the NameSpace object.

No comments:

Post a Comment