这是在 Access 2010 中,我对 VBA 几乎没有经验或熟悉。
在我的表单 (frmEmailLookup) 中,我设置了组合框和列表框以及一个子表单,以便当用户从 cmbBuilding 选择建筑物时,表单的其余部分会填充该建筑物的数据,包括最多 4 个的联系电子邮件建筑物中的人员(lstBuildingRepEmail1、lstBuildingRepEmail2、lstBuildingRepEmail3、lstBuildingRepEmail4)。我需要一个按钮(butEmailRecords)来生成一封电子邮件,其中包含来自子表单(qryBuildingAreaLookup)的查询作为附件。我可以设置一个可以关闭的宏,但它不允许动态电子邮件地址。我不希望我的用户必须深入到程序中来进行更新。
感谢您提供任何帮助,我知道我正在寻求大量代码编写帮助。
这是我尝试过的:
Option Compare Database
Private Sub butEmailRecords_Click()
Dim outputFileName As String
outputFileName = CurrentProject.Path & "\BuildingInventory" & ".xlsx"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "qryBuildingAreaLookup", outputFileName, True
On Error GoTo Error_Handler
Dim objOutlook As Outlook.Application
Dim objEmail As Outlook.MailItem
Set objOutlook = CreateObject("Outlook.application")
Set objEmail = objOutlook.CreateItem(olMailItem)
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("qryBuildinAreaLookup")
With rs
With objEmail
.To = tblBuilding.BuildingRep1
.To = tblBuilding.BuildingRep2
.To = tblBuilding.BuildingRep3
.To = tblBuilding.BuildingRep4
.Subject = "Look at this sample attachment"
.body = "The body doesn't matter, just the attachment"
.Attachments.Add "L:\Administration\FacilityInventoryDatabase\BuildingInventory.xls x"
.Send
'.ReadReceiptRequested
End With
Exit_Here:
Set objOutlook = Nothing
Exit Sub
Error_Handler:
MsgBox Err & ": " & Err.Description
Resume Exit_Here
End Sub