Access Cookbook - Ken Getz [278]
Set cti = ola.CreateItem(olContactItem)
cti.FirstName = varFirstName & ""
cti.LastName = varLastName & ""
cti.HomeAddressStreet = varAddress & ""
cti.HomeAddressCity = varCity & ""
cti.HomeAddressState = varState & ""
cti.HomeAddressPostalCode = varPostalCode & ""
cti.Email1Address = varEmail & ""
Finally, the procedure calls the Display method of the ContactItem object to display the unsaved item. (If you want to save the item before displaying it, call the Save method before calling the Display method.) The Display method isn't synchronous—that is, the code continues running, releases the ContactItem object from memory, and cleans up the module-level variables created earlier.
TIP
You may wonder why releasing the cti, ola, and nsp variables doesn't close the contact editor and shut down Outlook. That would happen only if you never displayed the contact within an editor for the user to see. Once you do that, though, Outlook is effectively "owned" by the user, and unless you explicitly call the Quit method of the Outlook Application object, it's now up the user to close the contact editor. When that happens, Outlook will shut down because no other references to it exist. Of course, if Outlook had been running before you ran the code, it would continue to run afterwards, since the variables used within the procedures here are simply additional references to the running copy of Outlook.
The SendEmail procedure shown here works much like the AddContact procedure:
Public Sub SendEmail(varTo As Variant)
Dim mli As Outlook.MailItem
InitOutlook
Set mli = ola.CreateItem(olMailItem)
mli.To = varTo & ""
mli.Subject = "Message for Access Contact"
mli.Display
Set mli = Nothing
CleanUp
End Sub
SendEmail receives the email address of the recipient and creates a new email message addressed to that recipient in Outlook. (You could, of course, gather and pass more information for the email message, such as the subject, in this procedure call. The sample merely sends the recipient.) SendEmail sets the To field of the new email message, creates a subject for you, and then displays the new, unsent email message in Outlook. It's up to the user to complete and send the email message.
TIP
If you wanted to actually send a message programmatically, you could supply the Subject and Body fields (and any others you'd like to supply) in your code, and then call the Send method of the MailItem object. For this example, we've simply created the message and dumped you into the email editor in Outlook.
Of course, there's much more to the Outlook object model than we've been able to show here. Start by exploring the data provided by the VBA Object Browser (press F2 from within a VBA module, select Outlook from the list of libraries in the upper-left corner of the window, and start digging). You can find several good books on programming the Outlook object model, and don't forget to check out the online help.
Make sure to try out various versions of Outlook if you're shipping an application to end users. The Outlook security patch and the various versions of security models are sure to hamper your applications if you intend to work with contacts or send email to contacts in the address book programmatically.
See Also
If you want to "fake" sending email, using Access only, see Recipe 10.4 in Chapter 10.
Chapter 13. Data Access Pages
When data access pages (DAPs) were introduced in Access 2000, they drew a lot of attention from Access developers who were looking for easy ways to move their data to the Web. DAPs promised to provide an Access-based designer that would allow developers to create web pages based on data, just as forms and reports were. Unfortunately, because of the limitations of the DAP design tools, the often-crippled functionality of the resulting pages (such as the inability to update data), and the requirement that users of