【发布时间】:2016-05-09 14:09:33
【问题描述】:
我正在尝试从访问表中提取姓名和电子邮件地址列表,并使用该信息发送个性化电子邮件。所以我试图将 2 列从访问中调用到一个数组中,然后使用一个循环来遍历每一行。我认为这将是最好的方法,但我对其他想法持开放态度。我已经能够生成生成一封包含第一条记录的电子邮件的代码,但我不知道如何获取其余记录:
Private Sub SMT()
Application.Run "VariablesForRanges"
strDB1 = ThisWorkbook.Path & "\database.accdb"
strqry1 = "SELECT table1.name, table2.Email"
strqry1 = strqry1 & " FROM table2 inner join table1 on table1.FULL_NAME=table2.Name"
strqry1 = strqry1 & " group by table1.name, table2.Email;"
Set cn1 = New ADODB.Connection
cn1.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strDB1 & ";"
Set rs1 = New ADODB.Recordset
With rs1
Set .ActiveConnection = cn1
.Open strqry1
End With
If rs1.BOF = True Then
MsgBox "There Are No Records To Import", vbInformation
Else
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "Hi " & rs1("name") & "," & vbNewLine & vbNewLine & _
"test" & vbNewLine & vbNewLine & _
"Best regards," & vbNewLine & _
On Error Resume Next
With OutMail
.To = rs1("Email")
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End If
End Sub
任何有关创建数组和循环的帮助将不胜感激。谢谢。
【问题讨论】: