Me_yashverma

Untitled

Aug 12th, 2024 (edited)
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub ExportAdvisoriesToExcel()
  2.     Dim olApp As Outlook.Application
  3.     Dim olNamespace As Outlook.Namespace
  4.     Dim olFolder As Outlook.MAPIFolder
  5.     Dim olMail As Outlook.MailItem
  6.     Dim xlApp As Object
  7.     Dim xlWB As Object
  8.     Dim xlSheet As Object
  9.     Dim i As Integer
  10.  
  11.     Set olApp = Outlook.Application
  12.     Set olNamespace = olApp.GetNamespace("MAPI")
  13.     Set olFolder = olNamespace.GetDefaultFolder(olFolderInbox) 'Adjust folder if necessary
  14.  
  15.     Set xlApp = CreateObject("Excel.Application")
  16.     Set xlWB = xlApp.Workbooks.Add
  17.     Set xlSheet = xlWB.Sheets(1)
  18.  
  19.     ' Set Headers
  20.    xlSheet.Cells(1, 1).Value = "Title"
  21.     xlSheet.Cells(1, 2).Value = "Severity"
  22.     xlSheet.Cells(1, 3).Value = "Type"
  23.     xlSheet.Cells(1, 4).Value = "Date Received"
  24.  
  25.     i = 2
  26.  
  27.     For Each olMail In olFolder.Items
  28.         If InStr(olMail.Subject, "Advisory") > 0 Then ' Adjust keyword as necessary
  29.            xlSheet.Cells(i, 1).Value = olMail.Subject
  30.            
  31.             ' Example parsing severity and type (custom logic required)
  32.            ' xlSheet.Cells(i, 2).Value = ParseSeverity(olMail.Body)
  33.            ' xlSheet.Cells(i, 3).Value = ParseType(olMail.Body)
  34.            
  35.             xlSheet.Cells(i, 4).Value = olMail.ReceivedTime
  36.             i = i + 1
  37.         End If
  38.     Next olMail
  39.  
  40.     xlWB.SaveAs "C:\Path\To\Save\AdvisoryReport.xlsx" 'Ensure path exists or adjust accordingly
  41.    xlWB.Close
  42.     xlApp.Quit
  43.  
  44.     MsgBox "Export Completed"
  45. End Sub
Advertisement
Add Comment
Please, Sign In to add comment