Creating macro that will sort emails into folders based on text in body

  • Thread starter Thread starter mugginns
  • Start date Start date
M

mugginns

I'm trying to find a good spot to find info on how to create a macro
that will basically look through the body of an email and sort it into
a folder based on finding a string, ie:

hospid= 102341

It would then be sent to a folder, ie:

hosp11

I'm a little new at programming for Outlook and was wondering if anyone
can help me out. I had looked at a post on outlookcode.com that
basically creates your own rules but i could not get this to work at
all, and I'm not sure if it was searching the body for the information
or not.

michael
 
Here's some sample code that can move an e-mail to the folder you choose if
it contains the specified text in the message body:

Dim objMail As Outlook.MailItem

'Look for open item window
If ActiveInspector Is Nothing Then Exit Sub
'Look for open e-mails only
If ActiveInspector.CurrentItem.Class <> olMail Then Exit Sub
Set objMail = ActiveInspector.CurrentItem

If InStr(objMail.Body, "Look for this text") > 0 Then
'will move to the folder you choose
Call objMail.Move(Application.GetNamespace("MAPI").PickFolder)
'You can replace the above Move argument with a MAPIFolder object
directly instead of choosing one
'Use NameSpace.GetDefaultFolder to easily get a reference to a
default Outlook folder
If Err.Number <> 0 Then
'A folder wasn't chosen, or a non-email folder was chosen
End If
End If
 
Am 29 Sep 2006 12:58:37 -0700 schrieb mugginns:

This basic loops through the Inbox. The loop counts backwards because I
guess that you want to move found items off that list.

Dim Item as Object
Dim Folder as Outlook.MapiFolder
Dim Target as Outlook.Folder
Dim i as Long
Dim Find as string

Set Folder=Application.GetDefaultFolder(olFolderinbox)

Find="hospid= 102431"

' If hosp11 is a subfolder of the Inbox
Set Target = Folder.Folders("hosp11")

For i=Folder.Items.Count To 1 Step-1
Set Item=Folder.Items(i)
If Instr(1, Item.Body, Find, vbTextCompare) Then
Item.Move Target
Endif
Next
 
Thanks for the help gentlemen. I'm looking at the code, and I'm looking
specifically at:

'You can replace the above Move argument with a MAPIFolder object
directly instead of choosing one
'Use NameSpace.GetDefaultFolder to easily get a reference to a
default Outlook folder

Basically I want to send the file to a folder called "blank1" that I
have created in my Inbox. I have looked and looked but I cannot
understand how to use this. Can you help explain ?
Michael
 
Thanks for the help gentlemen. I'm looking at the code, and I'm looking
specifically at:

'You can replace the above Move argument with a MAPIFolder object
directly instead of choosing one
'Use NameSpace.GetDefaultFolder to easily get a reference to a
default Outlook folder

Basically I want to send the file to a folder called "blank1" that I
have created in my Inbox. I have looked and looked but I cannot
understand how to use this. Can you help explain ?
Michael

Actually, I got mr. bauer's solution to work. The only issue is I
believe I am not using the default inbox - I am using an added account
to my outlook program. How would I use this "mailbox" instead of my
personal one ?

Michael
 
Am 2 Oct 2006 07:09:34 -0700 schrieb (e-mail address removed):

For instance, you can walk through the folders hierarchy: Via a folder´s
Parent property you get to its parent folder, via its Folders("Name")
property to one if a folder´s subfolder. You simply need to know the
folder´s path.
 
Back
Top