Once all of the Office documents selected in the form frmMain are automatically generated, you can send them as an e-mail message created in Outlook. This link gives you a convenient way to update people about a project.
Sub SendEmail(bReport As Boolean, bSheet As Boolean, _ bPres As Boolean) Dim appOL As New Outlook.Application Dim itemUsageEmail As Outlook.MailItem Set itemUsageEmail = appOL.CreateItem(olMailItem) End Sub |
The three Boolean variables you pass into the SendEmail procedure indicate which check boxes were selected in the form frmMain. You use these variables to determine which file attachments should be added to the e-mail message. Within the SendEmail procedure, you declare two variables. The first, the variable appOL, is the declaration of a new instance of the Outlook Application object. You use the keyword New in the declaration statement to indicate that the first time the variable appOL is used in code, you create a new instance. The second, the variable itemUsageEmail, declares the variable as an Outlook MailItem object, which you set in the Set statement following the declaration.
With itemUsageEmail .To = "Energy Committee" .Subject = "New energy usage documents" .Body = "The following documents reflect" & _ " the energy usage for the circuit: " & _ g_sCircuit & "." & vbCrLf .Display End With |
The With…End block sets the main properties of the e-mail message and then displays the message on the screen. If you want to send the e-mail message automatically, you can replace the Display method with the Send method in the last line of the With…End block. The message will go to the alias "Energy Committee."
If bReport = True Then .Attachments.Add g_sDBProjectPath & "Report.doc" End If If bSheet = True Then .Attachments.Add g_sDBProjectPath & "DataAnalysis.xls" End If If bPres = True Then .Attachments.Add g_sDBProjectPath & "EnergyPres.ppt" End If |
The three If…Then condition blocks evaluate the Boolean values representing the items selected in the form frmMain. If an item is selected and the appropriate Office document is generated, the document is attached to the e-mail message.
You've now added all the code needed to create an Outlook e-mail message. Later, when you run the integrated Office solution and select the item to create an Outlook e-mail message along with the other Office documents, the Outlook e-mail message should look like this:
NOTE
Before running the integrated solution, remove the apostrophe at the beginning of the Option Explicit statement in the modMain module if you previously added an apostrophe. The Option Explicit statement will enable you to find coding errors more easily (if any exist).
Depending on your computer system's hardware and software configuration, it may take a few minutes to iterate through the data in the Access database and generate each Office document. You can watch the progress by reading the Number Of Records Read indicator in the dialog box. Once the Main procedure finishes running, you'll see the new Word report, the Excel worksheet and chart, the PowerPoint presentation, and the Outlook e-mail message with the Office documents attached. Each window displayed on your screen should look similar to the graphic shown at the end of the example that created that particular Office document.
TIP
If your code has errors you can't easily resolve, or if you didn't have time to complete this chapter, you can run the solution from the Energy.mdb file provided in the Chapter 8 folder of the practice files. This file contains the complete working code for all the sections in this chapter.