I need to save, in a desktop folder, emails which match the following conditions:
- Subject starts with RE: FOR REVIEW
- Sender Names are: Alpha, Beta or Gamma (example)
If both of these conditions are met, a Yes/No MsgBox should pop up.
Code:
Private WithEvents InboxItems As Outlook.Items
Sub Application_Startup()
Dim xNameSpace As Outlook.NameSpace
Set xNameSpace = Outlook.Application.Session
Set InboxItems = xNameSpace.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub InboxItems_ItemAdd(ByVal objItem As Object)
Dim FSO
Dim xMailItem As Outlook.MailItem
Dim xFilePath As String
Dim xRegEx
Dim xFileName As String
Dim Output As String
Dim Item As Object
On Error Resume Next
If (Item.Subject Like "RE:FOR REVIEW*") And ((Item.SenderName = "Alpha") Or (Item.SenderName = "Beta") or (Item.SenderName = "Gamma") ) Then
Output = MsgBox("Do you want to save this email?", vbYesNo + vbQuestion, "Reminder")
If Output = vbNo Then Exit Sub
Else
xFilePath = CreateObject("WScript.Shell").SpecialFolders(16)
xFilePath = "C:\Users\ABC\Desktop\Test"
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FolderExists(xFilePath) = False Then
FSO.CreateFolder (xFilePath)
End If
Set xRegEx = CreateObject("vbscript.regexp")
xRegEx.Global = True
xRegEx.IgnoreCase = False
xRegEx.Pattern = "\||\/|\<|\>|""|:|\*|\\|\?"
If objItem.Class = olMail Then
Set xMailItem = objItem
xFileName = xRegEx.Replace(xMailItem.Subject, "")
xMailItem.SaveAs xFilePath & "\" & xFileName & ".html", olHTML
End If
End If
Exit Sub
End Sub
Problem:
The pop up comes up for all the subject line and all the users.
I tried using nested If else but didn't get the correct output.
The whole code is in ThisOutlookSession.
Edit 1,
I removed the On Error Resume Next
.
The edited code is :
Private WithEvents InboxItems As Outlook.Items
Sub Application_Startup()
Dim xNameSpace As Outlook.NameSpace
Set xNameSpace = Outlook.Application.Session
Set InboxItems = xNameSpace.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub InboxItems_ItemAdd(ByVal objItem As Object)
Dim FSO
Dim xMailItem As Outlook.MailItem
Dim xFilePath As String
Dim xRegEx
Dim xFileName As String
Dim Output As String
If objItem.Class = olMail Then '**
Set xMailItem = Application.CreateItem(olMailItem) '**
If (xMailItem.Subject Like "RE:FOR REVIEW*") And ((xMailItem.SenderName = "Alpha") Or (xMailItem.SenderName = "Beta") or (xMailItem.SenderName = "Gamma") ) Then
Output = MsgBox("Do you want to save this email?", vbYesNo + vbQuestion, "Reminder")
If Output = vbNo Then Exit Sub
Else
xFilePath = CreateObject("WScript.Shell").SpecialFolders(16)
xFilePath = "C:\Users\abc\Desktop\Test"
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FolderExists(xFilePath) = False Then
FSO.CreateFolder (xFilePath)
End If
Set xRegEx = CreateObject("vbscript.regexp")
xRegEx.Global = True
xRegEx.IgnoreCase = False
xRegEx.Pattern = "\||\/|\<|\>|""|:|\*|\\|\?"
If objItem.Class = olMail Then
Set xMailItem = objItem
xFileName = xRegEx.Replace(xMailItem.Subject, "")
xMailItem.SaveAs xFilePath & "\" & xFileName & ".html", olHTML
End If
End If
End If
Exit Sub
End Sub