Question : Need to strip string / email from message body, with Outlook XP VBA?

I'm looking for some sample code to allow me to strip an email address out of the body of an email?

The format would be :-

Fax                 =
E-Mail              = [email protected]
VAT ID              =

The code will run from vba in outlook xp. I already have some code which add data to a database bases on the header details.

I assume that from Outlook.Mailitem the property body would hold the content of the message?

Answer : Need to strip string / email from message body, with Outlook XP VBA?

It's difficult to figure out what's going on when the results change like this.  First the code isn't returning anything now it's producing an error.  Assuming that you are testing with the same message, then it's difficult to understand how it could produce such widely varied results.

That aside, the error is a result of a line without an "=" in it.  The code assumes that every line in the message consists of label and data separated by an equal sign.  I've modified the code to ignore blank lines or indeed any line that does not contain an equal sign.  I've also incorporated Chris' suggestion (good catch, Chris!) of exiting the loop once the desired label is found.  Not knowing which version you're using (i.e. the one I originally supplied where the entire message is passed as a parameter or your modified version that passes just the body as a parameter) I've modified your version.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
Function GetValue(pSource As String, strValueName As String) As String 
    Dim arrLines As Variant, _ 
        varLine As Variant, _ 
        arrFields As Variant 
         
    arrLines = Split(pSource, vbCrLf) 
    For Each varLine In arrLines 
        If InStr(1, varLine, "=") Then
            arrFields = Split(varLine, "=")
            If InStr(1, arrFields(0), strValueName) Then 
                GetValue = Trim(arrFields(1)) 
                Exit For
            End If 
        End If
    Next 
End Function
Random Solutions  
 
programming4us programming4us