Name Space Object

The Session property returns a NameSpace object that enables you to access the underlying Outlook data store and gives you access to the default folders. For example, you can access the NameSpace object by using the Application's Session property and then access the inbox, as shown in Listing 10.7.

❖ Note Do not confuse the Outlook NameSpace object with a .NET namespace. These are two completely separate concepts that unfortunately have the same name.

Listing 10.7. Accessing NameSpace to display the number of items in an inbox

Dim Inbox As Microsoft.Office.Interop.Outlook.MAPIFolder Inbox = Me.Session.GetDefaultFolder( _

Outlook.OlDefaultFolders.olFolderInbox) MsgBox(String.Format("Inbox contains {0} items.", _ Inbox.Items.Count))

This code displays the total number of items in the inbox. Notice that this example uses the Format method of the String class to display the information, making it easier to work with the string. To display this same message using VBA, you'd have to concatenate strings, as shown in Listing 10.8.

Listing 10.8. Using VBA to display the number of items in an inbox

Dim ns As Outlook.NameSpace

Set ns = Application.Session

Dim inbox As Outlook.MAPIFolder

Set inbox = ns.GetDefaultFolder(olFolderInbox)

MsgBox ("Inbox contains " & inbox.Items.Count & " items.")

0 0

Post a comment