Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Option Explicit
- '-----------------------------------------------
- ' Module Level Constants
- Private Const msSubFolder As String = "Coding" ' Name of the sub folder where the e-mails are stored
- Private Const msInfoTag As String = "Street Address: " ' The string that appears right before the info you want
- Private Const miDaysToCheck As Integer = 5 ' The number of days worth of e-mails to search
- Private Const msWorksheetName As String = "Demo" ' The name of the output worksheet
- Private Const msWorksheetRange As String = "SEARCH_RESULTS" ' The name of the output range
- Sub FindAddressesInOutlook()
- 'Declare All Variabels Used
- Dim oFolder As Outlook.MAPIFolder
- Dim oRestrictedList As Outlook.Items
- Dim oMailItem As Outlook.MailItem
- Dim sStreetAddress As String
- Dim iItemCount As Integer
- Dim iItemFound As Integer
- 'Sets oFolder to the Outlook Folder where the e-mails are saved
- Set oFolder = Outlook.Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
- Set oFolder = oFolder.Folders(msSubFolder)
- 'Returns all e-mails in oFolder that are marked unread from the last X number of days
- Set oRestrictedList = oFolder.Items.Restrict("[ReceivedTime] >= '" & Format(Int(Now) - miDaysToCheck, "ddddd h:nn AMPM") & "'")
- Set oRestrictedList = oRestrictedList.Restrict("[Unread]=true")
- 'Clears the output range of previous entries
- ThisWorkbook.Sheets(msWorksheetName).Range(msWorksheetRange).Value = ""
- 'Loops through the e-mails from oRestrictedList
- For iItemCount = 1 To oRestrictedList.Count
- 'If the 'Info Tag' (in this case 'Street Address: ' is found, save the address
- If InStr(1, oRestrictedList(iItemCount).Body, msInfoTag) <> 0 Then
- 'Strip everything prior to the address
- sStreetAddress = Mid(oRestrictedList(iItemCount).Body, InStr(1, oRestrictedList(iItemCount).Body, msInfoTag) + Len(msInfoTag))
- 'Strip everything after the address
- sStreetAddress = Trim(Left(sStreetAddress, InStr(1, sStreetAddress, vbNewLine) - 1))
- 'Save the street address to the output range
- ThisWorkbook.Sheets(msWorksheetName).Range(msWorksheetRange).Cells(1, 1).Offset(iItemFound).Value = sStreetAddress
- 'Increment the counter up 1 so it outputs to the next row
- iItemFound = iItemFound + 1
- End If
- Next
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement