-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEXCEL_OUTLOOK_SEND_LIBR.bas
executable file
·308 lines (238 loc) · 8.91 KB
/
EXCEL_OUTLOOK_SEND_LIBR.bas
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
Attribute VB_Name = "EXCEL_OUTLOOK_SEND_LIBR"
'-----------------------------------------------------------------------------------
Option Explicit 'Requires that all variables to be declared explicitly.
Option Base 1 'The "Option Base" statement allows to specify 0 or 1 as the
'default first index of arrays.
'-----------------------------------------------------------------------------------
Private Declare PtrSafe Function GetDesktopWindow Lib "user32" () As Long
Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" _
(ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
Private Const SW_SHOWNORMAL As Long = 1
Private Const SW_SHOWMAXIMIZED As Long = 3
Private Const SW_SHOWDEFAULT As Long = 10
Private Type EmailInfo
sAddrTo As String
sAddrCC As String
sAddrBCC As String
sAddrFrom As String
sSubject As String
sMessage As String
sPriority As Long
End Type
'************************************************************************************
'************************************************************************************
'FUNCTION : OUTLOOK_SEND_ATTACHMENT_FUNC
'DESCRIPTION : Send Large Emails in Microsoft Outlook
'LIBRARY : OUTLOOK
'GROUP : SEND
'ID : 001
'AUTHOR : RAFAEL NICOLAS FERMIN COTA
'************************************************************************************
'************************************************************************************
Function OUTLOOK_SEND_ATTACHMENT_FUNC(ByVal TO_STR_NAME As String, _
ByVal SUBJECT_STR_NAME As String, _
ByVal TEXT_MSG As String, _
ByVal FILE_PATH_STR As String, _
Optional ByVal CC_STR_NAME As String = "", _
Optional ByVal BCC_STR_NAME As String = "")
'TEMP_MSG = ""
'TEMP_MSG = TEMP_MSG & "Nicotico "
'TEMP_MSG = TEMP_MSG & "-" & vbCrLf & vbCrLf
'Debug.Print OUTLOOK_SEND_EXPRESS_MAIL_FUNC("rnfermincota@gmail.com", _
"Re:", TEMP_MSG)
Dim OUTLOOK_OBJ As Outlook.Application
Dim OUTLOOK_MSG_OBJ As Outlook.MailItem
'Dim OUTLOOK_RECIPIENT_OBJ As Outlook.Recipient
'Dim OUTLOOK_ATTACHMENT_OBJ As Outlook.Attachment
On Error GoTo ERROR_LABEL
OUTLOOK_SEND_ATTACHMENT_FUNC = False
Set OUTLOOK_OBJ = CreateObject("Outlook.Application")
Set OUTLOOK_MSG_OBJ = OUTLOOK_OBJ.CreateItem(olMailItem)
If OUTLOOK_VALIDATE_EMAIL_ADDRESS_FUNC(TO_STR_NAME) = False Then: GoTo ERROR_LABEL
With OUTLOOK_MSG_OBJ
.To = TO_STR_NAME
.CC = CC_STR_NAME
.BCC = BCC_STR_NAME
.Subject = SUBJECT_STR_NAME
.body = TEXT_MSG
.Attachments.Add (FILE_PATH_STR)
.ReadReceiptRequested = True
.display
.send
End With
Set OUTLOOK_OBJ = Nothing
Set OUTLOOK_MSG_OBJ = Nothing
OUTLOOK_SEND_ATTACHMENT_FUNC = True
Exit Function
ERROR_LABEL:
On Error Resume Next
Set OUTLOOK_MSG_OBJ = Nothing
Set OUTLOOK_OBJ = Nothing
OUTLOOK_SEND_ATTACHMENT_FUNC = False
End Function
'************************************************************************************
'************************************************************************************
'FUNCTION : OUTLOOK_SEND_EXPRESS_MAIL_FUNC
'DESCRIPTION : Send Large Emails in Microsoft Outlook
'LIBRARY : OUTLOOK
'GROUP : EMAIL
'ID : 002
'AUTHOR : RAFAEL NICOLAS FERMIN COTA
'************************************************************************************
'************************************************************************************
Function OUTLOOK_SEND_EXPRESS_MAIL_FUNC(ByVal FROM_STR_NAME As String, _
ByVal TO_STR_NAME As String, _
ByVal SUBJECT_STR_NAME As String, _
ByVal TEXT_MSG As String, _
Optional ByVal CC_STR_NAME As String = "", _
Optional ByVal BCC_STR_NAME As String = "", _
Optional ByVal PRIOR_INDEX As Long = 2, _
Optional ByRef SRC_PATH_NAME As Variant = "")
'PRIOR_INDEX: 2 --> High; 3 --> Mid; 4 --> Low Priority
' TEMP_MSG = ""
' TEMP_MSG = TEMP_MSG & "Nicotico "
' TEMP_MSG = TEMP_MSG & "-" & vbCrLf & vbCrLf
' Debug.Print OUTLOOK_SEND_EXPRESS_MAIL_FUNC("rnfermincota@gmail.com", _
"rafael_nicolas@hotmail.com", _
"Re:", TEMP_MSG)
Dim ii As Long
Dim jj As Long
Dim kk As Long
Dim EMAIL_OBJ As EmailInfo
On Error GoTo ERROR_LABEL
OUTLOOK_SEND_EXPRESS_MAIL_FUNC = False
If OUTLOOK_VALIDATE_EMAIL_ADDRESS_FUNC(FROM_STR_NAME) = False Then: GoTo ERROR_LABEL
If OUTLOOK_VALIDATE_EMAIL_ADDRESS_FUNC(TO_STR_NAME) = False Then: GoTo ERROR_LABEL
'the temp email file
If SRC_PATH_NAME = "" Then
SRC_PATH_NAME = Excel.Application.Path & _
Excel.Application.PathSeparator & "temp.eml"
End If
'complete the fields to be used
With EMAIL_OBJ
.sAddrFrom = FROM_STR_NAME
.sAddrTo = TO_STR_NAME
.sAddrCC = CC_STR_NAME
.sAddrBCC = BCC_STR_NAME
.sSubject = SUBJECT_STR_NAME
.sMessage = TEXT_MSG
.sPriority = PRIOR_INDEX
End With
'create the temp file
ii = FreeFile
Open SRC_PATH_NAME For Output As #ii
'if successful,
If ii <> 0 Then
'write out the data and
'send the email
If OUTLOOK_WRITE_BODY_FUNC(ii, EMAIL_OBJ) Then
'the desktop will be the
'default for error messages
'execute the passed operation
'the desktop will be the default for error messages
kk = GetDesktopWindow()
'execute the passed operation
jj = ShellExecute(kk, "Open", _
SRC_PATH_NAME, vbNullString, _
vbNullString, vbNormalFocus)
'This is optional. Uncomment the three lines
'below to have the "Open With.." dialog appear
'when the ShellExecute API call fails
' If success < 32 Then
' Call Shell("rundll32.exe shell32.dll,OpenAs_RunDLL " & _
SRC_PATH_NAME, vbNormalFocus)
' End If
End If
End If
OUTLOOK_SEND_EXPRESS_MAIL_FUNC = True
Exit Function
ERROR_LABEL:
OUTLOOK_SEND_EXPRESS_MAIL_FUNC = False
End Function
'This routine shows how to send Outlook mail from Excel.
'This could be useful for error reporting, for feedback,
'for any form of interaction with users. Try it out by
'specifying who to send the message to, and the message
Public Function OUTLOOK_SEND_MAIL_FUNC(ByVal RECIPIENT_ADDRESS_STR As String, _
ByVal SUBJECT_STR As String, _
ByVal BODY_STR As String, _
Optional ByVal SHOW_FLAG As Boolean = False)
Dim MAIL_OBJ As Outlook.MailItem
Dim OUTLOOK_OBJ As New Outlook.Application
On Error GoTo ERROR_LABEL
OUTLOOK_SEND_MAIL_FUNC = False
'in case you need current user name..
'Dim SPACE_OBJ As Outlook.NameSpace
'Set SPACE_OBJ = OUTLOOK_OBJ.Session
'Debug.Print SPACE_OBJ.CurrentUser.Name
Set MAIL_OBJ = OUTLOOK_OBJ.CreateItem(olMailItem)
With MAIL_OBJ.Recipients.Add(RECIPIENT_ADDRESS_STR)
.Type = olTo
.Resolve 'check address is valid
If Not .Resolved Then: GoTo ERROR_LABEL 'This email address does not
'appear to be valid
End With
MAIL_OBJ.Subject = SUBJECT_STR
MAIL_OBJ.body = BODY_STR
If SHOW_FLAG = True Then
MAIL_OBJ.display
Else
MAIL_OBJ.send
End If
Set MAIL_OBJ = Nothing
Set OUTLOOK_OBJ = Nothing
OUTLOOK_SEND_MAIL_FUNC = True
Exit Function
ERROR_LABEL:
On Error Resume Next
Set MAIL_OBJ = Nothing
Set OUTLOOK_OBJ = Nothing
OUTLOOK_SEND_MAIL_FUNC = False
End Function
'************************************************************************************
'************************************************************************************
'FUNCTION : OUTLOOK_WRITE_BODY_FUNC
'DESCRIPTION : Script for writing emails
'LIBRARY : OUTLOOK
'GROUP : BODY
'ID : 001
'AUTHOR : RAFAEL NICOLAS FERMIN COTA
'************************************************************************************
'************************************************************************************
Function OUTLOOK_WRITE_BODY_FUNC(ByVal ii As Long, _
ByRef EMAIL_OBJ As EmailInfo) As Boolean
'write the EMAIL_OBJ fields to the file
On Error GoTo ERROR_LABEL
Print #ii, "From: <"; EMAIL_OBJ.sAddrFrom; ">"
If Len(EMAIL_OBJ.sAddrTo) Then
Print #ii, "To: "; Chr$(34); EMAIL_OBJ.sAddrTo; Chr$(34)
Else
'no to address, so bail
OUTLOOK_WRITE_BODY_FUNC = False
Exit Function
End If
If Len(EMAIL_OBJ.sAddrCC) Then
Print #ii, "CC: "; Chr$(34); EMAIL_OBJ.sAddrCC; Chr$(34)
End If
If Len(EMAIL_OBJ.sAddrBCC) Then
Print #ii, "BCC: "; Chr$(34); EMAIL_OBJ.sAddrBCC; Chr$(34)
End If
Print #ii, "Subject: "; EMAIL_OBJ.sSubject
Print #ii, "X-Priority:"; EMAIL_OBJ.sPriority '1=high,3=normal,5=low
'this is the last header line - everything
'after this appears in the message.
Print #ii, "X-Unsent: 1"
Print #ii, "" 'Blank line
Print #ii, EMAIL_OBJ.sMessage
Close #ii
OUTLOOK_WRITE_BODY_FUNC = True
Exit Function
ERROR_LABEL:
OUTLOOK_WRITE_BODY_FUNC = False
End Function