verify and delete mailitem

J

Joanne

Daily I receive approx 100 emails. I have to open them individually
and verify that this line is present

Description: Successful

If it is present, I delete the email.

I think I should be able to do this programmatically, looping thru all
the emails in the collection and maybe doing a ctrl/find to locate the
text string and if it is present, close (maybe I don't have to close
it first?) and then delete the email. If the string is not present,
close the email and goto the next one without deleting. I am a bit
familiar with VBA in Word but rather outside my league here in
Outlook.

Here is my best guess on how to approach this and at least get
started:

Public Sub Deletions()
Dim obj As Object
Dim Items As Outlook.MailItem

Set Items = Application.Session.GetDefaultFolder(olFolderInbox).Items
For Each obj In Items
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Description: Successful"

here I think I need an if then statement saying if the
string is present, delete the item, if not close the item
only I can't think through it enough to figure out how to
tell the program that the text is indeed there (or not!)

End With
Next
End Sub

Would it be best to put the code to do this in the
'ThisOutlookSession' Module? I've never used the module and am not at
all sure when it should be used as verses just creating a new module.

Could someone please help me out with this. I sure appreciate your
time and expertise. I have spent the morning reading through this
group and that is how I came up with this 'best guess' of mine on how
to do this task. This newsgroup is chuck full of great info. Good job
and thanks a million. I love that you give us web sites to visit and
try to find the answers to our problems. While I wait for a response
to my inquiry I am going to go to Sue Mosher's site and check it out.
Joanne
 
S

Sue Mosher [MVP-Outlook]

Selection.Find is Word's method for searching inside text. Inside an Outlook item, you would use the Instr() function to test for a match inside the MailItem.Body

If Instr(obj.Body, "Description: Successful" > 0 Then
obj.Delete
End If

The tricky part is how your construct the loop. Because the index changes each time you delete an item, you can't use a For Each ... Next loop, but can instead us a countdown loop:

count = Items.Count
For i = count to 1 Step -1
Set obj = Items(i)
If Instr(obj.Body, "Description: Successful" > 0 Then
obj.Delete
End If
Next

You can put the code in ThisOutlookSession or create a new module, which would make it easier to export for backup.
--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
M

Michael Bauer

Am Sat, 10 Jun 2006 18:17:30 GMT schrieb Joanne:

Joanne, ThisOutlookSession is a good place for the code. For being able to
delete items you need a backward loop. Outlook´s Selection object doesn´t
know a Find method, instead you can use the Instr function:

Dim i as Long
....
For i=Items.Count To 1 Step-1
Set obj=Items(i)
If InStr(1, obj.Body, "Description: Successful", vbTextCompare) Then
obj.Delete
Endif
Next

Your loop handles all the items in the folder - again and again. If you have
another mechanism to clean up that folder then that´s ok. If instead the
folder´s content grows more and more then you´ll probably wish a faster way
to loop through the unhandled items only.
 
J

Joanne

Michael
I was cracking my brain on this all day yesterday and here you make it
just a short and sweet few lines. Excellent!! I thank you for your
time.
I spent considerable time at outlookcode.com yesterday and found
something called parsing text that looks at these kind of emails where
there is a label: data format and tried writing it up for my own use
but failed at it.
I am gratefully going to use your loop to do this particular job, but
I would like to know if it would be feasible to use the parsing text
in a case like this - I know there are many ways to skin the cat in
programming, I simply am woefully short on the knowledge to accomplish
it.
But I do love the hunt - especially when you mvps give us the help we
need when we get bottled up. Just reading your groups I pickup so many
hints and points in the right direction. Better than a classroom any
day!
Thanks again
Joanne
 
M

Michael Bauer

Am Sun, 11 Jun 2006 12:12:24 GMT schrieb Joanne:

Thanks, Joanne. But it´s no magic, for most of us developing software is the
job for many years.

"Parsing text" gives a lot of results. If you do have a link maybe I can
tell more about that function.

But why don´t you like the InStr function?
 
J

Joanne

Michael
(Sorry about the double post - I accidently started a new thread for
this and decided to send it to the original thread.)

I just thought I should learn more about text parsing for future
projects since I came across it. Actually I wish I had more time to
noodle around with programming MSOffice instead of working in it.
Programming appeals to my love of puzzles.

I am having a problem with my little macro you helped me with.
Here it is in full:


Public Sub Deletions()
Dim obj As Object
Dim Items As Outlook.MailItem

Set Items = Application.Session.GetDefaultFolder(olFolderInbox).Items

Dim i As Long
For i = Items.Count To 1 Step -1
Set obj = Items(i)
If InStr(1, obj.Body, "Description: Successful", vbTextCompare)
Then
obj.Delete
End If
Next

End Sub

I suspect that I don't have the beginning of the procedure correct.

The error I am getting is on the set items = line
The error is Run time error #13 Type Mismatch

Do I even need the set items statement here?
I am in the inbox when I run the macro, so maybe it isn't necessary
to tell the macro where to find the items?

Object, Items and long have all been dimmed - they are the only
variables in the procedure, so I know that is okay.

You created the loop so I know that's good to go.

Thanks for your help again - Someday maybe I can get these little
things done without needing my hand held so much ;-)
Joanne
 
M

Michael Bauer

Am Mon, 12 Jun 2006 13:08:44 GMT schrieb Joanne:

Joanne, that error tells you there´s a mismatch between both the data types
of the variable and the property you´re trying to pass to it. So please dim
Items As Outlook.Items instead of MailItem.

If you´re in doubt you can check it simply yourself: You set the variable to
the Items object in the Set Items = ... line. You can point the mouse over
..Items and right click "QuickInfo". That´ll display a tooltip which tells
you what kind of data type it is.

If you´re in the correct folder for sure then you could skip the
GetDefaultFolder function and use Application.ActiveExplorer.CurrentFolder
instead. But I´d suggest to use GetdefaultFolder, that makes your code more
independant.

You´re welcome.
 
J

Joanne

Sue
Thanks so much for your input on my little project. I always have
great appreciation for the time and expertise you mvps donate to us
wannabes.
Your code is so precise - I love it.
Also, have spent much time on your outlookcode.com - will spend much
more also - so much to learn and absorb from there.
Keep up the great work

I'm a big fan of this newsgroup
Joanne
 
J

Joanne

Thanks for the info Michael - the macro now works really sweet.
Didn't know about the "quickinfo" so feel like I got extra instruction
on this one. Will use it to make life easier next time I venture into
Outlook programming.
Joanne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top