Advertisement
Guest User

Untitled

a guest
May 9th, 2017
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2.  
  3. '-----------------------------------------------
  4. ' Module Level Constants
  5.  
  6. Private Const msSubFolder As String = "Coding"              ' Name of the sub folder where the e-mails are stored
  7. Private Const msInfoTag As String = "Street Address: "      ' The string that appears right before the info you want
  8. Private Const miDaysToCheck As Integer = 5                  ' The number of days worth of e-mails to search
  9. Private Const msWorksheetName As String = "Demo"            ' The name of the output worksheet
  10. Private Const msWorksheetRange As String = "SEARCH_RESULTS" ' The name of the output range
  11.  
  12. Sub FindAddressesInOutlook()
  13.    
  14.     'Declare All Variabels Used
  15.    Dim oFolder As Outlook.MAPIFolder
  16.     Dim oRestrictedList As Outlook.Items
  17.     Dim oMailItem As Outlook.MailItem
  18.     Dim sStreetAddress As String
  19.     Dim iItemCount As Integer
  20.     Dim iItemFound As Integer
  21.    
  22.     'Sets oFolder to the Outlook Folder where the e-mails are saved
  23.    Set oFolder = Outlook.Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
  24.     Set oFolder = oFolder.Folders(msSubFolder)
  25.    
  26.     'Returns all e-mails in oFolder that are marked unread from the last X number of days
  27.    Set oRestrictedList = oFolder.Items.Restrict("[ReceivedTime] >= '" & Format(Int(Now) - miDaysToCheck, "ddddd h:nn AMPM") & "'")
  28.     Set oRestrictedList = oRestrictedList.Restrict("[Unread]=true")
  29.    
  30.     'Clears the output range of previous entries
  31.    ThisWorkbook.Sheets(msWorksheetName).Range(msWorksheetRange).Value = ""
  32.    
  33.     'Loops through the e-mails from oRestrictedList
  34.    For iItemCount = 1 To oRestrictedList.Count
  35.    
  36.         'If the 'Info Tag' (in this case 'Street Address: ' is found, save the address
  37.        If InStr(1, oRestrictedList(iItemCount).Body, msInfoTag) <> 0 Then
  38.        
  39.             'Strip everything prior to the address
  40.            sStreetAddress = Mid(oRestrictedList(iItemCount).Body, InStr(1, oRestrictedList(iItemCount).Body, msInfoTag) + Len(msInfoTag))
  41.            
  42.             'Strip everything after the address
  43.            sStreetAddress = Trim(Left(sStreetAddress, InStr(1, sStreetAddress, vbNewLine) - 1))
  44.            
  45.             'Save the street address to the output range
  46.            ThisWorkbook.Sheets(msWorksheetName).Range(msWorksheetRange).Cells(1, 1).Offset(iItemFound).Value = sStreetAddress
  47.            
  48.             'Increment the counter up 1 so it outputs to the next row
  49.            iItemFound = iItemFound + 1
  50.            
  51.         End If
  52.    
  53.     Next
  54.    
  55. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement