+ Reply to Thread
Results 1 to 2 of 2

Implementing functionality to use word doc as e-mail body inside a for loop: Mail Macro

Hybrid View

  1. #1
    Registered User
    Join Date
    11-09-2021
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2016
    Posts
    1

    Question Implementing functionality to use word doc as e-mail body inside a for loop: Mail Macro

    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
    Last edited by AliGW; 11-09-2021 at 12:16 PM. Reason: Code tags added.

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2507 (Windows 11 Home 24H2 64-bit)
    Posts
    91,780

    Re: Implementing functionality to use word doc as e-mail body inside a for loop: Mail Macr

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between [code] [/code] tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional. As you are new here, I have done it for you this time.)
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Excel data inside e-mail body
    By Bellaggio1770 in forum Outlook Formatting & Functions
    Replies: 5
    Last Post: 05-23-2016, 10:07 AM
  2. Macros for Mail Range/Selection in the body of the mail along with attachment
    By xenon in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-20-2015, 06:29 AM
  3. [SOLVED] Send mail, loop, Body of the message
    By Fean in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-24-2015, 05:22 AM
  4. E-mail drafer macro leaves the body of the mail blank.
    By Gabor in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-25-2014, 05:00 PM
  5. Send e-mail with image on body (.html) on e-mail manager <> outlook
    By mariotnc in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-05-2012, 09:28 PM
  6. Replies: 1
    Last Post: 07-22-2012, 09:26 AM
  7. Word Format as Body of mail using Send mail
    By andy_iyeng in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-19-2012, 05:04 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1