Share this Page URL

Chapter 5. Using Visual Basic to Automat... > Chapter Summary - Pg. 190

Using Visual Basic to Automate Your Database Public Sub MailMessage() Dim appOutlook As New Outlook.Application Dim nsOutlook As NameSpace Dim msFolder As MAPIFolder Dim msgItem As MailItem Dim myAttach As Attachments Set Set Set Set nsOutlook = appOutlook.GetNamespace("MAPI") msFolder = nsOutlook.GetDefaultFolder(olFolderInbox) msgItem = appOutlook.CreateItem(olMailItem) myAttach = msgItem.Attachments 190 msgItem.Display 'Change this e-mail address msgItem.To = "" msgItem.Subject = "Updated Expenses Categories" msgItem.Body = "I've updated the expense categories. The file is attached." myAttach.Add FilePath & "ch05\campaignexpenses.xls" 'msgItem.Send End Sub The TransferData procedure uses the constant ac Export (rather than acImport ) to export data as a spreadsheet, but otherwise, it's similar to the Import procedure. If a user clicks Yes to export the campaign expense data, the procedure exports the table's data to the Excel file named Cam- paignExpenses.xls and then calls the MailMessage procedure. The MailMessage procedure's variable declarations refer to objects in the Outlook object model-- Application, NameSpace, MAPIFolder, MailItem, and Attachments. The NameSpace object is used for accessing data sources in Outlook. MAPI is the only type of namespace supported, and this value is provided to the GetNameSpace method at the start of the series of Set statements. By using the NameSpace object's GetDefaultFolder method, the MAPIFolder object ( msFolder ) is set to the