none
How to copy files from Outlook to C:\ , but restrict subject and body? RRS feed

  • Question

  • The VBA below can copy files from Outlook to C:\

    Sub wEaddressAtC()
    
    If ActiveCell <> "From sender email" Then Exit Sub
        Dim objNS As Outlook.Namespace
        Dim objFolder As Outlook.MAPIFolder
        Dim path As String
        Set objNS = GetNamespace("MAPI")
        Set objFolder = objNS.Folders("Ivan emailabc-Sundries for others")
        Set objFolder = objFolder.Folders("Get Email")
            
            For Each Item In objFolder.Items
                
                If TypeName(Item) = "MailItem" Then
                If Item.Sender <> "K K Lo" And (Not InStr(Item.Sender, "Mail") > 0 And Not InStr(Item.Sender, "MAIL") > 0 And Not InStr(Item.Sender, "post") > 0) Then
                If Item.Subject <> Not InStr(Item.Subject, "size") > 0 Then
                If Item.Body <> Not InStr(Item.Body, "regret") > 0 Then
                        
                        With CreateObject("VBSCRIPT.REGEXP")
                            .Global = True
                            .Pattern = "[\\/:*?|<>]"
                            tmpSubject = .Replace(Item.Subject, "")
                        End With
                    path = "C:\Users\DK-01\Desktop\Renaming\Quotation from SC\" & tmpSubject & ".msg"
                    Item.SaveAs (path)
                  
                 End If
                 End If
                 End If
                 End If
            Next
    End Sub
    
    
    How to correct the code below so that the file will not be copied if 
    there is "size" in subject;
    there is "regret" in body;
    If Item.Subject <> Not InStr(Item.Subject, "size") > 0 Then
    If Item.Body <> Not InStr(Item.Body, "regret") > 0 Then
    


    Wednesday, November 29, 2017 10:00 AM

Answers

  • Hi london7871,

    Please try to refer to below code.

    For Each Item In objFolder.Items
                
                If TypeName(Item) = "MailItem" Then
                If Item.Sender <> "K K Lo" And (Not InStr(Item.Sender, "Mail") > 0 And Not InStr(Item.Sender, "MAIL") > 0 And Not InStr(Item.Sender, "post") > 0) Then
                If Not InStr(Item.Subject, "size") > 0 Then
                If Not InStr(Item.Body, "regret") > 0 Then
                        
                        With CreateObject("VBSCRIPT.REGEXP")
                            .Global = True
                            .Pattern = "[\\/:*?|<>]"
                            tmpSubject = .Replace(Item.Subject, "")
                        End With
                    path = "C:\Users\DK-01\Desktop\Renaming\Quotation from SC\" & tmpSubject & ".msg"
                    Item.SaveAs (path)
                  
                 End If
                 End If
                 End If
                 End If
            Next

    Best Regards,

    Terry


    MSDN Community Support<br/> Please remember to click &quot;Mark as Answer&quot; the responses that resolved your issue, and to click &quot;Unmark as Answer&quot; if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact <a href="mailto:MSDNFSF@microsoft.com">MSDNFSF@microsoft.com</a>.


    • Edited by Terry Xu - MSFT Thursday, November 30, 2017 1:19 AM
    • Marked as answer by london7871 Thursday, November 30, 2017 1:55 AM
    Thursday, November 30, 2017 1:19 AM

All replies

  • Hi london7871,

    Please try to refer to below code.

    For Each Item In objFolder.Items
                
                If TypeName(Item) = "MailItem" Then
                If Item.Sender <> "K K Lo" And (Not InStr(Item.Sender, "Mail") > 0 And Not InStr(Item.Sender, "MAIL") > 0 And Not InStr(Item.Sender, "post") > 0) Then
                If Not InStr(Item.Subject, "size") > 0 Then
                If Not InStr(Item.Body, "regret") > 0 Then
                        
                        With CreateObject("VBSCRIPT.REGEXP")
                            .Global = True
                            .Pattern = "[\\/:*?|<>]"
                            tmpSubject = .Replace(Item.Subject, "")
                        End With
                    path = "C:\Users\DK-01\Desktop\Renaming\Quotation from SC\" & tmpSubject & ".msg"
                    Item.SaveAs (path)
                  
                 End If
                 End If
                 End If
                 End If
            Next

    Best Regards,

    Terry


    MSDN Community Support<br/> Please remember to click &quot;Mark as Answer&quot; the responses that resolved your issue, and to click &quot;Unmark as Answer&quot; if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact <a href="mailto:MSDNFSF@microsoft.com">MSDNFSF@microsoft.com</a>.


    • Edited by Terry Xu - MSFT Thursday, November 30, 2017 1:19 AM
    • Marked as answer by london7871 Thursday, November 30, 2017 1:55 AM
    Thursday, November 30, 2017 1:19 AM
  • Thanks Terry, it's work.
    Thursday, November 30, 2017 1:55 AM