【问题标题】:Getting Access to send emails to dynamic addresses获得将电子邮件发送到动态地址的访问权限
【发布时间】:2015-07-22 03:45:48
【问题描述】:

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

【问题讨论】:

  • 欢迎使用 Stack Overflow - 通常您需要发布一些您尝试过的特定问题的代码但无法正常工作 - 这个问题非常模糊,需要其他人为您完成工作 -这可能会被关闭。
  • 我有一个电子邮件宏,它使用循环根据需要通过的部门获取用户的所有电子邮件。那么唯一需要更新的就是员工名单了。
  • 感谢 TKEyi60。您是否使用 EMailDatabaseObject 宏?如果是这样,如何让“To”引用表中的字段?
  • 如果有的话会抛出什么错误?或者它做什么/不做什么?

标签: ms-access vba ms-access-2010


【解决方案1】:

这是我使用的基础知识:

'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。)

【讨论】:

  • 在 set resordset = currentdb.openrecordset 部分中,我指的是要作为附件发送的查询,对吧?
  • 我想您可以将其设置为附件,但我不确定如何通过 VBA 实际附加任何内容。我的只写在正文中,我的查询会提取电子邮件地址并将其放在收件人字段中。
  • 谢谢。我会再次尝试询问我已经走了多远。
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多