Consulting

Results 1 to 6 of 6

Thread: find and remove emails based on BCC input

  1. #1
    VBAX Newbie
    Joined
    Oct 2018
    Posts
    3
    Location

    find and remove emails based on BCC input

    Hello,

    Can you help me generate a script with these criteria:
    - search folder = Sent items
    - FROM: "*myself*"
    - BCC = "*someone*"
    if it meets these criteria, move the email to the 'Deleted Items' folder


    Outlook Rules does not provide what I am looking for, as I cannot set the BCC criteria, so hopefully a VBA might be achievable....

    Kind regards,
    Marijke

  2. #2
    Unless this is a shared mailbox or you have more than one account using the mailbox then all the items in the default sent folder are from you, in which case you can remove the references to strAddress in the following macro i.e. Change
    If olItem.SenderEmailAddress = strAddress And _
               olItem.BCC = strBCC Then
    to
    If olItem.BCC = strBCC Then
    Sub DeleteSent()
    Dim olItems As Outlook.Items
    Dim olItem As Outlook.MailItem
    Dim i As Long
    Dim strAddress As String
    Dim strBCC As String
    
    strAddress = "me@myaddress.com"    'Your e-mail address
        strBCC = "someone@somewhere.com" 'The BCC address
    
        Set olItems = Session.GetDefaultFolder(olFolderSentMail).Items
        olItems.Sort "[Received]", True
        For i = olItems.Count To 1 Step -1
            Set olItem = olItems(i)
            If olItem.SenderEmailAddress = strAddress And _
               olItem.BCC = strBCC Then
                olItem.Delete
            End If
        Next i
    Cleanup:
        Set olItems = Nothing
        Set olItem = Nothing
    lbl_Exit:
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word)
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Newbie
    Joined
    Oct 2018
    Posts
    3
    Location
    This is indeed a shared mailbox

    I have added to code, but an error appears on this step:

    Set olItem = olItems(i)


    The error message indicated:
    Run-time error '13': Type mismatch

  4. #4
    It seems that you may have items that are not mailitems (such as meeting requests) in the folder which would cause the error. You could change the declaration type of olItem to 'Object' or you can add some error trapping to check only mail items e.g. as shown below.

    If the BCC in question is shown as a name rather than an address, put the name (exactly as it appears) as the value of strBCC.

    Sub DeleteSent()
    Dim olItems As Outlook.Items
    Dim olItem As Outlook.MailItem
    Dim i As Long
    Dim strAddress As String
    Dim strBCC As String
    
    strAddress = "me@myaddress.com"    'Your e-mail address
    strBCC = "someone@somewhere.com"    'The BCC address
    
        Set olItems = Session.GetDefaultFolder(olFolderSentMail).Items
        olItems.Sort "[Received]", True
        For i = olItems.Count To 1 Step -1
            If TypeName(olItems(i)) = "MailItem" Then
                Set olItem = olItems(i)
                If Not olItem.BCC = "" Then
                    If olItem.SenderEmailAddress = strAddress And _
                       InStr(1, olItem.BCC, strBCC) > 0 Then
                        olItem.Delete
                    End If
                End If
            End If
        Next i
    Cleanup:
        Set olItems = Nothing
        Set olItem = Nothing
    lbl_Exit:
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word)
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    VBAX Newbie
    Joined
    Oct 2018
    Posts
    3
    Location
    No errors, but Outlook crashed on me.
    I'm guessing I simply have too many emails that are being reviewed....

    I'm sure there must be a method to select only emails with a certain 'age', so I can work through the sent mails in batches rather than in 1 go. (for example the last 30 days)
    Is this indeed possible and would you be able to provide me some directions there?


    also, will the script move the emails to the deleted items folder, or will it permanently delete the emails?

  6. #6
    Add the line
    DoEvents
    immediately before the line
    Next i
    which should stop the process appearing to hang.
    The messages are moved to the deleted folder.
    Graham Mayor - MS MVP (Word)
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •