[英]Working with Outlook using MS Access 2003 - VBA
我制作了以下程序,該程序從MS Access中的表格中獲取電子郵件地址列表,並發送每封測試電子郵件。 這是代碼:
'Get data back from field birth date
Set fld = rcdSet.Fields("Email Address")
'Subject
olMailItem.Subject = "Mailing List Test"
'Loop through the records
For i = 0 To intNumRecords
'Recipient/s
olMailItem.To = fld.Value
'Body of email
olMailItem.Body = strBodyText
'Automatically send the email
olMailItem.Send
'Reset email item otherwise it won't work
Set olMailItem = olFolder.Items.Add("IPM.Note")
'Move to the next record
rcdSet.MoveNext
Next
是的,我打開了一個記錄集,但沒有在上面包含該代碼。 所以這是我的問題:
我上面的方法正確嗎? 我必須在循環中重置olMailItem
,否則它將返回Type Error
。 有發送多個電子郵件的更好方法嗎?
我輸入了無效的電子郵件以查看會發生什么,這會導致另一個Type Error
。 無論如何,有沒有檢測到反彈(電子郵件Outlook向您發送通知,通知您這是一個錯誤的地址)電子郵件並將消息發送到Immediate Window
(此時用於開發目的)
謝謝
注意-添加了郵件項目的聲明
'Create Outlook object
Dim olApp As Outlook.Application
'Namespace
Dim olNS As Outlook.NameSpace
Dim olFolder As Outlook.MAPIFolder
'Create a reference to the email item you will use to send the email
Dim olMailItem As Outlook.MailItem
Set olApp = CreateObject("Outlook.Application")
Set olNS = olApp.GetNamespace("MAPI")
Set olFolder = olNS.GetDefaultFolder(olFolderInbox)
Set olMailItem = olFolder.Items.Add("IPM.Note")
如果我不重新設置olMailItem在for循環上出現錯誤.To
計划的一部分-上述Type Error
我不知道您聲明了IPM類型的對象,請注意並嘗試發送它,而是聲明一個mailitem。
這應該工作
'Loop through the records
For i = 0 To intNumRecords
Set olMailItem = olFolder.Items.Add
'Subject
olMailItem.Subject = "Mailing List Test"
'Recipient/s
olMailItem.To = fld.Value
'Body of email
olMailItem.Body = strBodyText
'Automatically send the email
olMailItem.Send
'Release new mailitem
Set olMailItem = Nothing
'Move to the next record
rcdSet.MoveNext
Next
檢出http://msdn.microsoft.com/zh-cn/library/office/bb220348(v=office.12).aspx ,以獲取有關add方法的更多信息。
編輯:看到完整循環
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.