0

这是在 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
4

2 回答 2

0

这是我使用的基础知识:

'Refers to Outlook's Application object
Dim appOutlook As Object

'Refers to an Outlook email message
Dim appOutlookMsg As Object

'Refers to an Outlook email recipient
Dim appOutlookRec As Object

'Create an Outlook session in the background
Set appOutlook = CreateObject("Outlook.Application")

'Create a new empty email message
Set appOutlookMsg = appOutlook.CreateItem(olMailItem)

'Using the new, empty message...
With appOutlookMsg

'SQL statement to grab emails

Set recordset = currentdb.openrecordset('SQL statement')

Do While Not recorset.EOF
Set appOutlookRec = .Recipients.Add(recordset.Email)
appOutlookRec.Type = olTo
recordset.MoveNext
Loop

.Subject = ....
.Body = ....
.Send

End With

这就是我使用的基础知识。我是初学者,所以这可能不是最好的方法,但它应该是一个开始。(我还必须在参考库中添加 Microsoft Oulook。)

于 2013-04-02T21:08:13.663 回答
0

我使用 CDO 对象来发送消息,因为我不喜欢依赖 Outlook(任何事情)。

这里有一篇关于使用 CDO 发送邮件(包括可下载的 VBA 代码)的相当全面的文章:

http://www.cpearson.com/excel/Email.aspx

于 2013-04-03T10:28:27.867 回答