Попробуйте что-то вроде этого
Private Sub Application_NewMail()
Dim olFld As Outlook.MAPIFolder
Set olFld = Outlook.Session.GetDefaultFolder(olFolderInbox)
olFld.Items.Sort "[ReceivedTime]", False
Dim olMail As Outlook.MailItem
Set olMail = olFld.Items.GetFirst
MyNiftyFilter olMail
End Sub
Private Sub MyNiftyFilter(Item As Outlook.MailItem)
Debug.Print Item
Debug.Print Item.Subject
Dim Matches As Variant
Dim RegExp As New VBScript_RegExp_55.RegExp
Dim Pattern As String
Dim Email_Subject As String
Pattern = "(([\w-\s]*)\s*)"
Email_Subject = Item.Subject
With RegExp
.Global = False
.Pattern = Pattern
.IgnoreCase = True
Set Matches = .Execute(Email_Subject)
End With
If Matches.Count > 0 Then
Debug.Print Matches(0) ' Print on Immediate Window
End If
Set RegExp = Nothing
Set Matches = Nothing
Set Item = Nothing
End Sub
для использования в регулярных выражениях \[(.*?)\]
* demo https://regex101.com/r/U3bjOf/1
https://regex101.com/r/U3bjOf/2
If Matches.Count > 0 Then
Debug.Print Matches(0) ' full match [ABC]
Debug.Print Matches(0).submatches(0) ' submatch ABC
End If
для создания функции использования подпапки, подобной этой
'// Function - Check folder Exist
Private Function FolderExists(Inbox As MAPIFolder, FolderName As String)
Dim Sub_Folder As MAPIFolder
On Error GoTo Exit_Err
Set Sub_Folder = Inbox.Folders(FolderName)
FolderExists = True
Exit Function
Exit_Err:
FolderExists = False
End Function
затем назовите это
Dim olFld As Outlook.MAPIFolder
Set olFld = Outlook.Session.GetDefaultFolder(olFolderInbox)
Dim SubFolder As Outlook.MAPIFolder
Dim FolderName As String
If Matches.Count > 0 Then
Debug.Print Matches(0) ' full match [ABC]
Debug.Print Matches(0).submatches(0) ' submatch ABC
FolderName = Matches(0).submatches(0)
'// Check if folder exist else create one
If FolderExists(Inbox, FolderName) = True Then
Set SubFolder = Inbox.Folders(FolderName)
Else
Set SubFolder = Inbox.Folders.Add(FolderName)
End If
Item.Move SubFolder
End If