我尝试了如何使用以下代码段以编程方式处理多个消息对象,但是遇到了问题。最后,我想在电子表格文件中向收件人列表发送邮件。哪种对象和方法能够以最简单的方式完成工作?
Dim w As Outlook.Application
Dim wInbox As Outlook.MAPIFolder
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim count, x, msgnum As Integer
' Handle Microsoft outlook
Set w = GetObject(, "Outlook.Application")
If Err = ERR_APP_NOTRUNNING Then ' Open new instance if none is running
Set w = New Outlook.Application
wInbox = w.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
End If
'Count number of emails required
count = Cells(1, 2).End(xlDown).Row
msgnum = wInbox.Items.count
For x = 1 To count
Set objOutlookMsg = w.CreateItem(olMailItem)
msgnum = wInbox.Items.count
Next x
-------编辑--------- 如果我像这样处理代码怎么办?
Dim w As Outlook.Application
Dim wInbox As Outlook.MAPIFolder
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim count, x, msgnum As Integer
' Handle Microsoft outlook
Set w = GetObject(, "Outlook.Application")
If Err = ERR_APP_NOTRUNNING Then ' Open new instance if none is running
Set w = New Outlook.Application
End If
wInbox = w.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
'Count number of emails required
count = Cells(1, 2).End(xlDown).Row
msgnum = wInbox.Items.count
For x = 1 To count
Set objOutlookMsg = w.CreateItem(olMailItem)
msgnum = wInbox.Items.count
Next x
答案 0 :(得分:2)
这对我有用......
Sub TestOutlookSend()
Dim w As Outlook.Application
Dim wInbox As Outlook.MAPIFolder
Dim objOutlookMsg As Outlook.MailItem
Dim rngAddr As Range, recip As String
Set rngAddr = ThisWorkbook.Sheets("Sheet1").Range("A2")
Set w = GetObject(, "Outlook.Application")
Set wInbox = w.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
Do While rngAddr.Value <> ""
recip = rngAddr.Value
Set objOutlookMsg = w.CreateItem(olMailItem)
With objOutlookMsg
.To = recip
.Subject = "Hello " & recip
.Body = "A message for" & recip
.Send
End With
Set rngAddr = rngAddr.Offset(1, 0)
Loop
End Sub