View Single Post
 
Old 07-24-2015, 08:00 AM
Baldeagle Baldeagle is offline Windows 8 Office 2013
Advanced Beginner
 
Join Date: Apr 2012
Posts: 62
Baldeagle is on a distinguished road
Default Send Emails from MailMerge in Word using Specified Email Address

A colleague has this morning used the following Macro to send about 240 personalised emails generated through Word MailMerge.

Code:
Sub EmailMergeWithAttachments()
'
' EmailMergeWithAttachments Macro
'
'
Dim Source As Document, Maillist As Document, TempDoc As Document
Dim Datarange As Range
Dim i As Long, j As Long
Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
Dim mysubject As String, message As String, title As String

Set Source = ActiveDocument

' Check if Outlook is running.  If it is not, start Outlook
On Error Resume Next
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
    Set oOutlookApp = CreateObject("Outlook.Application")
    bStarted = True
End If

' Open the catalog mailmerge document
With Dialogs(wdDialogFileOpen)
    .Show
End With
Set Maillist = ActiveDocument

' Show an input box asking the user for the subject to be inserted into the email messages
message = "Enter the subject to be used for each email message."    ' Set prompt.
title = " Email Subject Input"    ' Set title.
' Display message, title
mysubject = InputBox(message, title)

' Iterate through the Sections of the Source document and the rows of the catalog mailmerge document,
' extracting the information to be included in each email.
For j = 1 To Source.Sections.Count - 1
    Set oItem = oOutlookApp.CreateItem(olMailItem)
    With oItem
        .Subject = mysubject
        .Body = Source.Sections(j).Range.Text
        Set Datarange = Maillist.Tables(1).Cell(j, 1).Range
        Datarange.End = Datarange.End - 1
        .To = Datarange
        For i = 2 To Maillist.Tables(1).Columns.Count + 1
            Set Datarange = Maillist.Tables(1).Cell(j, i).Range
            Datarange.End = Datarange.End - 1
            .Attachments.Add Trim(Datarange.Text), olByValue, 1
        Next i
        .Send
    End With
    Set oItem = Nothing
Next j
Maillist.Close wdDoNotSaveChanges

'  Close Outlook if it was started by this macro.
If bStarted Then
    oOutlookApp.Quit
End If

MsgBox Source.Sections.Count - 1 & " messages have been sent."

'Clean up
Set oOutlookApp = Nothing

End Sub
Unfortunately in spite of the fact that he had set his official IMAP email address as the default in Outlook all the emails were sent from his personal POP account.

I have come across the following code which is supposed to specify the email account to be used -

Code:
Public Sub New_Mail()
Dim oAccount As Outlook.Account
Dim oMail As Outlook.MailItem
 
For Each oAccount In Application.Session.Accounts
   If oAccount = "Name_of_Default_Account" Then
      Set oMail = Application.CreateItem(olMailItem)
      oMail.SendUsingAccount = oAccount
      oMail.Display
   End If
Next
End Sub
But I am not very proficient in writing code and would appreciate guidance on how I can adapt this code and insert it at an appropriate place in the Macro that is being used. Can somebody please tell me how and where this can be inserted and confirm that this will achieve the desired result?
Reply With Quote