-
Jul 10th, 2018, 10:43 AM
#1
Thread Starter
Junior Member
permanently deleting emails from inbox with the subject starts with "xxxxx"
Hi Friends,
i need a VBA to delete emails permanently(shift+delete) with the subject line starts with "Automatic reply: xxxx" actually in place of xxxx, it may be any name, but my task is to delete the emails starts with the subject line "Automatic reply: "
Please help me friends.
Thanks in Advance.
-
Jul 10th, 2018, 11:16 AM
#2
Re: permanently deleting emails from inbox with the subject starts with "xxxxx"
Try something like this. You'd need to remove the "cnt" part, and the stops. I put that in there so that it wouldn't run too long in my test scenario.
Code:
Sub vbfDelete()
Dim objNS As Outlook.NameSpace: Set objNS = GetNamespace("MAPI")
Dim olFolder As Outlook.MAPIFolder
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
Dim Item As Object
Dim cnt As Integer
Dim str As String
Dim char4 As String
char4 = InputBox("Enter string to delete")
If Len(char4) <> 4 Then
MsgBox "Must enter 4 characters. Exiting..."
Exit Sub
End If
For Each Item In olFolder.Items
If TypeOf Item Is Outlook.MailItem Then
Dim oMail As Outlook.MailItem: Set oMail = Item
str = Item.Subject
If LCase(Left(str, 15)) = "automatic reply" Then
Stop
If LCase(Mid(str, 18, 4)) = char4 Then
cnt = cnt + 1
If cnt > 2 Then
Stop
End If
Item.Delete
End If
End If
End If
Next
End Sub
-
Jul 10th, 2018, 04:37 PM
#3
Re: permanently deleting emails from inbox with the subject starts with "xxxxx"
item.delete only moves to deleted items, i am not sure how to permanently delete
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jul 10th, 2018, 04:42 PM
#4
Re: permanently deleting emails from inbox with the subject starts with "xxxxx"
The loop can be run a 2nd time with this line:
Code:
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
being changed to this:
Code:
Set olFolder = objNS.GetDefaultFolder(olFolderDeletedItems)
Probably not the most elegant, but it appears to work.
-
Jul 10th, 2018, 04:39 PM
#5
Re: permanently deleting emails from inbox with the subject starts with "xxxxx"
Yes, good point, I missed that. I'm wondering if you can then just delete from the "Deleted" folder as well?
I'll check into that.
-
Jul 11th, 2018, 04:10 AM
#6
Re: permanently deleting emails from inbox with the subject starts with "xxxxx"
Probably not the most elegant, but it appears to work.
i figured that would work, or get the message id the first time then find it in the deleted items
sue mosher has posted an alternative, using CDO, but i have no idea if it is actually better
see http://www.outlookcode.com/codedetail.aspx?id=41
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jul 11th, 2018, 05:43 AM
#7
Re: permanently deleting emails from inbox with the subject starts with "xxxxx"
Hi,
to delete the contents in the "Paperbin Folder" in Outlook I use this...
Code:
Function delPaperBin()
Dim OL As Outlook.Application ' Outlook instance
Dim NS As Outlook.NameSpace ' - NameSpace
Dim OC As Outlook.MAPIFolder ' - Contacts
Dim MI As Outlook.MailItem
Dim J1 As Long
On Error Resume Next
Set OL = New Outlook.Application
If Not OL Is Nothing Then Set NS = OL.GetNamespace("MAPI")
If Not NS Is Nothing Then Set OC = NS.GetDefaultFolder(olFolderDeletedItems)
For J1 = OC.Items.Count To 1 Step -1
OC.Items(J1).Delete
Next
Set OC = Nothing
Set NS = Nothing
Set OL = Nothing
End Function
hope it helps
regards
Chris
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Jul 12th, 2018, 09:40 AM
#8
Thread Starter
Junior Member
Re: permanently deleting emails from inbox with the subject starts with "xxxxx"
Thank you all for providing the codes & spending your valuable time,
i was out of town for few days, sorry for the late reply, i will check the codes now & revert.
-
Jul 12th, 2018, 10:23 AM
#9
Thread Starter
Junior Member
Re: permanently deleting emails from inbox with the subject starts with "xxxxx"
Hi friends,
i got the below code from online, i just used to called delPaperBin code given in chris.
but public sub is not working, could some one please help me on this .
Code:
Public Sub process_email(itm As Outlook.MailItem)
Dim InStr As MailItem
If InStr(itm.Subject, "Automatic reply") > 0 Then
itm.UnRead = False
itm.Save
itm.Delete
End
End If
Call delPaperBin
End Sub
-
Jul 12th, 2018, 10:25 AM
#10
Thread Starter
Junior Member
Re: permanently deleting emails from inbox with the subject starts with "xxxxx"
its OK for me, if it deletes all the deleted items from the bin, chris code delPaperBin is perfectly works for that one.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|