Excel VBA help to create outlook emails

rodeo

New Member
Joined
Nov 9, 2015
Messages
5
Hi,

I am trying to create an outlook email for multiple recipients.

I have 2 sheets 1 and 2.

I want the code in sheet 1 column B to look into Sheet 2 column A and pick up all the email addresses matched the codes and create an email with list recipients in tostring and do repeat task for second code till its empty.

Also attach files corresponding to that code in column c in sheet 1.

I have created a below code but not sure how to create a tostring in VBA with multiple recipients.



Sub GenerateEmail()
i = 2 ' selects row 2 ,since row 1 ,i am keeping for titles
Dim wbBook As Excel.Workbook
Dim doText As DataObject
Dim wsSheet As Excel.Worksheet
Dim x As Variant
Dim myemail As String
Dim myrange As Range
Dim n As Range
Dim sm2 As Range




Set wbBook = ThisWorkbook
Set sm2 = ThisWorkbook.Sheets("Sheet 2").Range("A2:A1000")
Set sm1 = ThisWorkbook.Sheets("Sheet 1").Range("B2:B1000")



Do Until ThisWorkbook.Sheets("Sheet 1").Cells(i, "B").Value = ""


EmailTo = tostring


BCC = ThisWorkbook.Sheets("Sheet 1").Range("J3").Value
Subj = ThisWorkbook.Sheets("Sheet 1").Range("J4").Value
Path = "N:\Folder 1\Folder 2\Folder 3\Folder 3\Result\"
FileName = ThisWorkbook.Sheets("Sheet 1").Cells(i, 3)
SM = ThisWorkbook.Sheets("Sheet 1").Cells(i, 2)




x = Replace(Range("Content1").Value, "
", Format(Range("GenerationMonth").Value, "mmmm"))
x = x & Replace(Range("Content2").Value, "
", Format(Range("GenerationMonth").Value, "mmmm-yyyy"))
x = x & ThisWorkbook.Sheets("Sheet 3").Range("Content3").Value
Msg = x






Application.ScreenUpdating = False
Application.StatusBar = "Preparing email..."
Application.DisplayAlerts = False



'Variables for MS Outlook.
'Variables for MS Outlook.






Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.SentOnBehalfOfName = "Cleint1@Hotmail.com"
.To = EmailTo
.BCC = "Cleint1@Hotmail.com"
.Subject = "This is my subject" & Format(DateAdd("m", -1, Date), "mmmm yyyy")
.Attachments.Add Path & FileName
.Display
.BodyFormat = olFormatPlain
.Body = Msg
'send
End With
i = i + 1
Set doText = Nothing
Application.CutCopyMode = False


Loop


Cells(7, "J").Value = "Outlook msg count =" & i - 1

Set OutMail = Nothing
Set OutApp = Nothing

Application.DisplayAlerts = False
Application.ScreenUpdating = False
'Workbooks(MyFile).Close




End Sub</projection></projection>
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Kindly read the forum rules (MrExcel Message Board FAQ). In particular:
•Avoid multiple questions of a similar nature. Duplicate posts by the same user will be locked and/or deleted when found. If the answer you receive is inadequate for some reason, post a reply stating why with more information (if/when needed) in the same thread. Do not start a new topic.
 
Upvote 0
Should you require any further information, please do let me know.

I need this solved urgently.

Thank you so much everyone.
 
Upvote 0
SHEET 1

[TABLE="width: 869"]
<colgroup><col><col><col><col><col span="2"><col><col><col></colgroup><tbody>[TR]
[TD]client name[/TD]
[TD]Client code[/TD]
[TD]Attachments[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Previous Month Date[/TD]
[TD]September 2015[/TD]
[/TR]
[TR]
[TD]Ab 1[/TD]
[TD]Client 1[/TD]
[TD]Client 1 RESULT Sep-15.zip[/TD]
[TD] [/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Email Count[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]CD 2[/TD]
[TD]Client 2[/TD]
[TD]Client 2 RESULT Sep-15.zip[/TD]
[TD] [/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]EF 3[/TD]
[TD]Client 3[/TD]
[TD]Client 3 RESULT Sep-15.zip[/TD]
[TD] [/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]GH 4[/TD]
[TD]Client 4[/TD]
[TD]Client 4 RESULT Sep-15.zip[/TD]
[TD] [/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]JK 5[/TD]
[TD]Client 5[/TD]
[TD]Client 5 RESULT Sep-15.zip[/TD]
[TD] [/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD] SHEET 2
[TABLE="width: 315"]
<colgroup><col><col><col></colgroup><tbody>[TR]
[TD]SM Codes[/TD]
[TD]Name[/TD]
[TD]Email (Preferred)[/TD]
[/TR]
[TR]
[TD]Client 1[/TD]
[TD]AB 1[/TD]
[TD]Client 1@HOTMAIL.COM[/TD]
[/TR]
[TR]
[TD]Client 1[/TD]
[TD]AB 1[/TD]
[TD]Client 1@HOTMAIL.COM[/TD]
[/TR]
[TR]
[TD]Client 1[/TD]
[TD]AB 1[/TD]
[TD]Client 1@HOTMAIL.COM[/TD]
[/TR]
[TR]
[TD]Client 1[/TD]
[TD]AB 1[/TD]
[TD]Client 1@HOTMAIL.COM[/TD]
[/TR]
[TR]
[TD]Client 2[/TD]
[TD]CD 2[/TD]
[TD]Client 2@HOTMAIL.COM[/TD]
[/TR]
[TR]
[TD]Client 2[/TD]
[TD]CD 2[/TD]
[TD]Client 2@HOTMAIL.COM[/TD]
[/TR]
[TR]
[TD]Client 2[/TD]
[TD]CD 2[/TD]
[TD]Client 2@HOTMAIL.COM[/TD]
[/TR]
[TR]
[TD]Client 3[/TD]
[TD]EF 3[/TD]
[TD]Client 3@HOTMAIL.COM[/TD]
[/TR]
[TR]
[TD]Client 3[/TD]
[TD]EF 3[/TD]
[TD]Client 3@HOTMAIL.COM[/TD]
[/TR]
[TR]
[TD]Client 4[/TD]
[TD]GH 4[/TD]
[TD]Client 4@HOTMAIL.COM[/TD]
[/TR]
[TR]
[TD]Client 5[/TD]
[TD]JK 5[/TD]
[TD]Client 5@HOTMAIL.COM[/TD]
[/TR]
[TR]
[TD]Client 5[/TD]
[TD]JK 5[/TD]
[TD]Client 5@HOTMAIL.COM[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Hi,

I am trying to get the below code in the above to create a to string




Lastrow = Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
For Each myrange In Sheets("Sheet2").Range("A2:A" & Lastrow)
If myrange = Sheet1 Then
myemail = myemail & myrange.Offset(0, 4).Value & ";"
End If
Exit For


The problem I am facing is every email it generates it picks up all the email addresses from the previous email generated...

I wanted to generate each email for each client.

your input will be much appreciated.

Thank you
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top