Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Option Explicit
- '@ModuleDescription "Handles processing of incoming emails in Outlook, separating language parts in subject lines."
- Public WithEvents InboxItems As Outlook.Items
- '@Description "Sets up the event handler for incoming emails in the default Inbox folder."
- Private Sub Application_Startup()
- Dim MailNamespace As Outlook.NameSpace
- Set MailNamespace = Application.GetNamespace("MAPI")
- Set InboxItems = MailNamespace.GetDefaultFolder(olFolderInbox).Items
- End Sub
- '@Description "Processes new emails as they arrive if they are MailItems."
- Private Sub InboxItems_ItemAdd(ByVal Item As Object)
- If TypeName(Item) = "MailItem" Then
- On Error GoTo ErrorHandler
- ProcessIncomingEmail Item
- Exit Sub
- ErrorHandler:
- ' Log error to the Immediate Window for debugging purposes.
- Debug.Print "Error processing email: " & Err.Description
- End If
- End Sub
- '@Description "Processes the given MailItem, separating the subject into English and French components."
- '@Param mail The MailItem to process.
- Private Sub ProcessIncomingEmail(ByVal mail As Outlook.MailItem)
- On Error GoTo ErrorHandler
- Dim SubjectRegex As Object
- Set SubjectRegex = CreateObject("VBScript.RegExp")
- SubjectRegex.Pattern = "^[SDT]\d{5,9}"
- If SubjectRegex.Test(mail.Subject) Then
- Dim Sections() As String
- Sections = Split(mail.Subject, " // Description: ")
- If UBound(Sections) >= 1 Then
- Dim TicketNumber As String
- TicketNumber = GetTicketNumber(Sections(0))
- Dim FirstSectionWithoutTicket As String
- FirstSectionWithoutTicket = RemoveTicketNumber(Sections(0), TicketNumber)
- Dim EnglishSubject As String, FrenchSubject As String
- EnglishSubject = TicketNumber & " " & SplitBySpecificSeparator(FirstSectionWithoutTicket) & " // Description: " & SplitBySpecificSeparator(Sections(1))
- FrenchSubject = TicketNumber & " " & SplitBySpecificSeparator(FirstSectionWithoutTicket, True) & " // Description: " & SplitBySpecificSeparator(Sections(1), True)
- With mail
- Dim UserPropertyF As Outlook.UserProperty
- Set UserPropertyF = .UserProperties.Find("FrenchSubject", True)
- If UserPropertyF Is Nothing Then
- Set UserPropertyF = .UserProperties.Add("FrenchSubject", olText)
- End If
- UserPropertyF.Value = FrenchSubject
- Dim UserPropertyE As Outlook.UserProperty
- Set UserPropertyE = .UserProperties.Find("EnglishSubject", True)
- If UserPropertyE Is Nothing Then
- Set UserPropertyE = .UserProperties.Add("EnglishSubject", olText)
- End If
- UserPropertyE.Value = EnglishSubject
- .Save
- End With
- End If
- Else
- With mail
- Dim UserPropertyE2 As Outlook.UserProperty
- Set UserPropertyE2 = .UserProperties.Find("EnglishSubject", True)
- If UserPropertyE2 Is Nothing Then
- Set UserPropertyE2 = .UserProperties.Add("EnglishSubject", olText)
- End If
- UserPropertyE2.Value = mail.Subject
- .Save
- End With
- End If
- Exit Sub
- ErrorHandler:
- ' Handle error appropriately
- Debug.Print "Error in ProcessEmail: " & Err.Description
- End Sub
- '@Description "Removes the ticket number from the start of the given string, if present."
- '@Param text The string to remove the ticket number from.
- '@Param ticketNumber The ticket number to remove.
- '@Returns String without the ticket number at the beginning.
- Private Function RemoveTicketNumber(ByVal Text As String, ByVal TicketNumber As String) As String
- ' Check if the start of Text matches the TicketNumber
- If Left(Text, Len(TicketNumber)) = TicketNumber Then
- ' Remove the TicketNumber and return the trimmed remaining string
- RemoveTicketNumber = Trim(Mid(Text, Len(TicketNumber) + 1))
- Else
- RemoveTicketNumber = Text
- End If
- End Function
- '@Description "Extracts the ticket number (e.g., S###, T###, D###) from the given string."
- '@Param text The string to extract the ticket number from.
- '@Returns String representing the extracted ticket number, if found.
- Private Function GetTicketNumber(ByVal Text As String) As String
- On Error GoTo ErrorHandler
- Dim TicketRegex As Object
- Set TicketRegex = CreateObject("VBScript.RegExp")
- TicketRegex.Pattern = "^[SDT]\d{5,9}"
- If TicketRegex.Test(Text) Then
- GetTicketNumber = TicketRegex.Execute(Text)(0)
- Else
- GetTicketNumber = vbNullString
- End If
- Exit Function
- ErrorHandler:
- ' Handle error appropriately
- Debug.Print "Error in GetTicketNumber: " & Err.Description
- GetTicketNumber = vbNullString
- End Function
- '@Description "Splits the given string at a specific '/', determined by the total count of '/' (divided by 2), to separate English from French."
- '@Param text The string to split.
- '@Param isFrench Boolean flag indicating if the French part is to be returned.
- '@Returns String containing either the English or French part of the text.
- Private Function SplitBySpecificSeparator(ByVal Text As String, Optional ByVal IsFrench As Boolean = False) As String
- Dim SubParts As Variant
- SubParts = Split(Text, "/")
- ' Calculate the specific index for splitting, assuming even distribution of '/' for English and French
- Dim SeparatorIndex As Long
- If UBound(SubParts) = 1 Then
- SeparatorIndex = 1
- Else
- SeparatorIndex = (UBound(SubParts) + 1) \ 2
- End If
- Dim Result As String
- Result = vbNullString ' Initialize the result string
- Dim PartIndex As Long
- If IsFrench Then
- For PartIndex = SeparatorIndex To UBound(SubParts)
- Result = Result & SubParts(PartIndex) & IIf(PartIndex < UBound(SubParts), " / ", vbNullString)
- Next PartIndex
- Else
- For PartIndex = 0 To SeparatorIndex - 1
- Result = Result & SubParts(PartIndex) & IIf(PartIndex < SeparatorIndex - 1, " / ", vbNullString)
- Next PartIndex
- End If
- SplitBySpecificSeparator = Trim$(Result)
- End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement