Advertisement
Guest User

Enailsummary

a guest
Jan 17th, 2025
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.70 KB | None | 0 0
  1. Sub GenerateOrderUpdateSummaryHourly()
  2.  
  3. ' Outlook objects
  4.  
  5. Dim olApp As Outlook.Application
  6.  
  7. Dim olNS As Outlook.NameSpace
  8.  
  9. Dim sentFolder As Outlook.folder
  10.  
  11. Dim responseFolder As Outlook.folder
  12.  
  13. Dim sentItems As Outlook.Items
  14.  
  15. Dim responseItems As Outlook.Items
  16.  
  17. Dim sentEmail As Outlook.mailItem
  18.  
  19. Dim responseEmail As Outlook.mailItem
  20.  
  21.  
  22.  
  23. ' Variables
  24.  
  25. Dim folderPairs As Variant
  26.  
  27. Dim sentFolderName As String
  28.  
  29. Dim responseFolderName As String
  30.  
  31. Dim orderNumbers As Collection
  32.  
  33. Dim summary As String
  34.  
  35. Dim orderStatus As String
  36.  
  37. Dim orderNumber As Variant
  38.  
  39. Dim carrierEmail As String
  40.  
  41. Dim carrierResponseBody As String
  42.  
  43.  
  44.  
  45. ' Initialize Outlook objects
  46.  
  47. Set olApp = Application
  48.  
  49. Set olNS = olApp.GetNamespace("MAPI")
  50.  
  51.  
  52.  
  53. ' **REPLACE WITH YOUR FOLDER PAIRS**
  54.  
  55. folderPairs = Array( _
  56.  
  57. Array("Sent Items", "PSF"), _
  58.  
  59. Array("Sent Items", "BNCC"), _
  60.  
  61. Array("Sent Items", "ADDT"), _
  62.  
  63. Array("Sent Items", "PDDT") _
  64.  
  65. )
  66.  
  67.  
  68.  
  69. ' Initialize summary
  70.  
  71. summary = "Order Update Summary" & vbCrLf & vbCrLf
  72.  
  73. summary = summary & "Order Number" & vbTab & "Carrier Email" & vbTab & "Status" & vbCrLf
  74.  
  75.  
  76.  
  77. ' Loop through each folder pair
  78.  
  79. Dim pair As Variant
  80.  
  81. For Each pair In folderPairs
  82.  
  83. sentFolderName = pair(0)
  84.  
  85. responseFolderName = pair(1)
  86.  
  87.  
  88.  
  89. ' Access the Sent Items folder
  90.  
  91. Set sentFolder = olNS.GetDefaultFolder(olFolderSentMail) ' Direct access to Sent Items
  92.  
  93.  
  94.  
  95. ' Access response folder using recursive function
  96.  
  97. Set responseFolder = GetFolderByName(olNS.GetDefaultFolder(olFolderInbox), responseFolderName)
  98.  
  99.  
  100.  
  101. ' Check if response folder has any items
  102.  
  103. If Not responseFolder Is Nothing And responseFolder.Items.Count > 0 Then
  104.  
  105. Debug.Print "Response Folder Name: " & responseFolderName
  106.  
  107. Set sentItems = sentFolder.Items
  108.  
  109. Set responseItems = responseFolder.Items
  110.  
  111.  
  112.  
  113. ' Loop through sent emails
  114.  
  115. For Each sentEmail In sentItems
  116.  
  117. If sentEmail.Class = olMail Then
  118.  
  119. ' Extract order numbers from the sent email
  120.  
  121. Set orderNumbers = ExtractOrderNumbers(sentEmail.Body)
  122.  
  123.  
  124.  
  125. ' Loop through carrier responses
  126.  
  127. For Each responseEmail In responseItems
  128.  
  129. If responseEmail.Class = olMail Then
  130.  
  131. carrierResponseBody = responseEmail.Body
  132.  
  133. carrierEmail = responseEmail.SenderEmailAddress
  134.  
  135.  
  136.  
  137. ' Match each order number in the response
  138.  
  139. For Each orderNumber In orderNumbers
  140.  
  141. ' Call the function to get the status based on the response
  142.  
  143. orderStatus = GetCarrierResponse(CStr(orderNumber), carrierResponseBody)
  144.  
  145.  
  146.  
  147. ' Add to the summary
  148.  
  149. summary = summary & CStr(orderNumber) & vbTab & carrierEmail & vbTab & orderStatus & vbCrLf
  150.  
  151. Next orderNumber
  152.  
  153. End If
  154.  
  155. Next responseEmail
  156.  
  157. End If
  158.  
  159. Next sentEmail
  160.  
  161. Else
  162.  
  163. summary = summary & "Response folder '" & responseFolderName & "' not found or empty" & vbCrLf
  164.  
  165. End If
  166.  
  167. Next pair
  168.  
  169.  
  170.  
  171. ' Send the summary email
  172.  
  173. Call SendSummaryReport(summary)
  174.  
  175. End Sub
  176.  
  177.  
  178.  
  179. ' Function to get a folder by its name within a parent folder
  180.  
  181. Function GetFolderByName(parentFolder As Outlook.folder, folderName As String) As Outlook.folder
  182.  
  183. Dim subFolder As Outlook.folder
  184.  
  185. Dim trackingFolder As Outlook.folder
  186.  
  187. On Error Resume Next ' In case folder is not found
  188.  
  189.  
  190.  
  191. ' First, find the "Tracking" subfolder under Inbox
  192.  
  193. Set trackingFolder = parentFolder.Folders("Tracking")
  194.  
  195.  
  196.  
  197. If Not trackingFolder Is Nothing Then
  198.  
  199. ' Now, find the specific carrier subfolder under "Tracking"
  200.  
  201. Set subFolder = trackingFolder.Folders(folderName)
  202.  
  203. End If
  204.  
  205.  
  206.  
  207. If Not subFolder Is Nothing Then
  208.  
  209. Set GetFolderByName = subFolder
  210.  
  211. Else
  212.  
  213. Set GetFolderByName = Nothing
  214.  
  215. End If
  216.  
  217.  
  218.  
  219. On Error GoTo 0 ' Reset error handling
  220.  
  221. End Function
  222.  
  223.  
  224.  
  225. ' Function to extract order numbers from email body
  226.  
  227. Function ExtractOrderNumbers(emailBody As String) As Collection
  228.  
  229. Dim orderNumbers As New Collection
  230.  
  231. Dim orderNumber As String
  232.  
  233. Dim regex As Object
  234.  
  235. Set regex = CreateObject("VBScript.RegExp")
  236.  
  237.  
  238.  
  239. ' Regular expression pattern to match order numbers (example: 1234567890)
  240.  
  241. regex.IgnoreCase = True
  242.  
  243. regex.Global = True
  244.  
  245. regex.Pattern = "\d{10}" ' Match any 10-digit number
  246.  
  247.  
  248.  
  249. ' Find matches in the email body
  250.  
  251. If regex.Test(emailBody) Then
  252.  
  253. Dim matches As Object
  254.  
  255. Set matches = regex.Execute(emailBody)
  256.  
  257.  
  258.  
  259. For Each match In matches
  260.  
  261. orderNumbers.Add match.Value
  262.  
  263. Next match
  264.  
  265. End If
  266.  
  267.  
  268.  
  269. Set ExtractOrderNumbers = orderNumbers
  270.  
  271. End Function
  272.  
  273.  
  274.  
  275. ' Function to determine the carrier's response status based on the email body
  276.  
  277. Function GetCarrierResponse(orderNumber As String, responseBody As String) As String
  278.  
  279. Dim status As String
  280.  
  281.  
  282.  
  283. ' Check for different responses in the carrier email body
  284.  
  285. If InStr(1, responseBody, "delivered", vbTextCompare) > 0 Then
  286.  
  287. status = "Delivered"
  288.  
  289. ElseIf InStr(1, responseBody, "broke down", vbTextCompare) > 0 Then
  290.  
  291. status = "Broke Down"
  292.  
  293. ElseIf InStr(1, responseBody, "late", vbTextCompare) > 0 Then
  294.  
  295. status = "Late"
  296.  
  297. ElseIf InStr(1, responseBody, "on time", vbTextCompare) > 0 Then
  298.  
  299. status = "On Time"
  300.  
  301. Else
  302.  
  303. status = "Unknown"
  304.  
  305. End If
  306.  
  307.  
  308.  
  309. ' Return the status
  310.  
  311. GetCarrierResponse = status
  312.  
  313. End Function
  314.  
  315.  
  316.  
  317. ' Function to send the summary report email
  318.  
  319. Sub SendSummaryReport(summary As String)
  320.  
  321. Dim olApp As Outlook.Application
  322.  
  323. Dim olMail As Outlook.mailItem
  324.  
  325.  
  326.  
  327. Set olApp = Application
  328.  
  329. Set olMail = olApp.CreateItem(olMailItem)
  330.  
  331.  
  332.  
  333. ' Set email properties
  334.  
  335. olMail.Subject = "Order Update Summary"
  336.  
  337. olMail.To = [email protected] ' Replace with your email
  338.  
  339. olMail.Body = summary
  340.  
  341.  
  342.  
  343. ' Send the email
  344.  
  345. olMail.Send
  346.  
  347. End Sub
  348.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement