我正在學習Access和VBA。當我嘗試解釋我正在嘗試做什麼時,請和我一起裸照。如何從Access數據庫創建和發送電子郵件
我有產生這樣一個表的查詢 -
行1 - ID 1彼得Parker任務1 $ 50
行2 - ID 1彼得Parker任務2 $ 55
行3 - ID 1彼得·帕克任務3 $ 60
行4 - ID 2瑪麗珍任務1 $ 45
行5 ...
我希望能夠發送一個電子郵件給每個人提供的任務和數量,總金額列表 -
彼得·帕克
任務1 $ 50
任務2 $ 55
任務3 $ 60
總計$ 165
我有發送電子郵件的模塊,但我t每行需要一個接收者。我想我需要另一個循環,但是我失去了如何做到這一點。
這是我現在使用的代碼 -
Sub SendMessages(Optional AttachmentPath)
Dim MyDB As Database
Dim MyRS As Recordset
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachments
Dim TheAddress As String
Set MyDB = CurrentDb
Set MyRS = MyDB.OpenRecordset("qry_TeacherPayment - Round 2")
MyRS.MoveFirst
' Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application")
Do Until MyRS.EOF
'Set loop variables
Dim currentRecord As Integer
Dim oldRecord As Integer
Dim totalAmt As Double
currentRecord = MyRS![ID]
totalAmt = 0
If (currentRecord = MyRS![ID]) Then
' Create the e-mail message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
oldRecord = currentRecord
TheAddress = MyRS![WorkEmail]
With objOutlookMsg
' Add the To recipients to the e-mail message.
Set objOutlookRecip = .Recipients.Add("TheAddress")
objOutlookRecip.Type = olTo
' Set the from address.
objOutlookMsg.SentOnBehalfOfName = "email"
' Set the Subject, the Body, and the Importance of the e-mail message.
.Subject = "Subject"
objOutlookMsg.BodyFormat = olFormatHTML
body text
.HTMLBody = .HTMLBody & "</table></body></html>"
.Importance = olImportanceNormal 'Normal importance
' Resolve the name of each Recipient.
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
If Not objOutlookRecip.Resolve Then
objOutlookMsg.Display
End If
Next
.Send
End With
End If
MyRS.MoveNext
Loop
Set objOutlookMsg = Nothing
Set objOutlook = Nothing
DoCmd.SetWarnings True
End Sub
任何建議將非常感激!
乾杯!
傑森
到ATLEAST告訴你到目前爲止的代碼這將是有益的。這樣,人們可以給你想法或提示做什麼 – DragonSamu
謝謝DragonSamu。我已經使用我已用於單行收件人的代碼編輯我的帖子。我試圖在電子郵件的正文中添加一個循環,但不斷收到錯誤3021「沒有當前記錄」。 –