开发者

Send a message to each recipient in a list in a spreadsheet file

I am trying to handle multiple message objects.

Ultimately, I want to send a message to a list of recipients in a spreadsheet file.

Dim w As Outlook.Application
Dim wInbox As Outlook.MAPIFolder
Dim objOutlookMsg As Outlook.MailIt开发者_如何学JAVAem
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

-------Edit---------

What if I handle the code like this?

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
```


This worked for me...

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜