MAPIFolder Object Properties

This section covers the DefaultMessageClass, FolderPath, Folders, InAppFolderSyncObject, Items, Parent, Child, EntrylD, StorelD, UnReadltemCount, and WebViewURL properties. The Folders property is useful for accessing a folder in the folder list. The Items property is useful for retrieving a collection of items in the folder.

Referencing a Folder in the Folder List

You can use the Folders property with the MAPIFolder object to return another MAPIFolder object. The OpenMAPIFolder function shown is a more efficient way to return a folder in the folder list, compared with the folder-by-folder method illustrated here. In the following procedure, the Folders property is used to move through the various branches of a personal folders list:

Sub ReferenceAFolder_Click Set MyNameSpace = Application.GetNameSpace("MAPI") Set MyFolder = MyNameSpace. Folders _

("Building Microsoft Outlook 2002 Applications") Set BeyondFolder = MyFolder.Folders("4. Beyond the Basics") Set VBScriptFolder = BeyondFolder.Folders("VBScript") MsgBox VBScriptFolder.Name, vblnformation End Sub

Iterating Through a Collection of Items in a Folder

You can use the Items property of the MAPIFolder object to return a collection of items in the folder. The following example uses the Items property of the MAPIFolder object to return the collection of items in the Inbox folder. It then shows the Subject value of the first five items in the Items collection.

Sub lterateThroughACollectionofltems_Click() On Error Resume Next

Set MyNameSpace = Application.GetNameSpace("MAPI") Set MylnboxFolder= MyNameSpace.GetDefaultFolder(6) Set Myltems = MylnboxFolder.ltems For i = 1 to 5 Set Myltem = Myltems(i) Msgbox Myltem.Subject, vblnformation Next End Sub

The EntrylD and StorelD Properties

The EntrylD and StorelD properties of the MAPIFolder object can be used to identify a folder in Outlook. The EntrylD property corresponds to the MAPI property PR_ENTRYID. When an object is created, MAPI systems assign a permanent, unique ID string which does not change from one MAPI session to another. The EntrylD and StorelD properties, which are analogous to primary keys in a database table, let you identify both Folder and Item objects in the MAPI subsystem. Once you have these values, you can use the GetFolderFromID method to return a MAPIFolder object.

The following example displays the value of the MAPI EntrylD and StorelD for the current folder in a message box, displays another message box showing the folder's UnReadltemCount, DefaultMessageClass, and WebViewURL, and then uses the GetFolderFromID method to re-instantiate the Folder object and display the folder:

Sub ShowFolderlnfo_Click Set MyNameSpace = Application.GetNameSpace("MAPI") Set MyFolder = MyNameSpace. PickFolder

If MyFolder Is Nothing Then

MsgBox "User pressed cancel.", vblnformation Exit Sub End If

MsgBox "The Entry ID for the selected folder is:" & vbCr_ & MyFolder.EntrylD & vbCr & vbCr_ & "The Store ID for the selected folder is:" & vbCr_ & MyFolder.StorelD, vblnformation MsgBox MyFolder.UnReadltemCount & " of" & MyFolder.Items.Count _ & " items are unread." & vbCr_ & "The default message class is: _ & MyFolder. DefaultMessageClass & vbCr_ & "The folder URL is: " & MyFolder.WebViewURL, vblnformation Set MyFolder = MyNameSpace.GetFolderFromID _

(MyFolder.EntrylD, MyFolder.StorelD) MyFolder.Display End Sub

Making a Folder Available for Offline Use

You can programmatically make a folder available for offline use by using the InAppFolderSyncObject property. Somewhat mysteriously named, the InAppFolderSyncObject property determines whether the folder will be added to the Application Folders Send/Receive group. For additional information on Send/Receive groups and offline use, see Chapter 8. "Folders." The following code example adds the Top Accounts folder to the Application Folders group and starts a synchronization programmatically:

Sub SyncTopAccounts()

Dim objFolder As Outlook.MAPIFolder

Dim objAppSync As SyncObject

Dim objOutlook As Outlook.Application

Set objOutlook = CreateObject("Outlook.Application")

Set objFolder = objOutlook.GetNamespace("MAPI") _

.GetDefaultFolder(olFolderlnbox) Set objFolder = objFolder.Folders("Top Accounts") objFolder.lnAppFolderSyncObject = True Set objAppSync = ThisOutlookSession.GetNamespace("MAPI") _ .SyncObjects.AppFolders objAppSync.Start End Sub

Returning a Folder from a Folder Path

The OpenMAPIFolder procedure allows you to return a MAPIFolder object if you supply a folder path as an argument. A folder path is expressed as follows:

Set objFolder= OpenMAPIFolder_ ("\Public Folders\AII Public Folders\Sales\Q401")

Function OpenMAPIFolder(ByVal strPath) 'As MAPIFolder Dim objFldr 'As MAPIFolder Dim strDir 'As String Dim strName 'As String Dim i 'As Integer On Error Resume Next If Left(strPath, Len("\")) = Then strPath = Mid(strPath, Len("\") + 1) Else

Set objFldr = Application.ActiveExplorer.CurrentFolder End If

While strPath <>"" i = lnStr(strPath, "\") If i Then strDir = Left(strPath, i -1) strPath = Mid (strPath, i + Len("\")) Else strDir = strPath strPath ="" End If

If objFldr Is Nothing then Set objFldr = Application.GetNameSpace("MAPI").Folders(strDir) On Error Goto 0 Else

Set objFldr = objFldr.Folders(strDir) End If Wend

Set OpenMAPIFolder = objFldr End Function

The type declarations have been commented out in the OpenMAPIFolder and GetFolderPath functions. If you want to use these functions in VBA or Visual Basic code, remove the comment marks.

Returning a Folder Path from a Folder

If you are using a version of Outlook prior to Outlook 2002, the GetFolderPath function allows you to return a string representing a folder path if you supply a MAPIFolder object as an argument. If you are using Outlook 2002, you simply use the FolderPath property of the MAPIFolder object to return a string that contains the folder path.

+1 0

Post a comment