I need VBA code that checks the email subject for a specific substring like this "Nr. 123456789"
.
I have this RegEx for matching:
(Nr.\s1\d{8}):
https://regexr.com/4i2v1
my VBA code to match one email:
Private WithEvents olInboxItems As Items
Private Sub Application_Startup()
Set olInboxItems = Session.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
Dim ns As Outlook.NameSpace
Dim MailDest As Outlook.Folder
Set ns = Application.GetNamespace("MAPI")
Set Reg1 = CreateObject("VBScript.RegExp")
Reg1.Pattern = "(Nr.\s1\d{8})"
If Reg1.test(Item.Subject) Then
Set MailDest = ns.GetDefaultFolder(olFolderInbox).Folders("Admin")
Item.Move MailDest
End If
End Sub
Thats work for simple Mail-Subject-Check. But I must check a second mail with the same number (Nr. 123456789) but not the same subject string. The second mail arrives 5-10 mins after first mail.If i have two mails with the same number, then move both mails to another folder.
my thought for the code: After matching one Mail with RegEx, check all other mails to find the "first"-Mail. If not machting second mail, do nothing. But i dont know, how to scan all mails after matching.
Example for the subjects:
Mail 1 subject = "Lorem ipsum Nr. 100448899 dolor sit amet" Mail 2 subject = "At vero eos et accusam Nr. 100448899 no sea"