Advertisement
DeadshotOMEGA

Email Subject Management Code

Jan 17th, 2024
2,115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VisualBasic 6.25 KB | Source Code | 0 0
  1. Option Explicit
  2. '@ModuleDescription "Handles processing of incoming emails in Outlook, separating language parts in subject lines."
  3. Public WithEvents InboxItems As Outlook.Items
  4.  
  5. '@Description "Sets up the event handler for incoming emails in the default Inbox folder."
  6. Private Sub Application_Startup()
  7.     Dim MailNamespace As Outlook.NameSpace
  8.     Set MailNamespace = Application.GetNamespace("MAPI")
  9.     Set InboxItems = MailNamespace.GetDefaultFolder(olFolderInbox).Items
  10. End Sub
  11.  
  12. '@Description "Processes new emails as they arrive if they are MailItems."
  13. Private Sub InboxItems_ItemAdd(ByVal Item As Object)
  14.     If TypeName(Item) = "MailItem" Then
  15.         On Error GoTo ErrorHandler
  16.         ProcessIncomingEmail Item
  17.         Exit Sub
  18.  
  19. ErrorHandler:
  20.         ' Log error to the Immediate Window for debugging purposes.
  21. Debug.Print "Error processing email: " & Err.Description
  22.     End If
  23. End Sub
  24.  
  25. '@Description "Processes the given MailItem, separating the subject into English and French components."
  26. '@Param mail The MailItem to process.
  27. Private Sub ProcessIncomingEmail(ByVal mail As Outlook.MailItem)
  28.     On Error GoTo ErrorHandler
  29.  
  30.     Dim SubjectRegex As Object
  31.     Set SubjectRegex = CreateObject("VBScript.RegExp")
  32.     SubjectRegex.Pattern = "^[SDT]\d{5,9}"
  33.  
  34.     If SubjectRegex.Test(mail.Subject) Then
  35.         Dim Sections() As String
  36.         Sections = Split(mail.Subject, " // Description: ")
  37.  
  38.         If UBound(Sections) >= 1 Then
  39.             Dim TicketNumber As String
  40.             TicketNumber = GetTicketNumber(Sections(0))
  41.  
  42.             Dim FirstSectionWithoutTicket As String
  43.             FirstSectionWithoutTicket = RemoveTicketNumber(Sections(0), TicketNumber)
  44.  
  45.             Dim EnglishSubject As String, FrenchSubject As String
  46.             EnglishSubject = TicketNumber & " " & SplitBySpecificSeparator(FirstSectionWithoutTicket) & " // Description: " & SplitBySpecificSeparator(Sections(1))
  47.             FrenchSubject = TicketNumber & " " & SplitBySpecificSeparator(FirstSectionWithoutTicket, True) & " // Description: " & SplitBySpecificSeparator(Sections(1), True)
  48.  
  49.             With mail
  50.                 Dim UserPropertyF As Outlook.UserProperty
  51.                 Set UserPropertyF = .UserProperties.Find("FrenchSubject", True)
  52.                 If UserPropertyF Is Nothing Then
  53.                     Set UserPropertyF = .UserProperties.Add("FrenchSubject", olText)
  54.                 End If
  55.                 UserPropertyF.Value = FrenchSubject
  56.  
  57.                 Dim UserPropertyE As Outlook.UserProperty
  58.                 Set UserPropertyE = .UserProperties.Find("EnglishSubject", True)
  59.                 If UserPropertyE Is Nothing Then
  60.                     Set UserPropertyE = .UserProperties.Add("EnglishSubject", olText)
  61.                 End If
  62.                 UserPropertyE.Value = EnglishSubject
  63.                 .Save
  64.             End With
  65.         End If
  66.  
  67.     Else
  68.         With mail
  69.             Dim UserPropertyE2 As Outlook.UserProperty
  70.             Set UserPropertyE2 = .UserProperties.Find("EnglishSubject", True)
  71.             If UserPropertyE2 Is Nothing Then
  72.                 Set UserPropertyE2 = .UserProperties.Add("EnglishSubject", olText)
  73.             End If
  74.             UserPropertyE2.Value = mail.Subject
  75.             .Save
  76.         End With
  77.     End If
  78.  
  79.     Exit Sub
  80.  
  81. ErrorHandler:
  82.     ' Handle error appropriately
  83. Debug.Print "Error in ProcessEmail: " & Err.Description
  84. End Sub
  85.  
  86. '@Description "Removes the ticket number from the start of the given string, if present."
  87. '@Param text The string to remove the ticket number from.
  88. '@Param ticketNumber The ticket number to remove.
  89. '@Returns String without the ticket number at the beginning.
  90. Private Function RemoveTicketNumber(ByVal Text As String, ByVal TicketNumber As String) As String
  91.     ' Check if the start of Text matches the TicketNumber
  92.    If Left(Text, Len(TicketNumber)) = TicketNumber Then
  93.         ' Remove the TicketNumber and return the trimmed remaining string
  94.        RemoveTicketNumber = Trim(Mid(Text, Len(TicketNumber) + 1))
  95.     Else
  96.         RemoveTicketNumber = Text
  97.     End If
  98. End Function
  99.  
  100. '@Description "Extracts the ticket number (e.g., S###, T###, D###) from the given string."
  101. '@Param text The string to extract the ticket number from.
  102. '@Returns String representing the extracted ticket number, if found.
  103. Private Function GetTicketNumber(ByVal Text As String) As String
  104.     On Error GoTo ErrorHandler
  105.     Dim TicketRegex As Object
  106.     Set TicketRegex = CreateObject("VBScript.RegExp")
  107.     TicketRegex.Pattern = "^[SDT]\d{5,9}"
  108.  
  109.     If TicketRegex.Test(Text) Then
  110.         GetTicketNumber = TicketRegex.Execute(Text)(0)
  111.     Else
  112.         GetTicketNumber = vbNullString
  113.     End If
  114.  
  115.     Exit Function
  116.  
  117. ErrorHandler:
  118.     ' Handle error appropriately
  119. Debug.Print "Error in GetTicketNumber: " & Err.Description
  120.     GetTicketNumber = vbNullString
  121. End Function
  122.  
  123. '@Description "Splits the given string at a specific '/', determined by the total count of '/' (divided by 2), to separate English from French."
  124. '@Param text The string to split.
  125. '@Param isFrench Boolean flag indicating if the French part is to be returned.
  126. '@Returns String containing either the English or French part of the text.
  127. Private Function SplitBySpecificSeparator(ByVal Text As String, Optional ByVal IsFrench As Boolean = False) As String
  128.     Dim SubParts As Variant
  129.     SubParts = Split(Text, "/")
  130.  
  131.     ' Calculate the specific index for splitting, assuming even distribution of '/' for English and French
  132.    Dim SeparatorIndex As Long
  133.     If UBound(SubParts) = 1 Then
  134.         SeparatorIndex = 1
  135.     Else
  136.         SeparatorIndex = (UBound(SubParts) + 1) \ 2
  137.     End If
  138.  
  139.     Dim Result As String
  140.     Result = vbNullString                        ' Initialize the result string
  141.  
  142.     Dim PartIndex As Long
  143.     If IsFrench Then
  144.         For PartIndex = SeparatorIndex To UBound(SubParts)
  145.             Result = Result & SubParts(PartIndex) & IIf(PartIndex < UBound(SubParts), " / ", vbNullString)
  146.         Next PartIndex
  147.     Else
  148.         For PartIndex = 0 To SeparatorIndex - 1
  149.             Result = Result & SubParts(PartIndex) & IIf(PartIndex < SeparatorIndex - 1, " / ", vbNullString)
  150.         Next PartIndex
  151.     End If
  152.  
  153.     SplitBySpecificSeparator = Trim$(Result)
  154. End Function
Tags: vba
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement