Advertisement
Guest User

Untitled

a guest
Jul 28th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. Const xlUp As Long = -4162
  2. Sub ExportToExcel(MyMail As MailItem)
  3.  
  4. Dim strID As String, olNS As Outlook.NameSpace
  5. Dim olMail As Outlook.MailItem
  6. Dim strFileName As String
  7. Dim strBody As String
  8. Dim Reg1 As RegExp
  9. Dim M1 As MatchCollection
  10. Dim M As Match
  11.  
  12. Set Reg1 = New RegExp
  13. With Reg1
  14. .Pattern = "/http://www.changedetection.com/log(.*)/ig"
  15. .Global = False
  16. End With
  17.  
  18.  
  19. If Reg1.test(olMail.Body) Then
  20.  
  21. Set M1 = Reg1.Execute(olMail.Body)
  22. For Each M In M1
  23. strBody = M.SubMatches(1)
  24. Next
  25. End If
  26.  
  27. '~~> Excel Variables
  28. Dim oXLApp As Object, oXLwb As Object, oXLws As Object
  29. Dim lRow As Long
  30.  
  31. strID = MyMail.EntryID
  32. Set olNS = Application.GetNamespace("MAPI")
  33. Set olMail = olNS.GetItemFromID(strID)
  34.  
  35.  
  36.  
  37. '~~> Establish an EXCEL application object
  38. On Error Resume Next
  39. Set oXLApp = GetObject(, "Excel.Application")
  40.  
  41. '~~> If not found then create new instance
  42. If Err.Number <> 0 Then
  43. Set oXLApp = CreateObject("Excel.Application")
  44. End If
  45. Err.Clear
  46. On Error GoTo 0
  47.  
  48. '~~> Show Excel
  49. oXLApp.Visible = True
  50.  
  51. '~~> Open the relevant file
  52. Set oXLwb = oXLApp.Workbooks.Open("M:MonitorMonitor_Test_1.xlsx")
  53.  
  54. '~~> Set the relevant output sheet. Change as applicable
  55. Set oXLws = oXLwb.Sheets("Test")
  56.  
  57. lRow = oXLws.Range("A" & oXLApp.Rows.Count).End(xlUp).Row + 1
  58.  
  59. '~~> Write to outlook
  60. With oXLws
  61. '
  62. '~~> Code here to output data from email to Excel File
  63. '~~> For example
  64. '
  65. .Range("A" & lRow).Value = olMail.Subject
  66. .Range("B" & lRow).Value = olMail.SenderName
  67. .Range("C" & lRow).Value = strBody
  68.  
  69. '
  70. End With
  71.  
  72. '~~> Close and Clean up Excel
  73. oXLwb.Close (True)
  74. oXLApp.Quit
  75.  
  76. Set Reg1 = Nothing
  77. Set oXLws = Nothing
  78. Set oXLwb = Nothing
  79. Set oXLApp = Nothing
  80.  
  81. Set olMail = Nothing
  82. Set olNS = Nothing
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement