'how to output the result of a regular expression in outlook vba

I was trying to use regex to find an invoice number in outlook email and I'm getting the following object defined error while trying to output the result of a regular expression that I'm using in my outlook VBA. I Have used the string "invoice Number: 123" as my test string, this will be replaced later on with the original email body. However, I'm getting this error in the msgbox line.

Error message:

enter image description here

scope of error in my code:

enter image description here

My Code:

Sub ReplyEmailTemplate()

Dim origEmail As MailItem
Dim replyEmail As MailItem
Set origEmail = Application.ActiveWindow.Selection.Item(1)
Set replyEmail = Application.CreateItem(olMailItem)

answer = MsgBox("please note that once submitted, all changes will be saved and the file will be sent to the robot to process it." & _
vbNewLine & vbNewLine & "do you still want to submit?" & _
vbNewLine & vbNewLine & "( YES ) = submit " & _
vbNewLine & vbNewLine & "( NO ) = cancel ", vbQuestion + vbYesNo + vbCritical + vbDefaultButton2, "WARNING!")

If answer = vbYes Then

Dim RegEx As Object, MyString As String, allMatches As Object
Set RegEx = CreateObject("VBScript.RegExp")
MyString = "invoice Number: 123"

With RegEx
  .Pattern = "(?<=invoice Number:\s).[0-9]+"
  .Global = True
End With

MsgBox RegEx.Test(MyString)
 
replyEmail.To = origEmail.Sender
replyEmail.CC = origEmail.CC
replyEmail.Subject = origEmail.Subject
replyEmail.HTMLBody = origEmail.Body 'replyEmail.HTMLBody '& origEmail.Reply.HTMLBody
replyEmail.Display

End If

End Sub


Solution 1:[1]

after more research I found this solution which seems to work fine with me. however, i still want the regex to get the numbers after a specific string not just the first number that it finds in the email body. the solution:

''''''regex

Dim stringOne As String
Dim regexOne As Object
Dim theMatches As Object
Dim Match As Object
Dim invnum As String
Set regexOne = New RegExp

regexOne.Pattern = "-?\d*\.?\d+"
regexOne.Global = True
regexOne.IgnoreCase = True
stringOne = origEmail.Body

Set theMatches = regexOne.Execute(stringOne)
invnum = theMatches(0)
'MsgBox invnum

''''''regex

solution found on: automateexcel

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kamal_hamad