我希望有人可以幫助我與下面的代碼。嘗試使用特定的電子郵件帳戶(不是默認設置)從Outlook 2010發送電子郵件,該電子郵件基於靜態模板,該模板從表格(發件人_表格)中爲電子郵件正文中的(TO:主題和一些可變字段)提取數據。到目前爲止,下面的代碼的工作原理除了它不循環我的表中的所有記錄。電子郵件通過指定的帳戶發出,並從電子郵件中的表格中提取適當的數據,但在第一條記錄後停止。訪問VBA從DAO記錄集發送outlook.mailitem與循環不循環通過整個表
Private Sub test_Click()
'You must add a reference to the Microsoft Outlook Library
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim strbody As String
Dim stremail As String
Dim strsubject As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("Senders_Table")
With rs
If .EOF And .BOF Then
MsgBox "No emails will be sent becuase there are no records assigned from the list", vbInformation
Else
Do Until .EOF
stremail = ![email]
strsubject = ![address]
strbody = "Dear " & ![name] & "," & _
Chr(10) & Chr(10) & "Some kind of greeting" & ![address] & "!" & _
" email message body goes here"
.Edit
.Update
.MoveNext
Loop
End If
End With
On Error Resume Next
With OutMail
.To = stremail
.CC = ""
.BCC = ""
.Subject = strsubject
.Body = strbody
.SendUsingAccount = OutApp.Session.Accounts.Item(2)
.Send
End With
On Error GoTo 0
If Not rs Is Nothing Then
rs.Close
Set rs = Nothing
End If
Set OutMail = Nothing
Set OutApp = Nothing
End Sub