Below is the code i am using to find an email with the subject "raw sales log" in my inbox and copy and paste it into another worksheet. It works fine, but when i have more than one email with the same subject, it gives me an error. I am looking to add some code that will look for the subject i have specified, but only find the most recent one, and ignore the others
Code:
Option Explicit
Sub REFRESH()
Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olInBox As Outlook.MAPIFolder
Dim olMoveToFolder As Outlook.MAPIFolder
Dim olItems As Outlook.Items
Dim olAtt As Outlook.Attachment
Dim strSaveToFolder As String
Dim strPathAndFilename As String
Dim Ans As Long
Dim i As Long
Dim LoginName As String
LoginName = Environ("username")
strSaveToFolder = "C:\Users\" & LoginName & "\Desktop\" 'change the path accordingly
If Right(strSaveToFolder, 1) <> "\" Then strSaveToFolder = strSaveToFolder & "\"
Set olApp = CreateObject("Outlook.Application")
Set olNS = olApp.GetNamespace("MAPI")
Set olInBox = olNS.GetDefaultFolder(olFolderInbox)
Set olMoveToFolder = olInBox.Folders(olFolderDeletedItems) 'change the name of the subfolder accordingly
Set olItems = olInBox.Items.Restrict("[Subject] = 'raw sales log'")
For i = olItems.Count To 1 Step -1
If olItems(i).Attachments.Count > 0 Then
For Each olAtt In olItems(i).Attachments
strPathAndFilename = strSaveToFolder & olAtt.Filename
If Len(Dir(strPathAndFilename, vbNormal)) = 0 Then
olAtt.SaveAsFile strPathAndFilename
olItems(i).Save
Else
Ans = MsgBox(olAtt.Filename & " already exists. Overwrite file?", vbQuestion + vbYesNo)
If Ans = vbYes Then
olAtt.SaveAsFile strPathAndFilename
olItems(i).Save
End If
End If
Next olAtt
olItems(i).Move olMoveToFolder
End If
Next i
Set olApp = Nothing
Set olNS = Nothing
Set olInBox = Nothing
Set olMoveToFolder = Nothing
Set olItems = Nothing
Set olAtt = Nothing
Sheets("COPY RAW DATA HERE").Select
Cells.Select
Selection.ClearContents
Workbooks.Open "c:\users\" & LoginName & "\desktop\rawsaleslog.csv"
Cells.Select
Selection.Copy
Windows("ny sales calculator ultimate.xlsm").Activate
Cells.Select
ActiveSheet.Paste
Range("A1").Select
Sheets("PIVOT TABLE").Select
Application.ScreenUpdating = False
Sheets("COPY RAW DATA HERE").Select
Range("A1").Select
Columns("E:E").Select
Selection.Replace What:="5027", Replacement:="5525", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="5605", Replacement:="5600", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="5086", Replacement:="5101", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("F4").Select
Sheets("PIVOT TABLE").Select
Range("A1").Select
ActiveSheet.PivotTables("PivotTable1").PivotCache.REFRESH
ActiveSheet.PivotTables("PivotTable1").PivotFields("Type").ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("Type").CurrentPage = "SLD"
ActiveSheet.PivotTables("PivotTable1").PivotFields("branch").ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("branch").CurrentPage = "all"
ActiveSheet.PivotTables("PivotTable1").PivotFields("emp type").ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("emp type").CurrentPage = "all"
ActiveSheet.PivotTables("PivotTable1").PivotFields("Selling Bunit").ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("Selling Bunit").CurrentPage = "ALL"
ActiveSheet.PivotTables("PivotTable1").PivotFields("lead src").ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("Lead Src").CurrentPage = _
"(All)"
ActiveSheet.PivotTables("PivotTable1").PivotFields("branch").CurrentPage = "BRANCH"
ActiveSheet.PivotTables("PivotTable1").PivotFields("emp type").CurrentPage = "SALES REPS"
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Lead Src")
.PivotItems("0").Visible = False
End With
Range("A1").Select
Application.ScreenUpdating = True
End Sub
Sub screen_update()
Application.ScreenUpdating = True
End Sub