OL98: Programmatically Import Outlook Items from MS Access (180981)



The information in this article applies to:

  • Microsoft Outlook 98

This article was previously published under Q180981

SUMMARY

This article provides an overview and sample code for creating Microsoft Outlook 98 Contacts from information stored in a Microsoft Access 97 database. The sample code in the "More Information" section of this article should be run from a Microsoft Access 97 global module and provides a starting point for the development of a complete solution.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. The Import and Export feature in Outlook 98 does not allow you to import or export information in user-defined fields (or properties). To import information into these fields, you may need to create a programming solution that uses Automation to convert the information.

The following sample code specifically converts information from Microsoft Access into newly-created Outlook Contacts, using Data Access Objects (DAO) version 3.5. With modifications, you can use this code as a basis for creating other types of Outlook items, such as appointments, notes, tasks, and so on.

There is also an overview and separate code example available on the Microsoft Web site at the following location. It is the first example in the whitepaper.

Programming Considerations

  • The following sample code uses the Northwind database included with Microsoft Office 97. If you installed Office to a location other than the default, be sure to change the path to the database in the code.
  • You need to set references to DAO 3.5 and the Outlook 8.5 Object Library.
  • The field types used in this example are text, in both Microsoft Access and Outlook. To convert other types of fields, you must modify the code appropriately.
  • The contacts are automatically placed in the default Contact folder, which is at the same level as the Inbox that receives incoming mail.
  • The CreateItem method used in this example creates a new Outlook contact using the default form for the default contact folder. If you created a custom Outlook Contact form, you must set the MessageClass field for each item to the name of your custom form.

Sample Code

Sub ExportAccessContactsToOutlook()

      ' Set up DAO Objects.
      Dim oDataBase As Object
      Dim rst As Object
      Set oDataBase = OpenDatabase _
         ("c:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb")
      Set rst = oDataBase.OpenRecordset("Customers")

      ' Set up Outlook Objects.
      Dim olns As Object               ' Outlook Namespace.
      Dim cf As Object                 ' Contact folder.
      Dim c As Object                  ' Contact item.
      Dim Prop As Object               ' User property.
      Dim ol As New Outlook.Application
      Set olns = ol.GetNamespace("MAPI")
      Set cf = olns.GetDefaultFolder(olFolderContacts)

      With rst
         .MoveFirst

         ' Loop through the Microsoft Access records.
         Do While Not .EOF

            ' Create a new Contact item.
            Set c = ol.CreateItem(olContactItem)

            ' Specify which Outlook form to use.
            ' Change "IPM.Contact" to "IPM.Contact.<formname>" if you've
            ' created a custom Contact form in Outlook.
            c.MessageClass = "IPM.Contact"

            ' Create all built-in Outlook fields.
            If ![CompanyName] <> "" Then c.CompanyName = ![CompanyName]
            If ![ContactName] <> "" Then c.FullName = ![ContactName]

            ' Create the first user property (UserField1).
            Set Prop = c.UserProperties.Add("UserField1", olText)

            ' Set its value.
            If ![CustomerID] <> "" Then Prop = ![CustomerID]

            ' Create the second user property (UserField2).
            Set Prop = c.UserProperties.Add("UserField2", olText)

            ' Set its value and so on....
            If ![Region] <> "" Then Prop = ![Region]

            ' Save the contact.
            c.Save
            .MoveNext
         Loop
      End With
   End Sub
				

REFERENCES

For more information about creating solutions with Microsoft Outlook 98, please see the following articles in the Microsoft Knowledge Base:

180826 OL98: Resources for Custom Forms and Programming

182349 OL98: Questions About Custom Forms and Outlook Solutions


Modification Type:MajorLast Reviewed:8/4/2005
Keywords:kbcode kbhowto kbProgramming KB180981