Hi all,
I have run into an issue creating a VBA macro which is supposed to send out mass-emails through Excel. I've attached my code below. Essentially, this macro works by pulling the information for the e-mail from various cells in Excel and continues down the rows where the information is stored, through a for loop. Currently, the body of the e-mail is something I have to store in column N in order for it to copy through. However, due to the character limit and formatting, as well as overall functionality I would like to implement a way where I can set a word document as the body of the e-mail for all the e-mails generated in the spreadsheet.
I have viewed many implementations of "word-doc as e-mail body in vba" but the issue I'm having is that it does not work with the code that I have written which has to iterate through all the rows of e-mails and copy the same body to them. Is there a way I can implement this functionality inside my code? Thanks.
---
Option Explicit
Sub Send_Mails()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Send_Mails")
Dim i As Integer
Dim OA As Object
Dim msg As Object
Set OA = CreateObject("outlook.application")
Dim last_row As Integer
last_row = Application.CountA(sh.Range("A:A"))
'loop through the range to send all emails
For i = 2 To last_row
Set msg = OA.createitem(0)
msg.to = sh.Range("A" & i).Value
msg.SentOnBehalfOfName = sh.Range("R2").Value
msg.bcc = sh.Range("B" & i).Value
msg.cc = sh.Range("C" & i).Value
msg.Subject = sh.Range("I" & i).Value
msg.body = sh.Range("N" & i).Value
If sh.Range("O" & i).Value <> "" Then
msg.attachments.Add sh.Range("O" & i).Value
End If
If sh.Range("P" & i).Value <> "" Then
msg.attachments.Add sh.Range("O" & i).Value
End If
msg.send
sh.Range("Q" & i).Value = "LOA sent"
Next i
MsgBox "All the LOAs have been sent successfully"
End Sub
Bookmarks