forked from tsh3lley/Automatic-Emailer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAutomaticEmailScript.txt
200 lines (147 loc) · 5.64 KB
/
AutomaticEmailScript.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
Option Explicit
'REMEMBER TO ADD REFERENCES! (Tools->References->Check the boxes for "Microsoft Scripting Runtime" and "Microsoft Outlook ##.# Object Library")
'YOUR EXCEL SHEET MUST BE FORMATTED AS:
' | A | B | C | D | E
' 1 | email address | first & last name | name of company | CC Email | body of email
' 2 |john.doe@yahoo.com | John Doe | National Oil Co | yourboss@aol.com|I am writing to alert you of a recent change in company policy...
' 3 |jsmith19@gmail.com | Jane Smith | Github.com | team@gmail.com |
' 4 | ccole490@aol.com | Karen Cole | United Coal | |
'Remember to open and log into outlook before running this script.
'Press "F5" or click the play button in the toolbar to run.
'Your message will contain <!FirstName Lastname!> and <!Company!> in the confirmation window, but these values will match your columns B and C when they are sent.
'Warning: if you run this script, emails will be sent as soon as you confirm your message content!. Make sure your message is correct.
'EMAIL BODY
'There is no need to add a salutation, emails will automatically contain "Dear Firstname Lastname," and a blank line.
'Write your message in cell E2, using <!Company!> where you want the company variable to be placed.
'Use <!Enter!> on its own line to have a blank line.
'Ensure that your desired formatting matches the formatting in the confirmation window.
'************************************
'Program created by:
'Thomas Shelley and John Mccutcheon
'For:
'The United States Embassy, Chengdu
'************************************
Sub SendEmail()
Dim skipLine As String
Dim outlookApp As Object
Dim mItem As Object
Dim cell As Range
Dim filePath As String
Dim eAddress As String
Dim msgCompany As String
Dim msgString As String
Dim msgName As String
Dim msgGreeting As String
Dim msgSubject As String
Dim msgBody As String
Dim eAttachment As String
Dim ccString As String
Dim boolAttach As Boolean
Dim numMails As Integer
'set up variables for use in the confirmation window
msgCompany = "<Company Name>"
msgName = "<!FirstName LastName!>"
msgGreeting = "Dear " & msgName & "," & skipLine
skipLine = " " & vbNewLine & " " & vbNewLine
msgSubject = InputBox("Enter the subject of your Email")
numMails = 0
Set outlookApp = CreateObject("Outlook.Application")
'add attachments
Dim responseInt As Integer
Dim strPrompt As String
Dim strTitle As String
strPrompt = "Would you like to add attachments?"
strTitle = "Attach Files"
responseInt = MsgBox(strPrompt, vbYesNo, strTitle)
If responseInt = vbYes Then
Dim filter As String, title As String, msg As String
Dim i As Integer, filterIndex As Integer
Dim fileName As Variant
filter = "Excel Files (*.xls),*.xls," & _
"Text Files (*.txt),*.txt," & _
"All Files (*.*),*.*"
filterIndex = 3
title = "Select File(s) to Attach"
With Application
fileName = .GetOpenFilename(filter, filterIndex, title, , True)
End With
If Not IsArray(fileName) Then
MsgBox "No file was selected."
boolAttach = False
End If
boolAttach = True
End If
Dim confirm As Integer
Dim prompt As String
Dim t As String
'show the confirmation window
msgString = Range("E2").Text
msgString = Replace(msgString, "<!Enter!>", " ")
msgString = Replace(msgString, "<!enter!>", " ")
prompt = "Your Email reads: " & skipLine & msgGreeting & skipLine & msgString & skipLine & "Are you sure you want to send?"
t = "Send Emails"
confirm = MsgBox(prompt, vbYesNo, t)
If confirm = vbNo Then
Exit Sub
End If
Dim ccCounter As Integer
ccCounter = 0
On Error GoTo noCC:
'add CCs
For Each cell In Columns("D").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value Like "*@*" Then
ccString = ccString & cell.Value & "; "
End If
Next
noCC:
Resume Next
'create the email variables
For Each cell In Columns("A").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value Like "*@*" Then
eAddress = cell.Offset(0, 0).Value
msgName = cell.Offset(0, 1).Value
msgCompany = cell.Offset(0, 2).Value
msgGreeting = "Dear " & msgName & "," & skipLine
msgBody = Replace(msgString, "<!Company!>", msgCompany)
msgBody = Replace(msgBody, "<!company!>", msgCompany)
Set mItem = outlookApp.CreateItem(0)
'send the emails
With mItem
.To = eAddress
.Subject = msgSubject
.Body = msgGreeting & vbNewLine & msgBody
If ccString <> "" Then
.cc = ccString
End If
If boolAttach = True Then
For i = LBound(fileName) To UBound(fileName)
.Attachments.Add fileName(i)
Next i
End If
.Send
End With
numMails = numMails + 1
End If
Next
MsgBox (numMails & " Messages Sent!")
End Sub
'function to add attachements
Function selectFile()
Dim fd As FileDialog, fileName As String
On Error GoTo ErrorHandler
Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.AllowMultiSelect = True
If fd.Show = True Then
If fd.SelectedItems() <> vbNullString Then
fileName = fd.SelectedItems()
End If
Else
End
End If
selectFile = fileName
Set fd = Nothing
Exit Function
ErrorHandler:
Set fd = Nothing
MsgBox "Error " & Err & ": " & Error(Err)
End Function