Sending emails from excel using VBA

annux3

New Member
Joined
Mar 20, 2015
Messages
7
Hey,
I am having difficulties with putting a VBA code together. The code should send personalized emails taking data from different excel cells. I also need a greeting line such as Hello! and beginning line such as "Here is the overview of the used service:" and after that VBA code should take only needed data for every company. And end with "Best wishes, etc".
My excel table looks something like this, email addresses and company are duplicates so it would be nice if the code knew to take only what belongs to certain company:


PeriodNumberProviderServiceCountDurationTotalEmail
20150412345ETVPhone31332example@example.com
20150454321ETVPhone21223example@example.com
20150498765TallinkPhone81898this@email.com
20150456789TallinkPhone71771this@email.com

<tbody>
</tbody>




Final product should look like this:
First email
to example@example.com
Hello!
Here is the overview of the used service:
Period / Number / Provider / Count / Duration /
201504 / 900654 / ETV / 5 / 89 /
201504 / 900098 / ETV / 2 / 24 /
Best wishes,
My Name
*Signature preferably


Second email
to this@email.com
Hello!
Here is the overview of the used service:
Period / Number / Provider / Count / Duration /
201504 / 354864 / Tele2 / 7 / 64/
201504 / 354864 / Tele2 / 7 / 64/
Best wishes,
My Name
*Signature preferably


ETC.


This email needs to be sent in the beginning of every month. There are about 13-15 different emails - so 13-15 letters to be sent.
Any ideas?
Code that i found is provided in the comments, but it gives me this: Compile error: User-defined type not defined. Sub Mail_To_Friends() gets colored yellow.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Code that i found looks like this:




Sub Mail_To_Friends()
'Below Loop can be changed to While Loop or increase the limit (10) if your list has more than 10 mail ids
Dim SendTo As String
Dim ToMSg As String


For i = 1 To 10
SendTo = ThisWorkbook.Sheets(1).Cells(i, 1)
If SendTo <> “” Then
ToMSg = ThisWorkbook.Sheets(1).Cells(i, 3)
Send_Mail SendTo, ToMSg
End If
Next i
End Sub
Sub Send_Mail(SendTo As String, ToMSg As String)
Dim OutlookApp As Object
Dim OutlookMail As Object


Set OutlookApp = CreateObject(“Outlook.Application”)
Set OutlookMail = OutlookApp.CreateItem(0)


With OutlookMail
.to = SendTo
.CC = “”
.BCC = “”
.Subject = “Happy New Year”
.Body = ToMSg
.Display ' or just put .Send to directly send the mail instead of display
End With


Set OutlookMail = Nothing
Set OutlookApp = Nothing
End Sub






I have added few lines:


Dim Valitud As String
Dim Pakkuja As String
Dim Arv As String
Dim Kestvus As String


Valitud = ThisWorkbook.Sheets(3).Cells(i, 2)
Pakkuja = ThisWorkbook.Sheets(3).Cells(i, 3)
Arv = ThisWorkbook.Sheets(3).Cells(i, 4)
Kestvus = ThisWorkbook.Sheets(3).Cells(i, 5)
ToMSg = Valitud & Pakkuja & Arv & Kestvus 'This line should add up previous Strings - however i do not know if it does, because i cannot run the code
 
Upvote 0

Forum statistics

Threads
1,216,101
Messages
6,128,845
Members
449,471
Latest member
lachbee

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