Well Dave, this is a multifunctional function specially designed for your needs but I hope useful to others:
Code:
Function StringPart(ByVal strText As String, _
Optional strFrom As String, _
Optional strTo As String, _
Optional ExFrom As Boolean = False, _
Optional ExTo As Boolean = False) As String
'Returns the part of strText between strFrom and strTo strings.
'If strFrom is omited, works as Left function.
'If strTo is omited, works as Right function.
'If both strFrom and strTo are passed, works as Mid function.
'If ExFrom is True, exclude the strFrom of the returned value.
'If ExTo is True, exclude the strTo of the returned value.
Dim strMid As String
Dim lngPos As Long
Dim lngLen As Long
strMid = strText
'Right function.
lngLen = Len(strFrom)
If lngLen > 0 Then
lngPos = InStr(1, strMid, strFrom)
If lngPos > 0 Then
If ExFrom Then lngPos = lngPos + lngLen + 1
strMid = Trim$(Mid$(strMid, lngPos))
End If
End If
'Left function.
lngLen = Len(strTo)
If lngLen > 0 Then
lngPos = InStr(1, strMid, strTo)
If lngPos > 0 Then
If Not ExTo Then lngPos = lngPos + Len(strTo) + 1
strMid = Trim$(Left$(strMid, lngPos - 1))
End If
End If
StringPart = strMid
End Function
Run the procedure below and check the results.
Code:
Sub TestStringPart()
Dim strOld As String
Dim strStart As String
Dim strStop As String
strOld = "This was sent to you, please do not print" & vbCrLf _
& "This is also a message for you" & vbCrLf _
& "Form Response Notification" & vbCrLf _
& "My Name is Joe Bloggs" & vbCrLf _
& "My Number Is 123456" & vbCrLf _
& "Reply to Joe Bloggs" & vbCrLf _
& "PLease do not print" & vbCrLf _
& "Goodbye" & vbCrLf _
& "Have a nice day"
strStart = "Form Response Notification"
strStop = "Reply to"
MsgBox "strFrom = '" & strStart & "'" & vbCrLf & vbCrLf & StringPart(strOld, strStart), , "Right function"
MsgBox "strFrom = '" & strStart & "'" & vbCrLf & "ExFrom=True" & vbCrLf & vbCrLf & StringPart(strOld, strStart, , True), , "Right function"
MsgBox "strTo = '" & strStop & "'" & vbCrLf & vbCrLf & StringPart(strOld, , strStop), , "Left function"
MsgBox "strTo = '" & strStop & "'" & vbCrLf & "ExTo=True" & vbCrLf & vbCrLf & StringPart(strOld, , strStop, , True), , "Left function"
MsgBox "strFrom = '" & strStart & "'" & vbCrLf & "strTo = '" & strStop & "'" & vbCrLf & vbCrLf & StringPart(strOld, strStart, strStop), , "Mid function"
MsgBox "strFrom = '" & strStart & "'" & vbCrLf & "strTo = '" & strStop & "'" & vbCrLf & "ExFrom=True" & vbCrLf & "ExTo=True" & vbCrLf & vbCrLf & StringPart(strOld, strStart, strStop, True, True), , "Mid function"
End Sub
Cheers,
John