Microsoft KB Archive/161012

= VBA: How to Create a New Contact Item in Outlook with Automation =

Article ID: 161012

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition
 * Microsoft Outlook 97 Standard Edition

-



This article was previously published under Q161012



SUMMARY
Advanced: Requires expert coding, interoperability, and multiuser skills.

This article shows you how to use Automation from a Microsoft Access form to start Microsoft Outlook and display a new contact screen for input. You can change just one line of code to make this example apply to a new Microsoft Outlook appointment, journal entry, mail message, note, post, or task.

For information about how to run Microsoft Schedule+ with Automation, please see the following article in the Microsoft Knowledge Base:

147633 ACC: How to Run Schedule+ from MS Access Using Automation

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.



MORE INFORMATION
The following example shows how to create a form in Microsoft Access that starts Microsoft Outlook from a command button. Then, the Automation code opens a new contact screen for input in Microsoft Outlook. After you enter the contact, and save and close the contact form, the Automation code quits Microsoft Outlook and returns to the Microsoft Access form.

To run Microsoft Outlook from a Microsoft Access form, follow these steps:

 Open the sample database Northwind.mdb.  Create a new form not based on any table or query in Design view:      Form: FrmOutlook ---     Caption: Outlook Form

Command button: Name: RunOutlook Caption: Start Outlook OnClick: =StartOutlook  On the View menu, click Code to open the form module. On the Tools menu, click References. Click Microsoft Outlook 8.0 Object Library in the Available References box. If it does not appear in the list, click the Browse button and browse for MSOutl8.olb, which is installed by default in the C:\Program Files\Microsoft Office\Office folder. Click OK to close the References dialog box.</li>  Type the following procedure in the form's code module: Public Function StartOutLook On Error GoTo StartOutLook_Error Dim spObj As Object, MyItem As Object

' Create a Microsoft OutLook object. Set spObj = CreateObject("Outlook.Application")

' Create and open new contact form for input. ' You can substitute olAppointmentItem, olJournalItem, olMailItem, ' olNoteItem, olPostItem, or olTaskItem for olContactItem. Set MyItem = spObj.CreateItem(olContactItem) MyItem.Display

' Quit Microsoft Outlook. Set spObj = Nothing Exit Function

StartOutLook_Error: MsgBox "Error: " & Err & " " & Error Exit Function End Function </li> Close the module and switch the form to Form view.</li> Click the Start Outlook button. Note that Microsoft Outlook starts and displays a new contact screen.</li></ol>

<div class="references_section">