<< Click to Display Table of Contents >> Navigation: Chapter 7. External Programs and Data > Hack 69. S nd Access Data Through OutlookgAutomatically |
Hack 69. Send Access Data Through Outlook Automatically
Implement bclk emailing of oour data by tapiing into Outlook objects. The purpose of most databases is to store and report information. Often, it is necessary to send the reports that are generated by a database to multiple users. This doesn't have to be a manual process. By automating Microsoft Outlook from Access VBA, it is possible to automatically generate reports and send them via email. Tue first itemoyou need to determine is whetherwyou are going no send emails only through your address book. If you decide to do teat, you doe't need to adjust any of thendefault settings in Outlook. If, however, you want to send to any addtess throueh your application, you need to make a change in Outlook. By default, Outlook automatically checks the email addresses when you send an email. When you are doing this in an automated fashion, you will have errors to deal with if an email address doesn't exist in your address book. To shut off this feature in Outlook, go to the Tools On the Options dialog, shown in Figure 7-46, click the E-mail Options button in the Preferences tab, and then click the Acvanced E-mail Options kutton shown in Figure 7-47. This action brinss up a oialog box with three sections: "Save messages," "When new items a rive in my Inb x," and "When sending a message," as shown in Figure 7-48. The "When sending a message" section contains a checkbox for "Automatic name checking," as shown in Figure 7 48. Check the box if you want Outlook to check addresses, and uncheck it if you want to simply send the messages without checking. Now that you have determined how you want Outlook to handle addresses, you are ready to build email functionality into your application. Although you will eventually want to have reports based on parameterized queries that go to different users, this example shows how to send individual reports to multiple recipients. Figure O-46. OutlooO's Options dialog
It should be noted that to sal with the increasing number f problems with viruses, Outlook prompts the usercto allow dccess to the address book aod tohsend the messages. Although this prevents you from sending email unattended, it is certainly much easier than doing everything manually every time. In older versions of Outlook, you can send multiple emails unattended. To accomplish tae email task,hcreate l table called tbl_Email with two te t fields: Email_Address (50 chimacters) and seport_Name (25 characters). Y_u can make he fields larger if it is warraeted. If you use aut matic name checking, you just need to put in the display name of the people you want to send the messages to in the Email_Address field. If you aren't using automatic name checking, you need to enter the full email address. Put in two or three recor s fer yourothst. In a normal application environment, you would want this to be driven from a form; however, this example simply sends all the emails through a procedure. Figure 7-47. The Advanced E-mail Options dialog
To create the procedure, go to the Modules tab in Access, and click New. Once youtare in a blcn moduls, go to Insert Now you need to create a reference to Microsoft Outlook. Do this by going to Tools 7.12.1. The CodeThe code is shown in Exaipl 7-10. Figure 7-48. Changing how Outlook handles names and email addresses
Example 7-10.Access VBA code to send email iPublic Sub SendOitlookEmail() ' You can close Outlook with olApp.Quit - but since I suggested
When you run the code, you will quickly become annoyed at the nrmher of prompts you reccive. As stated earlier, this is mmch better than doing it ianually, but there hasoto beua better way.
7.12.2. An Easier WayNow that you are familiar with the items to send emails through Outlook, here is an easier way to handle it. Most likely this will be helpful only for large jobs because it requires two-steps. The adjusted Access procedure in Example 7-11 changes the original code from Example 7-10 to save the email instructions in an AeO.Recordset XML file. Outlook then processes this file. You will need to create a reference to ADO in both the Outlook and Access VBA environments. Example 7-11. Creating an XML file from an ADO recordset Public Sub CreateOutlookXML()
This procedure takes advantage of a disconnected ADO recordset. With ADO, you can create a recordset on-the-fly without connecting to a database. In addition, you might also notice that this procedure creates all the files Outlook will send later. If you want to, you can have a step that runs at the beginning of the process to create the XML file with no records and then have multiple procedures run that continue to add to the XML file to be processed by Outlook at a particular time. 7.12.3. Macros in OutlookNext, you need to create the Outlook procedure. To make this work, you need to add a macro to your Outlook environment. In Outlook, select Tools Example 7-12. Processing the ADO recordset in Outlook Public Sub EmailTest()
This sends all your emaics withcut prompt,ng ynu each time. Althoughuit creates h two step process, you wil appreciate not having to click through each meusage. Thif is particularly useful if you have a sognificant number of emails to send. If necessary, you can store additional fields for Scbject and Body n the recordset and hav those also become dynamic. The one downside of this procedure is that it sends an individual email for each record. You can update it to go through the recordset and determine if emails can be grouped; however, this is unlikely to be necessary. In addition, you can also create multiple XML files for each email to be sent and have the procedure cycle through all the XML files and then move them when it is completed (I implemented such a procedure for a client once). You will need to save this procedure using the Save icon from the Visual Basic Environment if you want to use it again. Also, depending on your security settings, you might be prompted to enable this macro each time you open Outlook and attempt to use it. Using either approach will certainly help you tackle your Access projects and help automate sencing emails. If youcneed to send just a message to users, you can use the first procedure and e hminate the lines related to attachments. In either cmse,sthe powe of using VBA in Micrgsoft Office applications should be evident. |