Outlook Delete email after is saved











up vote
9
down vote

favorite
1












I am very limited with my VBA skills but I got so far that now I want to finish this project.



I have below VBA code working nicely in my outlook. It saves required email into my drive.



Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set Items = objNS.GetDefaultFolder(olFolderInbox).Items

End Sub

Private Sub Items_ItemAdd(ByVal item As Object)

On Error GoTo ErrorHandler

'Only act if it's a MailItem
Dim Msg As Outlook.MailItem
If TypeName(item) = "MailItem" Then
Set Msg = item

'Change variables to match need. Comment or delete any part unnecessary.
If (Msg.SenderEmailAddress = "noreply@test.com") Or _
(Msg.Subject = "Smartsheet") Or _
(Msg.Subject = "Defects") And _
(Msg.Attachments.Count >= 1) Then

'Set folder to save in.
Dim olDestFldr As Outlook.MAPIFolder
Dim myAttachments As Outlook.Attachments
Dim Att As String

'location to save in. Can be root drive or mapped network drive.
Const attPath As String = "C:"


' save attachment
Set myAttachments = item.Attachments
Att = myAttachments.item(1).DisplayName
myAttachments.item(1).SaveAsFile attPath & Att

' mark as read
Msg.UnRead = False

End If
End If


ProgramExit:
Exit Sub

ErrorHandler:
MsgBox Err.number & " - " & Err.Description
Resume ProgramExit
End Sub


I want to now add the code to move email after its attachment is saved to my Test folder. Test folder is under Inbox in my outlook.



I have added
Set FldrDest = Session.Folders("Address1").Folders("Inbox").Folders("Test")



under Private Sub Application_Startup() and then I added code into my VBA.



Code is after ' mark as read



If .Parent.Name = "Test" And .Parent.Parent.Name = "Inbox" Then
' MailItem is already in destination folder
Else
.Move FldrDest
End If


No other changes but it gives me compilation errors.










share|improve this question




























    up vote
    9
    down vote

    favorite
    1












    I am very limited with my VBA skills but I got so far that now I want to finish this project.



    I have below VBA code working nicely in my outlook. It saves required email into my drive.



    Private WithEvents Items As Outlook.Items

    Private Sub Application_Startup()
    Dim olApp As Outlook.Application
    Dim objNS As Outlook.NameSpace
    Set olApp = Outlook.Application
    Set objNS = olApp.GetNamespace("MAPI")
    Set Items = objNS.GetDefaultFolder(olFolderInbox).Items

    End Sub

    Private Sub Items_ItemAdd(ByVal item As Object)

    On Error GoTo ErrorHandler

    'Only act if it's a MailItem
    Dim Msg As Outlook.MailItem
    If TypeName(item) = "MailItem" Then
    Set Msg = item

    'Change variables to match need. Comment or delete any part unnecessary.
    If (Msg.SenderEmailAddress = "noreply@test.com") Or _
    (Msg.Subject = "Smartsheet") Or _
    (Msg.Subject = "Defects") And _
    (Msg.Attachments.Count >= 1) Then

    'Set folder to save in.
    Dim olDestFldr As Outlook.MAPIFolder
    Dim myAttachments As Outlook.Attachments
    Dim Att As String

    'location to save in. Can be root drive or mapped network drive.
    Const attPath As String = "C:"


    ' save attachment
    Set myAttachments = item.Attachments
    Att = myAttachments.item(1).DisplayName
    myAttachments.item(1).SaveAsFile attPath & Att

    ' mark as read
    Msg.UnRead = False

    End If
    End If


    ProgramExit:
    Exit Sub

    ErrorHandler:
    MsgBox Err.number & " - " & Err.Description
    Resume ProgramExit
    End Sub


    I want to now add the code to move email after its attachment is saved to my Test folder. Test folder is under Inbox in my outlook.



    I have added
    Set FldrDest = Session.Folders("Address1").Folders("Inbox").Folders("Test")



    under Private Sub Application_Startup() and then I added code into my VBA.



    Code is after ' mark as read



    If .Parent.Name = "Test" And .Parent.Parent.Name = "Inbox" Then
    ' MailItem is already in destination folder
    Else
    .Move FldrDest
    End If


    No other changes but it gives me compilation errors.










    share|improve this question


























      up vote
      9
      down vote

      favorite
      1









      up vote
      9
      down vote

      favorite
      1






      1





      I am very limited with my VBA skills but I got so far that now I want to finish this project.



      I have below VBA code working nicely in my outlook. It saves required email into my drive.



      Private WithEvents Items As Outlook.Items

      Private Sub Application_Startup()
      Dim olApp As Outlook.Application
      Dim objNS As Outlook.NameSpace
      Set olApp = Outlook.Application
      Set objNS = olApp.GetNamespace("MAPI")
      Set Items = objNS.GetDefaultFolder(olFolderInbox).Items

      End Sub

      Private Sub Items_ItemAdd(ByVal item As Object)

      On Error GoTo ErrorHandler

      'Only act if it's a MailItem
      Dim Msg As Outlook.MailItem
      If TypeName(item) = "MailItem" Then
      Set Msg = item

      'Change variables to match need. Comment or delete any part unnecessary.
      If (Msg.SenderEmailAddress = "noreply@test.com") Or _
      (Msg.Subject = "Smartsheet") Or _
      (Msg.Subject = "Defects") And _
      (Msg.Attachments.Count >= 1) Then

      'Set folder to save in.
      Dim olDestFldr As Outlook.MAPIFolder
      Dim myAttachments As Outlook.Attachments
      Dim Att As String

      'location to save in. Can be root drive or mapped network drive.
      Const attPath As String = "C:"


      ' save attachment
      Set myAttachments = item.Attachments
      Att = myAttachments.item(1).DisplayName
      myAttachments.item(1).SaveAsFile attPath & Att

      ' mark as read
      Msg.UnRead = False

      End If
      End If


      ProgramExit:
      Exit Sub

      ErrorHandler:
      MsgBox Err.number & " - " & Err.Description
      Resume ProgramExit
      End Sub


      I want to now add the code to move email after its attachment is saved to my Test folder. Test folder is under Inbox in my outlook.



      I have added
      Set FldrDest = Session.Folders("Address1").Folders("Inbox").Folders("Test")



      under Private Sub Application_Startup() and then I added code into my VBA.



      Code is after ' mark as read



      If .Parent.Name = "Test" And .Parent.Parent.Name = "Inbox" Then
      ' MailItem is already in destination folder
      Else
      .Move FldrDest
      End If


      No other changes but it gives me compilation errors.










      share|improve this question















      I am very limited with my VBA skills but I got so far that now I want to finish this project.



      I have below VBA code working nicely in my outlook. It saves required email into my drive.



      Private WithEvents Items As Outlook.Items

      Private Sub Application_Startup()
      Dim olApp As Outlook.Application
      Dim objNS As Outlook.NameSpace
      Set olApp = Outlook.Application
      Set objNS = olApp.GetNamespace("MAPI")
      Set Items = objNS.GetDefaultFolder(olFolderInbox).Items

      End Sub

      Private Sub Items_ItemAdd(ByVal item As Object)

      On Error GoTo ErrorHandler

      'Only act if it's a MailItem
      Dim Msg As Outlook.MailItem
      If TypeName(item) = "MailItem" Then
      Set Msg = item

      'Change variables to match need. Comment or delete any part unnecessary.
      If (Msg.SenderEmailAddress = "noreply@test.com") Or _
      (Msg.Subject = "Smartsheet") Or _
      (Msg.Subject = "Defects") And _
      (Msg.Attachments.Count >= 1) Then

      'Set folder to save in.
      Dim olDestFldr As Outlook.MAPIFolder
      Dim myAttachments As Outlook.Attachments
      Dim Att As String

      'location to save in. Can be root drive or mapped network drive.
      Const attPath As String = "C:"


      ' save attachment
      Set myAttachments = item.Attachments
      Att = myAttachments.item(1).DisplayName
      myAttachments.item(1).SaveAsFile attPath & Att

      ' mark as read
      Msg.UnRead = False

      End If
      End If


      ProgramExit:
      Exit Sub

      ErrorHandler:
      MsgBox Err.number & " - " & Err.Description
      Resume ProgramExit
      End Sub


      I want to now add the code to move email after its attachment is saved to my Test folder. Test folder is under Inbox in my outlook.



      I have added
      Set FldrDest = Session.Folders("Address1").Folders("Inbox").Folders("Test")



      under Private Sub Application_Startup() and then I added code into my VBA.



      Code is after ' mark as read



      If .Parent.Name = "Test" And .Parent.Parent.Name = "Inbox" Then
      ' MailItem is already in destination folder
      Else
      .Move FldrDest
      End If


      No other changes but it gives me compilation errors.







      vba outlook outlook-vba






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 at 13:28









      R3uK

      12.5k42859




      12.5k42859










      asked Nov 9 at 10:49









      Kalenji

      214214




      214214
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted
          +100










          The MailItem.Move is in fact a function that return the object that has been moved in the new destination.
          The old object is kind of "lost", see how to use it (i've commented the deletion part in the whole code ;) )



          Set Msg = .Move(FldrDest)
          MsgBox Msg.SenderEmailAddress & vbCrLf & Msg.Subject


          Full code with some suggestions for improvement (see '--> comments) :



          Private WithEvents Items As Outlook.Items

          'location to save in. Can be root drive or mapped network drive.
          '-->As it is a constant you can declare it there (and so, use it in the whole module if you want to do other things with it!)
          Private Const attPath As String = "C:"


          Private Sub Application_Startup()
          Dim olApp As Outlook.Application
          Dim objNS As Outlook.NameSpace
          Set olApp = Outlook.Application
          Set objNS = olApp.GetNamespace("MAPI")
          Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
          End Sub

          Private Sub Items_ItemAdd(ByVal item As Object)

          On Error GoTo ErrorHandler
          'Only act if it's a MailItem
          If TypeName(item) = "MailItem" Then
          Dim Msg As Outlook.MailItem
          '-->Use directly the parameter and keep it under wraps using "With", it'll improve efficiency
          With item
          'Change variables to match need. Comment or delete any part unnecessary.
          If (.SenderEmailAddress = "noreply@test.com" _
          Or .Subject = "Smartsheet" _
          Or .Subject = "Defects" _
          ) _
          And .Attachments.Count >= 1 Then


          Dim aAtt As Outlook.Attachment
          '-->Loop through the Attachments' collection
          for each aAtt in item.Attachments
          '-->You can either use aAtt.DisplayName or aAtt.FileName
          '-->You can test aAtt.Size or aAtt.Type

          'save attachment
          aAtt.SaveAsFile attPath & aAtt.DisplayName
          next aAtt

          'mark as read
          .UnRead = False

          Dim olDestFldr As Outlook.MAPIFolder
          Set FldrDest = Session.Folders("Address1").Folders("Inbox").Folders("Test")
          If .Parent.Name = "Test" And .Parent.Parent.Name = "Inbox" Then
          'MailItem is already in destination folder
          Else
          Set Msg = .Move(FldrDest)
          MsgBox Msg.SenderEmailAddress & vbCrLf & Msg.Subject
          'Msg.delete
          End If
          End If
          End With 'item
          End If


          ProgramExit:
          Exit Sub

          ErrorHandler:
          MsgBox Err.number & " - " & Err.Description
          Resume ProgramExit
          End Sub





          share|improve this answer























          • It gives an error - Expected: line number or lable or stement or end of statement and lines starting If ( .SenderEmailAddress = "noreply@test.com" _ to And .Attachments.Count >= 1 Then are in red.
            – Kalenji
            Nov 15 at 12:44










          • @Kalenji : My bad I forgot a _ (changed it in the edit), I wanted to show the parenthesis that is very important when you use And and Or. Because what you posted would probably not behaved as you expected it to ;)
            – R3uK
            Nov 15 at 13:00












          • Works like magic. Just removed the pop up as not needed :) I guess I need my outlook client up and running for this macro to be working? I mean as soon as I log off it won't work?
            – Kalenji
            Nov 15 at 14:31












          • Glad I could help. For this to work, you'll need Outlook up and running and to be logged on (if you're not, it won't find this Set FldrDest = Session.Folders("Address1").Folders("Inbox").Folders("Test")). I'm not so sure, but I'd say that the code will run when you reopen Outlook and send/receive mails. The only thing that the documentations says it's This event does not run when a large number of items are added to the folder at once.
            – R3uK
            Nov 16 at 13:26


















          up vote
          0
          down vote













          Easier than I thought. Just added a loop with Msg.Delete.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224268%2foutlook-delete-email-after-is-saved%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            4
            down vote



            accepted
            +100










            The MailItem.Move is in fact a function that return the object that has been moved in the new destination.
            The old object is kind of "lost", see how to use it (i've commented the deletion part in the whole code ;) )



            Set Msg = .Move(FldrDest)
            MsgBox Msg.SenderEmailAddress & vbCrLf & Msg.Subject


            Full code with some suggestions for improvement (see '--> comments) :



            Private WithEvents Items As Outlook.Items

            'location to save in. Can be root drive or mapped network drive.
            '-->As it is a constant you can declare it there (and so, use it in the whole module if you want to do other things with it!)
            Private Const attPath As String = "C:"


            Private Sub Application_Startup()
            Dim olApp As Outlook.Application
            Dim objNS As Outlook.NameSpace
            Set olApp = Outlook.Application
            Set objNS = olApp.GetNamespace("MAPI")
            Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
            End Sub

            Private Sub Items_ItemAdd(ByVal item As Object)

            On Error GoTo ErrorHandler
            'Only act if it's a MailItem
            If TypeName(item) = "MailItem" Then
            Dim Msg As Outlook.MailItem
            '-->Use directly the parameter and keep it under wraps using "With", it'll improve efficiency
            With item
            'Change variables to match need. Comment or delete any part unnecessary.
            If (.SenderEmailAddress = "noreply@test.com" _
            Or .Subject = "Smartsheet" _
            Or .Subject = "Defects" _
            ) _
            And .Attachments.Count >= 1 Then


            Dim aAtt As Outlook.Attachment
            '-->Loop through the Attachments' collection
            for each aAtt in item.Attachments
            '-->You can either use aAtt.DisplayName or aAtt.FileName
            '-->You can test aAtt.Size or aAtt.Type

            'save attachment
            aAtt.SaveAsFile attPath & aAtt.DisplayName
            next aAtt

            'mark as read
            .UnRead = False

            Dim olDestFldr As Outlook.MAPIFolder
            Set FldrDest = Session.Folders("Address1").Folders("Inbox").Folders("Test")
            If .Parent.Name = "Test" And .Parent.Parent.Name = "Inbox" Then
            'MailItem is already in destination folder
            Else
            Set Msg = .Move(FldrDest)
            MsgBox Msg.SenderEmailAddress & vbCrLf & Msg.Subject
            'Msg.delete
            End If
            End If
            End With 'item
            End If


            ProgramExit:
            Exit Sub

            ErrorHandler:
            MsgBox Err.number & " - " & Err.Description
            Resume ProgramExit
            End Sub





            share|improve this answer























            • It gives an error - Expected: line number or lable or stement or end of statement and lines starting If ( .SenderEmailAddress = "noreply@test.com" _ to And .Attachments.Count >= 1 Then are in red.
              – Kalenji
              Nov 15 at 12:44










            • @Kalenji : My bad I forgot a _ (changed it in the edit), I wanted to show the parenthesis that is very important when you use And and Or. Because what you posted would probably not behaved as you expected it to ;)
              – R3uK
              Nov 15 at 13:00












            • Works like magic. Just removed the pop up as not needed :) I guess I need my outlook client up and running for this macro to be working? I mean as soon as I log off it won't work?
              – Kalenji
              Nov 15 at 14:31












            • Glad I could help. For this to work, you'll need Outlook up and running and to be logged on (if you're not, it won't find this Set FldrDest = Session.Folders("Address1").Folders("Inbox").Folders("Test")). I'm not so sure, but I'd say that the code will run when you reopen Outlook and send/receive mails. The only thing that the documentations says it's This event does not run when a large number of items are added to the folder at once.
              – R3uK
              Nov 16 at 13:26















            up vote
            4
            down vote



            accepted
            +100










            The MailItem.Move is in fact a function that return the object that has been moved in the new destination.
            The old object is kind of "lost", see how to use it (i've commented the deletion part in the whole code ;) )



            Set Msg = .Move(FldrDest)
            MsgBox Msg.SenderEmailAddress & vbCrLf & Msg.Subject


            Full code with some suggestions for improvement (see '--> comments) :



            Private WithEvents Items As Outlook.Items

            'location to save in. Can be root drive or mapped network drive.
            '-->As it is a constant you can declare it there (and so, use it in the whole module if you want to do other things with it!)
            Private Const attPath As String = "C:"


            Private Sub Application_Startup()
            Dim olApp As Outlook.Application
            Dim objNS As Outlook.NameSpace
            Set olApp = Outlook.Application
            Set objNS = olApp.GetNamespace("MAPI")
            Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
            End Sub

            Private Sub Items_ItemAdd(ByVal item As Object)

            On Error GoTo ErrorHandler
            'Only act if it's a MailItem
            If TypeName(item) = "MailItem" Then
            Dim Msg As Outlook.MailItem
            '-->Use directly the parameter and keep it under wraps using "With", it'll improve efficiency
            With item
            'Change variables to match need. Comment or delete any part unnecessary.
            If (.SenderEmailAddress = "noreply@test.com" _
            Or .Subject = "Smartsheet" _
            Or .Subject = "Defects" _
            ) _
            And .Attachments.Count >= 1 Then


            Dim aAtt As Outlook.Attachment
            '-->Loop through the Attachments' collection
            for each aAtt in item.Attachments
            '-->You can either use aAtt.DisplayName or aAtt.FileName
            '-->You can test aAtt.Size or aAtt.Type

            'save attachment
            aAtt.SaveAsFile attPath & aAtt.DisplayName
            next aAtt

            'mark as read
            .UnRead = False

            Dim olDestFldr As Outlook.MAPIFolder
            Set FldrDest = Session.Folders("Address1").Folders("Inbox").Folders("Test")
            If .Parent.Name = "Test" And .Parent.Parent.Name = "Inbox" Then
            'MailItem is already in destination folder
            Else
            Set Msg = .Move(FldrDest)
            MsgBox Msg.SenderEmailAddress & vbCrLf & Msg.Subject
            'Msg.delete
            End If
            End If
            End With 'item
            End If


            ProgramExit:
            Exit Sub

            ErrorHandler:
            MsgBox Err.number & " - " & Err.Description
            Resume ProgramExit
            End Sub





            share|improve this answer























            • It gives an error - Expected: line number or lable or stement or end of statement and lines starting If ( .SenderEmailAddress = "noreply@test.com" _ to And .Attachments.Count >= 1 Then are in red.
              – Kalenji
              Nov 15 at 12:44










            • @Kalenji : My bad I forgot a _ (changed it in the edit), I wanted to show the parenthesis that is very important when you use And and Or. Because what you posted would probably not behaved as you expected it to ;)
              – R3uK
              Nov 15 at 13:00












            • Works like magic. Just removed the pop up as not needed :) I guess I need my outlook client up and running for this macro to be working? I mean as soon as I log off it won't work?
              – Kalenji
              Nov 15 at 14:31












            • Glad I could help. For this to work, you'll need Outlook up and running and to be logged on (if you're not, it won't find this Set FldrDest = Session.Folders("Address1").Folders("Inbox").Folders("Test")). I'm not so sure, but I'd say that the code will run when you reopen Outlook and send/receive mails. The only thing that the documentations says it's This event does not run when a large number of items are added to the folder at once.
              – R3uK
              Nov 16 at 13:26













            up vote
            4
            down vote



            accepted
            +100







            up vote
            4
            down vote



            accepted
            +100




            +100




            The MailItem.Move is in fact a function that return the object that has been moved in the new destination.
            The old object is kind of "lost", see how to use it (i've commented the deletion part in the whole code ;) )



            Set Msg = .Move(FldrDest)
            MsgBox Msg.SenderEmailAddress & vbCrLf & Msg.Subject


            Full code with some suggestions for improvement (see '--> comments) :



            Private WithEvents Items As Outlook.Items

            'location to save in. Can be root drive or mapped network drive.
            '-->As it is a constant you can declare it there (and so, use it in the whole module if you want to do other things with it!)
            Private Const attPath As String = "C:"


            Private Sub Application_Startup()
            Dim olApp As Outlook.Application
            Dim objNS As Outlook.NameSpace
            Set olApp = Outlook.Application
            Set objNS = olApp.GetNamespace("MAPI")
            Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
            End Sub

            Private Sub Items_ItemAdd(ByVal item As Object)

            On Error GoTo ErrorHandler
            'Only act if it's a MailItem
            If TypeName(item) = "MailItem" Then
            Dim Msg As Outlook.MailItem
            '-->Use directly the parameter and keep it under wraps using "With", it'll improve efficiency
            With item
            'Change variables to match need. Comment or delete any part unnecessary.
            If (.SenderEmailAddress = "noreply@test.com" _
            Or .Subject = "Smartsheet" _
            Or .Subject = "Defects" _
            ) _
            And .Attachments.Count >= 1 Then


            Dim aAtt As Outlook.Attachment
            '-->Loop through the Attachments' collection
            for each aAtt in item.Attachments
            '-->You can either use aAtt.DisplayName or aAtt.FileName
            '-->You can test aAtt.Size or aAtt.Type

            'save attachment
            aAtt.SaveAsFile attPath & aAtt.DisplayName
            next aAtt

            'mark as read
            .UnRead = False

            Dim olDestFldr As Outlook.MAPIFolder
            Set FldrDest = Session.Folders("Address1").Folders("Inbox").Folders("Test")
            If .Parent.Name = "Test" And .Parent.Parent.Name = "Inbox" Then
            'MailItem is already in destination folder
            Else
            Set Msg = .Move(FldrDest)
            MsgBox Msg.SenderEmailAddress & vbCrLf & Msg.Subject
            'Msg.delete
            End If
            End If
            End With 'item
            End If


            ProgramExit:
            Exit Sub

            ErrorHandler:
            MsgBox Err.number & " - " & Err.Description
            Resume ProgramExit
            End Sub





            share|improve this answer














            The MailItem.Move is in fact a function that return the object that has been moved in the new destination.
            The old object is kind of "lost", see how to use it (i've commented the deletion part in the whole code ;) )



            Set Msg = .Move(FldrDest)
            MsgBox Msg.SenderEmailAddress & vbCrLf & Msg.Subject


            Full code with some suggestions for improvement (see '--> comments) :



            Private WithEvents Items As Outlook.Items

            'location to save in. Can be root drive or mapped network drive.
            '-->As it is a constant you can declare it there (and so, use it in the whole module if you want to do other things with it!)
            Private Const attPath As String = "C:"


            Private Sub Application_Startup()
            Dim olApp As Outlook.Application
            Dim objNS As Outlook.NameSpace
            Set olApp = Outlook.Application
            Set objNS = olApp.GetNamespace("MAPI")
            Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
            End Sub

            Private Sub Items_ItemAdd(ByVal item As Object)

            On Error GoTo ErrorHandler
            'Only act if it's a MailItem
            If TypeName(item) = "MailItem" Then
            Dim Msg As Outlook.MailItem
            '-->Use directly the parameter and keep it under wraps using "With", it'll improve efficiency
            With item
            'Change variables to match need. Comment or delete any part unnecessary.
            If (.SenderEmailAddress = "noreply@test.com" _
            Or .Subject = "Smartsheet" _
            Or .Subject = "Defects" _
            ) _
            And .Attachments.Count >= 1 Then


            Dim aAtt As Outlook.Attachment
            '-->Loop through the Attachments' collection
            for each aAtt in item.Attachments
            '-->You can either use aAtt.DisplayName or aAtt.FileName
            '-->You can test aAtt.Size or aAtt.Type

            'save attachment
            aAtt.SaveAsFile attPath & aAtt.DisplayName
            next aAtt

            'mark as read
            .UnRead = False

            Dim olDestFldr As Outlook.MAPIFolder
            Set FldrDest = Session.Folders("Address1").Folders("Inbox").Folders("Test")
            If .Parent.Name = "Test" And .Parent.Parent.Name = "Inbox" Then
            'MailItem is already in destination folder
            Else
            Set Msg = .Move(FldrDest)
            MsgBox Msg.SenderEmailAddress & vbCrLf & Msg.Subject
            'Msg.delete
            End If
            End If
            End With 'item
            End If


            ProgramExit:
            Exit Sub

            ErrorHandler:
            MsgBox Err.number & " - " & Err.Description
            Resume ProgramExit
            End Sub






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 15 at 12:58

























            answered Nov 15 at 10:17









            R3uK

            12.5k42859




            12.5k42859












            • It gives an error - Expected: line number or lable or stement or end of statement and lines starting If ( .SenderEmailAddress = "noreply@test.com" _ to And .Attachments.Count >= 1 Then are in red.
              – Kalenji
              Nov 15 at 12:44










            • @Kalenji : My bad I forgot a _ (changed it in the edit), I wanted to show the parenthesis that is very important when you use And and Or. Because what you posted would probably not behaved as you expected it to ;)
              – R3uK
              Nov 15 at 13:00












            • Works like magic. Just removed the pop up as not needed :) I guess I need my outlook client up and running for this macro to be working? I mean as soon as I log off it won't work?
              – Kalenji
              Nov 15 at 14:31












            • Glad I could help. For this to work, you'll need Outlook up and running and to be logged on (if you're not, it won't find this Set FldrDest = Session.Folders("Address1").Folders("Inbox").Folders("Test")). I'm not so sure, but I'd say that the code will run when you reopen Outlook and send/receive mails. The only thing that the documentations says it's This event does not run when a large number of items are added to the folder at once.
              – R3uK
              Nov 16 at 13:26


















            • It gives an error - Expected: line number or lable or stement or end of statement and lines starting If ( .SenderEmailAddress = "noreply@test.com" _ to And .Attachments.Count >= 1 Then are in red.
              – Kalenji
              Nov 15 at 12:44










            • @Kalenji : My bad I forgot a _ (changed it in the edit), I wanted to show the parenthesis that is very important when you use And and Or. Because what you posted would probably not behaved as you expected it to ;)
              – R3uK
              Nov 15 at 13:00












            • Works like magic. Just removed the pop up as not needed :) I guess I need my outlook client up and running for this macro to be working? I mean as soon as I log off it won't work?
              – Kalenji
              Nov 15 at 14:31












            • Glad I could help. For this to work, you'll need Outlook up and running and to be logged on (if you're not, it won't find this Set FldrDest = Session.Folders("Address1").Folders("Inbox").Folders("Test")). I'm not so sure, but I'd say that the code will run when you reopen Outlook and send/receive mails. The only thing that the documentations says it's This event does not run when a large number of items are added to the folder at once.
              – R3uK
              Nov 16 at 13:26
















            It gives an error - Expected: line number or lable or stement or end of statement and lines starting If ( .SenderEmailAddress = "noreply@test.com" _ to And .Attachments.Count >= 1 Then are in red.
            – Kalenji
            Nov 15 at 12:44




            It gives an error - Expected: line number or lable or stement or end of statement and lines starting If ( .SenderEmailAddress = "noreply@test.com" _ to And .Attachments.Count >= 1 Then are in red.
            – Kalenji
            Nov 15 at 12:44












            @Kalenji : My bad I forgot a _ (changed it in the edit), I wanted to show the parenthesis that is very important when you use And and Or. Because what you posted would probably not behaved as you expected it to ;)
            – R3uK
            Nov 15 at 13:00






            @Kalenji : My bad I forgot a _ (changed it in the edit), I wanted to show the parenthesis that is very important when you use And and Or. Because what you posted would probably not behaved as you expected it to ;)
            – R3uK
            Nov 15 at 13:00














            Works like magic. Just removed the pop up as not needed :) I guess I need my outlook client up and running for this macro to be working? I mean as soon as I log off it won't work?
            – Kalenji
            Nov 15 at 14:31






            Works like magic. Just removed the pop up as not needed :) I guess I need my outlook client up and running for this macro to be working? I mean as soon as I log off it won't work?
            – Kalenji
            Nov 15 at 14:31














            Glad I could help. For this to work, you'll need Outlook up and running and to be logged on (if you're not, it won't find this Set FldrDest = Session.Folders("Address1").Folders("Inbox").Folders("Test")). I'm not so sure, but I'd say that the code will run when you reopen Outlook and send/receive mails. The only thing that the documentations says it's This event does not run when a large number of items are added to the folder at once.
            – R3uK
            Nov 16 at 13:26




            Glad I could help. For this to work, you'll need Outlook up and running and to be logged on (if you're not, it won't find this Set FldrDest = Session.Folders("Address1").Folders("Inbox").Folders("Test")). I'm not so sure, but I'd say that the code will run when you reopen Outlook and send/receive mails. The only thing that the documentations says it's This event does not run when a large number of items are added to the folder at once.
            – R3uK
            Nov 16 at 13:26












            up vote
            0
            down vote













            Easier than I thought. Just added a loop with Msg.Delete.






            share|improve this answer

























              up vote
              0
              down vote













              Easier than I thought. Just added a loop with Msg.Delete.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                Easier than I thought. Just added a loop with Msg.Delete.






                share|improve this answer












                Easier than I thought. Just added a loop with Msg.Delete.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 at 12:48









                Kalenji

                214214




                214214






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224268%2foutlook-delete-email-after-is-saved%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Schultheiß

                    Verwaltungsgliederung Dänemarks

                    Liste der Kulturdenkmale in Wilsdruff