-
Notifications
You must be signed in to change notification settings - Fork 3
/
QuoteFixNames.bas
346 lines (303 loc) · 12.6 KB
/
QuoteFixNames.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
Attribute VB_Name = "QuoteFixNames"
'@Folder("QuoteFixMacro")
Option Explicit
Option Private Module
'Last names may contain formal suffixes. If they are included here, they can be stripped.
Private Const LASTNAME_SUFFIXES As String = _
"II/III/IV/Jr/Jr./Sr/Sr./Esq."
'Extracts the name of the sender from the sender's name provided in the email.
'TODO: Future work is to extract the first name out of the stored Outlook contacts (if that contact exists)
'
'Notes:
' * Names are returned by reference
Public Sub getNamesFromMail(ByVal item As MailItem, ByRef senderName As String, ByRef firstName As String, ByRef lastName As String)
'Wildcard replacements
senderName = item.SentOnBehalfOfName
If Len(senderName) = 0 Then
senderName = item.senderName
End If
getNamesOutOfString senderName, senderName, firstName, lastName, item.senderEmailAddress
End Sub
'Code duplication of getNamesFromMail, because there is no common ancestor of MailItem and MeetingItem
Public Sub getNamesFromMeeting(ByVal item As MeetingItem, ByRef senderName As String, ByRef firstName As String, ByRef lastName As String)
'Wildcard replacements
' `MeetingItem.SentOnBehalfOfName` not supported in Outlook 2019.
' Thus, we fall back to `senderName`.
senderName = item.senderName
If Len(senderName) = 0 Then
senderName = item.senderName
End If
getNamesOutOfString senderName, senderName, firstName, lastName, item.senderEmailAddress
End Sub
'Attempts to extract the name of the sender from the sender's name provided in the email.
'
'Comment:
' This is very difficult to do definitively. Consider how many different variations
' and arrangements a name like "Dr. John James Walker Smith III" could entail including
' questions of first vs. last names, initials, titles, suffixes, etc..
'
'In:
' originalName - name as presented by Outlook
'Out:
' senderName - complete name of sender
' firstName - first name of sender
' lastName - last name of sender
' senderEmailAddress - sender email address (optional because of tests)
'Notes:
' * Public to enable testing
' * Names are returned by reference
Public Sub getNamesOutOfString(ByVal originalName As String, ByRef senderName As String, ByRef firstName As String, ByRef lastName As String, Optional ByRef senderEmailAddress As String = vbNullString)
'Find out firstName
Dim tmpName As String
tmpName = originalName
'cleanup quotes: if name is enclosed in quotes, just remove them
If (Left$(tmpName, 1) = """" And Right$(tmpName, 1) = """") Then
tmpName = Mid$(tmpName, 2, Len(tmpName) - 2)
End If
'default full senderName: originalName without quotes
senderName = tmpName
'default firstName: fullname
firstName = tmpName
Dim title As String
title = vbNullString
'Has to be later used for extracting the last name
tmpName = removeDepartment(tmpName)
If (Left$(tmpName, 4) = "Dr. ") Then
tmpName = Mid$(tmpName, 5)
title = "Dr. "
ElseIf (Right$(tmpName, 5) = ", Dr.") Then
tmpName = Left$(tmpName, Len(tmpName) - 5)
title = "Dr. "
ElseIf (Right$(tmpName, 3) = "Dr.") Then
tmpName = Left$(tmpName, Len(tmpName) - 3)
title = "Dr. "
End If
'Some companies have "(Text)" at the end of their name.
'We strip that
If (Right$(tmpName, 1) = ")") Then
Dim fPos As Long
fPos = InStrRev(tmpName, "(")
If fPos > 0 Then
tmpName = Trim$(Left$(tmpName, fPos - 1))
End If
End If
fPos = InStr(tmpName, ",")
If fPos > 0 Then
'Firstname is separated by comma and positioned behind the lastname
firstName = Trim$(Mid$(tmpName, fPos + 1))
'Firstname field may include middle initial(s)
Do While (UCase$(Right$(firstName, 2)) Like " [A-Z]" Or UCase$(Right$(firstName, 2)) Like "[A-Z].")
firstName = Trim$(Left$(firstName, Len(firstName) - 2))
Loop
lastName = Trim$(Left$(tmpName, fPos - 1))
'lastName field may have a formal suffix
lastName = StripSuffixes(lastName)
Else
'Determining first and last name is really hard unless
'there are only two names, or there is a middle initial(s)
fPos = InStr(Trim$(tmpName), " ")
If fPos > 0 Then
'First strip any possible, (single,) formal suffix on the name
tmpName = StripSuffixes(tmpName)
Dim lPos As Long
lPos = InStrRev(Trim$(tmpName), " ")
If fPos = lPos Then
'single first name and last name separated by space
firstName = Trim$(Left$(tmpName, fPos - 1))
lastName = Trim$(Mid$(tmpName, lPos + 1))
If firstName = UCase$(firstName) And Not lastName = UCase$(lastName) Then
'in case the firstName is written in uppercase letters (and not everything in capital letters),
'we assume that the sender's last name is the firstName (in the string)
lastName = firstName
firstName = Trim$(Mid$(tmpName, lPos + 1))
End If
Else
'middle section could be a single/multiple name/initial (or both)
Dim midName As String
midName = Trim$(Mid$(Left$(tmpName, lPos), fPos))
'One or two initials are easy
Do While Len(midName) = 1 Or _
Left$(midName, 1) = "." Or _
Left$(midName, 2) Like "[A-Z] " Or _
Left$(midName, 2) Like "[A-Z]."
midName = Trim$(Mid$(midName, 2))
Dim i As Long
i = i + 1
Loop
Do While Right$(midName, 2) Like " [A-Z]" Or _
Right$(midName, 2) Like "[A-Z]."
midName = Trim$(Left$(midName, Len(midName) - 2))
Dim j As Long
j = j + 1
Loop
If Len(midName) = 0 Then
'initials only
firstName = Trim$(Left$(tmpName, fPos - 1))
lastName = Trim$(Mid$(tmpName, lPos + 1))
ElseIf i <> 0 And j = 0 Then
'initials before double last name
lastName = midName & Trim$(Mid$(tmpName, lPos + 1))
firstName = Trim$(Left$(tmpName, fPos - 1))
ElseIf i = 0 And j <> 0 Then
'initials after double first name
lastName = Trim$(Mid$(tmpName, lPos + 1))
firstName = Trim$(Left$(tmpName, fPos - 1)) & midName
ElseIf Left$(midName, 1) = LCase$(Left$(midName, 1)) Then
'Midname starts with a lower case letter
'We assume "correct" casing. Thus, we hit a name such as Firstname von Lastname
firstName = Trim$(Left$(tmpName, fPos - 1))
lastName = Trim$(Mid$(tmpName, fPos + 1))
Else
'anything else can't be definitively identified as a first, middle or last name
firstName = tmpName
lastName = vbNullString
End If
End If
Else
fPos = InStr(tmpName, "@")
If fPos > 0 Then
'first name is (currently) an email address. Just take the prefix
tmpName = Left$(tmpName, fPos - 1)
End If
fPos = InStr(tmpName, ".")
If fPos > 0 Then
'first name is separated by a dot
lastName = Mid$(tmpName, fPos + 1)
tmpName = Left$(tmpName, fPos - 1)
Else
'name is a single string, without "." or " "
'final guess: LastnameFirstname
If (IsUpperCaseChar(Left$(tmpName, 1))) Then
i = 2
Dim UpperCaseCharCount As Long
UpperCaseCharCount = 0
Dim LastUpperCaseCharPos As Long
LastUpperCaseCharPos = 0
Do While (i < Len(tmpName) And (UpperCaseCharCount < 2))
If (IsUpperCaseChar(Mid$(tmpName, i, 1))) Then
LastUpperCaseCharPos = i
UpperCaseCharCount = UpperCaseCharCount + 1
End If
i = i + 1
Loop
If (UpperCaseCharCount = 1) Then
'LastnameFirstname format found
tmpName = Mid$(tmpName, LastUpperCaseCharPos)
End If
End If
End If
firstName = tmpName
End If
End If
Dim fnEmail As String
Dim lnEmail As String
getFirstNameLastNameOutOfEmail senderEmailAddress, fnEmail, lnEmail
If (LCase$(firstName) = LCase$(lnEmail)) And (LCase$(lastName) = LCase$(fnEmail)) Then
' in case firstname and lastname are reversed in the email address, we assume that email format is firstname.lastname and reverse the names here
Dim tmp As String
tmp = firstName
firstName = lastName
lastName = tmp
End If
'fix casing of names
If InStr(firstName, " ") = 0 Then
firstName = FixCase(firstName)
End If
If InStr(lastName, " ") = 0 Then
lastName = FixCase(lastName)
End If
senderName = title & Trim$(firstName & " " & lastName)
lastName = title & lastName
End Sub
Public Function removeDepartment(ByVal tmpName As String) As String
Dim parts() As String
parts = Split(tmpName, " ")
Dim length As Long
length = UBound(parts) - LBound(parts) + 1
If length <= 2 Then
removeDepartment = tmpName
Exit Function
End If
Dim indexWordBeforeLastUppercasedWord As Long
indexWordBeforeLastUppercasedWord = UBound(parts)
Do While (indexWordBeforeLastUppercasedWord >= LBound(parts) + 2)
If Not IsUpperCaseWord(parts(indexWordBeforeLastUppercasedWord)) Then
Exit Do
End If
indexWordBeforeLastUppercasedWord = indexWordBeforeLastUppercasedWord - 1
Loop
If indexWordBeforeLastUppercasedWord < LBound(parts) + 1 Then
removeDepartment = tmpName
Exit Function
End If
Dim i As Long
For i = LBound(parts) To indexWordBeforeLastUppercasedWord
Dim result As String
result = result & parts(i) & " "
Next
removeDepartment = Left$(result, Len(result) - 1)
End Function
Public Function IsUpperCaseWord(ByVal word As String) As Boolean
IsUpperCaseWord = word Like "[A-Z][A-Z]*"
End Function
Private Function StripSuffixes(ByVal tempName As String) As String
'Create array of possible suffixes
Dim NameSuffixesArr() As String
NameSuffixesArr = Split(LASTNAME_SUFFIXES, "/")
'Strip the last suffix (is it ever the case that someone has multiple suffixes?)
Dim i As Long
For i = LBound(NameSuffixesArr) To UBound(NameSuffixesArr)
If (Right$(tempName, Len(NameSuffixesArr(i)) + 1)) = " " & NameSuffixesArr(i) Then
StripSuffixes = Trim$(Left$(tempName, Len(tempName) - Len(NameSuffixesArr(i))))
End If
Next
StripSuffixes = tempName
End Function
Private Function IsUpperCaseChar(ByVal c As String) As Boolean
IsUpperCaseChar = c Like "[A-Z]"
End Function
Public Sub getFirstNameLastNameOutOfEmail(ByVal email As String, ByRef firstName As String, ByRef lastName As String)
If Len(email) = 0 Then
firstName = vbNullString
lastName = vbNullString
Exit Sub
End If
Dim parts() As String
parts = Split(email, "@")
Dim addressee As String
addressee = parts(0)
parts = Split(addressee, ".")
Dim length As Long
length = UBound(parts) - LBound(parts) + 1
If (length <> 2) Then
firstName = addressee
lastName = vbNullString
Exit Sub
End If
firstName = parts(LBound(parts))
firstName = stripNumbers(firstName)
lastName = parts(UBound(parts))
lastName = stripNumbers(lastName)
End Sub
Private Function FixCase(ByVal word As String) As String
If Len(word) = 0 Then
FixCase = word
Exit Function
End If
Dim parts() As String
parts = Split(word, "-")
Dim i As Long
For i = LBound(parts) To UBound(parts)
Dim result As String
result = result & UCase$(Left$(parts(i), 1)) & LCase$(Mid$(parts(i), 2)) & "-"
Next
FixCase = Left$(result, Len(result) - 1)
End Function
Private Function stripNumbers(ByVal s As String) As String
Dim result As String
result = s
Do While (Right$(result, 1) Like "#")
result = Left$(result, Len(result) - 1)
Loop
stripNumbers = result
End Function