I inherited some VBA for dumping emails from a folder in Outlook into a SQL database. I have the first Sub() working, but the function below when I run the script gives me a "VBA Runtime Error 5". I am not able to figure out why it is throwing the error and it looks like a generic error. I would like assistance in debugging this. The error is with the line (6 rows from the bottom): GetText = Replace(Trim(Mid(body, s + l, ml)), "'", "''")
Below is the whole function. Is there anything glaring that I am missing?
Function GetText(ByVal body As String, ByVal start_ As String, ByVal end_ As String, ByVal maxlength As Double) As String
'return the text in the range, less the start_ text itself. Also checks for "--- End Of Report ---" in addition to the end_ value.
'returns '' if not found
'limit size to maxlength, unless it is -1 which means no limit
Dim l, s, e, ml As Double
GetText = ""
'check that we have the starting value
s = InStr(1, body, start_)
If s > 0 Then
l = Len(start_)
'get the location of the end_. If 0, get End Of Report location
e = InStr(1, body, end_)
If e = 0 Then
Select Case start_
Case "ADDRESS:", "NETWORK:", "EMAIL:"
end_ = "SECURITY TYPE:"
Case "USER:"
end_ = "EMAIL:"
Case "DISK:"
end_ = "CULTURE:"
Case "CULTURE:"
end_ = "USER:"
Case "OS:"
end_ = "CLR:"
Case "HARDWARE:"
end_ = "ENVIRONMENT:"
Case "XMR:"
end_ = "CPU:"
Case "ARGS:"
end_ = "RIGHTS:"
Case "MEMORY:", "ENVIRONMENT:"
end_ = "DISK:"
Case "BUILD:"
end_ = "HARDWARE:"
Case "!!!EXCEPTION ENCOUNTERED!!!"
end_ = "--- End Of Report ---"
End Select
e = InStr(1, body, end_)
If e = 0 Then
Select Case start_
Case "USER:"
end_ = "SECURITY TYPE:"
Case "HARDWARE:"
end_ = "MEMORY:"
Case "CULTURE:", "EMAIL:", "NETWORK:"
end_ = "SECURITY:"
End Select
e = InStr(1, body, end_)
If e = 0 Then
Select Case start_
Case "HARDWARE:"
end_ = "DISK:"
Case "USER:"
end_ = "SECURITY:"
End Select
e = InStr(1, body, end_)
End If
End If
End If
If e = 0 Then
e = InStr(1, body, "!!!EXCEPTION ENCOUNTERED!!!")
If e = 0 Then e = InStr(1, body, "--- End Of Report ---")
End If
ml = e - s - l 'the length of the returning text
If maxlength > -1 And ml > maxlength Then
' MsgBox "Hit"
ml = maxlength
End If
GetText = Replace(Trim(Mid(body, s + l, ml)), "'", "''")
If ml = 1000000 Then
GetText = GetText & "[truncated]"
End If
End If
End Function