Regex 5 digits numbers from threads in MS Outlook

I have the following macro. It should print a 5-digit number from the subject line from an open Outlook email. An example topic line would be "Thank you. Order: # 98512" (without quotes).

I have a library checked in Outlook links for Regex 5.5.

I do not get any output. Only the following error.

The bug highlighter throws and error: 
Run-Time error '450':
Wrong Number of arguments or invalid property assignment
With highlights on the MsgBox orderNumber command. 

Here is the current macro:

Sub ShowTitle()
        Dim orderNumber
        Dim orderRegExp
        Dim regExVar As String
        Dim CurrentMessage As MailItem

    'Takes the email title from the subject line
        Set CurrentMessage = ActiveInspector.CurrentItem
        regExVar = CurrentMessage.Subject

    'Regex out the order number from the regExVar

        Set orderRegExp = New RegExp
        orderRegExp.Pattern = " \d{5} "
        orderRegExp.Pattern = True
        Set orderNumber = orderRegExp.Execute(regExVar)


    'displays in a message box
        MsgBox orderNumber

End Sub
+4
source share
1 answer

Hope this does what you need.

Sub ShowTitle()
    Dim orderNumber
    Dim orderRegExp As RegExp
    Dim regExVar As String

    ' Takes the email title from the subject line
    regExVar = "Thank you. Order:#98512"

    Set orderRegExp = New RegExp
    ' \s    Match any space character
    ' \S    Match any non-space character
    ' \d    Match any digit
    ' [xyz] Match any one character enclosed in the character set
    ' {}    Specifies how many times a token can be repeated
    ' $     Match must occur at the end of the string

    orderRegExp.Pattern = "[\s\S]\d{4}$"

    If orderRegExp.test(regExVar) Then
        Set orderNumber = orderRegExp.Execute(regExVar)
        'displays in a message box
        MsgBox orderNumber(0).value
    End If
End Sub

enter image description here

+2
source

Source: https://habr.com/ru/post/1569483/


All Articles