0

I need to save, in a desktop folder, emails which match the following conditions:

  1. Subject starts with RE: FOR REVIEW
  2. 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
Community
  • 1
  • 1
  • 3
    I would recommend you remove the `On Error Resume Next` statement. It's completely masking any and all errors occurring in that section of code. After removing, try running the code again - and see if an actual error message appears. – Mistella Nov 27 '18 at 18:25
  • Also, `On Error Resume Next` does a fall-through into the "true" section of the if-statement when the error occurs during the if-condition. – Mistella Nov 27 '18 at 18:26
  • https://stackoverflow.com/a/31753321/1571407 – niton Nov 27 '18 at 20:27
  • Thanks guys for your suggestion. –  Nov 27 '18 at 21:19
  • Please look at the edit. The code shows no error but I am not getting the desired result. –  Nov 27 '18 at 21:27
  • Appears you do not know what (ByVal objItem As Object) does. objItem is what has been added to the folder. Refer to it by the correct name, not Item, nor a randomly newly created xMailItem – niton Nov 27 '18 at 23:21

1 Answers1

0

A suggested If/Else structure with appropriate mailitem.

Option Explicit

Private Sub InboxItems_ItemAdd(ByVal objItem As Object)

    Dim FSO
    Dim xMailItem As MailItem
    Dim xFilePath As String
    Dim xRegEx
    Dim xFileName As String

    If objItem.Class = olMail Then

        'objItem could be used directly but this is sometimes beneficial
        Set xMailItem = objItem

        If (xMailItem.subject Like "RE:FOR REVIEW*") Then

            If ((xMailItem.senderName = "Alpha") Or _
                (xMailItem.senderName = "Beta") Or _
                (xMailItem.senderName = "Gamma")) Then

                If MsgBox("Do you want to save this email?", vbYesNo + vbQuestion, "Reminder") = vbYes Then

                    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 = "\||\/|\<|\>|""|:|\*|\\|\?"

                    xFileName = xRegEx.Replace(xMailItem.subject, "")

                    xMailItem.SaveAs xFilePath & "\" & xFileName & ".html", olHTML

                End If

            End If

        End If

    End If

End Sub
niton
  • 8,771
  • 21
  • 32
  • 52