This seems to work. I already had the SendMAPIMessage subroutine in place in an app of my own, so I just had to write the BuildMessage part.
In the VBA window, you'll need to go to Tools - References and select the reference to Microsoft Outlook xx.0 Object Library to make it work. They're in alpha order.
Good luck!
Option Explicit
Dim Rw As Long, Col As Long, Cnt1 As Long, Cnt2 As Long
Dim Recipient As String, CCRecipient As String, BodyTxt As String
Dim NewLine As String
Const AddressExt = "@yourcompany.com"
Sub BuildMessage()
NewLine = Chr(10)
Recipient = "thisone; thatone" ' This could be built from your spreadsheet as well
CCRecipient = "someoneelse"
BodyTxt = "Good morning all, today we had " & Range("A1").Value & " new donors to our charity."
BodyTxt = BodyTxt & NewLine & NewLine ' Double-space
Rw = 1
Col = 4 ' Word column
Cnt1 = 0
' This will only work if the words are sorted
Do Until Cells(Rw, Col).Value = ""
If Cells(Rw, Col).Value = Cells(Rw + 1, Col).Value Then
Cnt1 = Cnt1 + 1
Else
If Cnt1 > 14 Then
BodyTxt = BodyTxt & Cnt1 & " " & Cells(Rw, Col).Value & " donated to the charity."
BodyTxt = BodyTxt & NewLine
End If
Cnt1 = 0
End If
Rw = Rw + 1
Loop
Rw = 1
Col = 6 ' Yes/No column
Cnt1 = 0
Cnt2 = 0
Do Until Cells(Rw, Col).Value = ""
If UCase(Cells(Rw, Col).Value) = "YES" Then
Cnt1 = Cnt1 + 1
ElseIf UCase(Cells(Rw, Col).Value) = "NO" Then
Cnt2 = Cnt2 + 1
End If
Rw = Rw + 1
Loop
BodyTxt = BodyTxt & NewLine
BodyTxt = BodyTxt & Cnt1 & " said they would attend the benefit dinner." & NewLine
BodyTxt = BodyTxt & Cnt2 & " said they would not attend the benefit dinner." & NewLine & NewLine
BodyTxt = BodyTxt & "Keep up the good work!"
Call SendMAPIMessage(Recipient, CCRecipient, BodyTxt, "Charity Update")
End Sub
Sub SendMAPIMessage(MsgTo As String, MsgCC As String, MsgTxt As String, MsgSubject As String)
Dim TempArray() As String, varArrayItem As Variant, strEmailAddress As String
Dim objOL As Outlook.Application
Dim MAPISession As Outlook.Namespace, MAPIFolder As Outlook.MAPIFolder, MAPIMailItem As Outlook.MailItem
Dim oRecipient As Outlook.Recipient
Set objOL = New Outlook.Application
Set MAPISession = objOL.Application.Session
If Not MAPISession Is Nothing Then
MAPISession.Logon , , True, False
Set MAPIFolder = MAPISession.GetDefaultFolder(olFolderOutbox)
If Not MAPIFolder Is Nothing Then
Set MAPIMailItem = MAPIFolder.Items.Add(olMailItem)
If Not MAPIMailItem Is Nothing Then
With MAPIMailItem
TempArray = Split(MsgTo, ";")
For Each varArrayItem In TempArray
strEmailAddress = Trim(varArrayItem)
If Len(strEmailAddress) > 0 Then
Set oRecipient = .Recipients.Add(strEmailAddress & AddressExt)
oRecipient.Type = olTo
Set oRecipient = Nothing
End If
Next varArrayItem
TempArray = Split(MsgCC, ";")
For Each varArrayItem In TempArray
strEmailAddress = Trim(varArrayItem)
If Len(strEmailAddress) > 0 Then
Set oRecipient = .Recipients.Add(strEmailAddress & AddressExt)
oRecipient.Type = olCC
Set oRecipient = Nothing
End If
Next varArrayItem
.Subject = MsgSubject
If StrComp(Left(MsgTxt, 1), "<", vbTextCompare) = 0 Then
.HTMLBody = MsgTxt
Else
.Body = MsgTxt
End If
On Error GoTo SendErr
.Send
On Error GoTo 0
Set MAPIMailItem = Nothing
End With
End If
Set MAPIFolder = Nothing
End If
MAPISession.Logoff
End If
Exit Sub
SendErr:
MsgBox "Unable to send message to " & MsgTo & " " & MsgCC & vbCrLf & vbCrLf & Err.Description
End Sub
Bookmarks