Link to home
Start Free TrialLog in
Avatar of thelarster
thelarster

asked on

VB Outlook Subject Line - Grab Number

Is is possible to grab a number from the subject of an Outlook message using VB Script? For example if I get a lot of messages that have the subject 10/02/123456 but the 123456 changes every message (it's an order number) but is always 8 characters in length and always follows that pattern.

If so, any one have an example or link. I want to take that number and the parse to another program but wasn't sure how to grab it first.

Thanks
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

Your example is not an 8 digit number :)

Does your subject have other text as well?
Avatar of thelarster
thelarster

ASKER

Here's a 'real life' subject line example:  010/02/06869427 Elemica - Warning

The only thing that changes is the 8 digits after 02/

mathherwspatrick-that was a quck reply!
thelarster,

This function was written for VBA/VB6, but yu can adapt it for VBScript:



Function RegExpFind(LookIn As String, PatternStr As String, Optional Pos, _
    Optional MatchCase As Boolean = True)

    ' This function uses Regular Expressions to parse a string (LookIn), and return matches to a
    ' pattern (PatternStr).  Use Pos to indicate which match you want:
    ' Pos omitted               : function returns a zero-based array of all matches
    ' Pos = 0                   : the last match
    ' Pos = 1                   : the first match
    ' Pos = 2                   : the second match
    ' Pos = <positive integer>  : the Nth match
    ' If Pos is greater than the number of matches, is negative, or is non-numeric, the function
    ' returns an empty string.  If no match is found, the function returns an empty string
   
    ' If MatchCase is omitted or True (default for RegExp) then the Pattern must match case (and
    ' thus you may have to use [a-zA-Z] instead of just [a-z] or [A-Z]).
   
    ' If you use this function in Excel, you can use range references for any of the arguments.
    ' If you use this in Excel and return the full array, make sure to set up the formula as an
    ' array formula.  If you need the array formula to go down a column, use TRANSPOSE()
   
    Dim RegX As Object
    Dim TheMatches As Object
    Dim Answer() As String
    Dim Counter As Long
   
    ' Evaluate Pos.  If it is there, it must be numeric and converted to Long
    If Not IsMissing(Pos) Then
        If Not IsNumeric(Pos) Then
            RegExpFind = ""
            Exit Function
        Else
            Pos = CLng(Pos)
        End If
    End If
   
    ' Create instance of RegExp object
    Set RegX = CreateObject("VBScript.RegExp")
    With RegX
        .Pattern = PatternStr
        .Global = True
        .IgnoreCase = Not MatchCase
    End With
       
    ' Test to see if there are any matches
    If RegX.test(LookIn) Then
       
        ' Run RegExp to get the matches, which are returned as a zero-based collection
        Set TheMatches = RegX.Execute(LookIn)
       
        ' If Pos is missing, user wants array of all matches.  Build it and assign it as the
        ' function's return value
        If IsMissing(Pos) Then
            ReDim Answer(0 To TheMatches.Count - 1) As String
            For Counter = 0 To UBound(Answer)
                Answer(Counter) = TheMatches(Counter)
            Next
            RegExpFind = Answer
       
        ' User wanted the Nth match (or last match, if Pos = 0).  Get the Nth value, if possible
        Else
            Select Case Pos
                Case 0                          ' Last match
                    RegExpFind = TheMatches(TheMatches.Count - 1)
                Case 1 To TheMatches.Count      ' Nth match
                    RegExpFind = TheMatches(Pos - 1)
                Case Else                       ' Invalid item number
                    RegExpFind = ""
            End Select
        End If
   
    ' If there are no matches, return empty string
    Else
        RegExpFind = ""
    End If
   
    ' Release object variables
    Set RegX = Nothing
    Set TheMatches = Nothing
   
End Function


Use it like this:

MsgBox "The number is... " & RegExpFind(olMsg.Subject, "\d{8}", 1)

Regards,

Patrick
Do you already have the code to extract the subject line? If so, we can provide the RegExp or other solution.
No, I don't have the code to grab the subject. I am not sure how to implement the above code either. This seems a bit different than VB for Excel.

Thanks,
Where do the emails reside? Are you using Outlook Express? Should we be looking to poll the DBX files? Or do you use Exchange? Or Hotmail? Etc...
I'm using Outlook 2003 SP2 on my companies Exchange Server. The emails are in a sub folder of my Inbox.
Let's start with trying the following script:

http://www.tek-tips.com/viewthread.cfm?qid=1147829&page=1

That should give you some important information, and some good starting points for creating your code. Let me know if that works for you.
thelarster,

Please give a detailed description of what you are trying to do.

Regards,

Patrick
Patrick,

When I am in my Inbox and reading a message I want a script that grabs the order number (after I hit some key combo to run the script.) into a variable. Once I have that I can write some additional scripting to enter that order number into SAP and bring up the order. I get emails confirming there is a new order and this would save about 5 steps to bring that order up in SAP.

Here's a 'real life' subject line example:  010/02/06869427 Elemica - Warning

The only thing that changes is the 8 digits after 02/ which is the order number.
ASKER CERTIFIED SOLUTION
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ah, I misunderstood.  Go with Patrick's code.
Patrick - that worked! Thank you VERY much.

L00M - thanks for your contribution also.
thelarster,

Glad to help :)

Regards,

Patrick
I'm trying to add to this script... is there a way to also grab the 1st 2 digit number in the body? The first line always reads Order :  010/02/06889132 for exaple. I need to grab the 02. I tried but it always grabs the 1st 2 digits (in this example 01)

Thanks!
thelarster,

With respect, you need to open a new question for this, and not try to wring more out of a
three month old question.

Regards,

Patrick