Connecting to a Microsoft Exchange or Outlook Folder

You can open a Microsoft Exchange or Outlook folder directly, link a folder to a Microsoft Access 97 table, or import the data in a folder to an existing Microsoft Access table. The following sections describe these methods of accessing data stored in Microsoft Exchange or Outlook folders.

Opening a Folder

To open a Microsoft Exchange or Outlook folder directly, use the connection string as an argument to the OpenDatabase method. Even though you are opening the folder directly, you must still specify a Microsoft Access database file that will contain the system files necessary to maintain the connection. This technique is useful when you don’t need to maintain a persistent connection to the data in a folder.

The following code fragment uses the OpenDatabase method to open the personal folder store, and prints the name of each top-level folder to the Debug window. In this example, strDbPath is the path to the database:

Dim dbs As Database
Dim tdf As TableDef
Dim strConnect As String

strConnect = "Exchange 4.0;MAPILEVEL=Personal Folders|;"
' Open Microsoft Exchange folder.
Set dbs = OpenDatabase(strDbPath, 0, 0, strConnect)

' Open each top-level folder.
For Each tdf In dbs.TableDefs
	Debug.Print tdf.Name
Next tdf
Debug.Print "Done"
dbs.Close
Linking to a Folder

When you create a link to a Microsoft Exchange or Outlook folder, the folder is treated as a linked table. The following example creates a linked table in the current database that contains all the entries in the Inbox folder. In this example, strDbPath is the path to the database:

Dim dbs As Database
Dim tdf As TableDef
Dim strConnect As String

' Open database.
Set dbs = OpenDatabase(strDbPath)

' Build the connection string.
strConnect = "Exchange 4.0;MAPILEVEL=Mailbox - David Jones" _
	& "|;TABLETYPE=0;" _
	& "DATABASE=" & dbs.Name & ";"

' Create a TableDef object. The name specified for the
' TableDef object is displayed as the name of the link
' in the Database window.
Set tdf = dbs.CreateTableDef("Linked Exchange Folder")
With tdf
	' Set connection information and specify a folder.
	.Connect = strConnect
	.SourceTableName = "Inbox"
End With
' Append the TableDef object to create the link.
dbs.TableDefs.Append tdf
Importing a Folder

To import the data in Microsoft Exchange or Outlook folders to an existing Microsoft Access table, you need to make sure that the table that will receive the data contains the fields you want to import. Once you have a table in the correct format, you establish a temporary link to the folder and run an append query to add records to the specified table.

See Also For more information about creating a table and importing data from a Microsoft Exchange or Outlook folder, see the white paper “Accessing Microsoft Exchange and Microsoft Outlook Data Using Visual Basic,” located in the Papers folder on the companion CD-ROM.