A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Outlook and VBA
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Macro to Write Rules



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old June 23rd 09, 06:01 PM posted to microsoft.public.outlook.program_vba
Stratuser
external usenet poster
 
Posts: 4
Default Macro to Write Rules

My firm's server crashed and wiped out all my rules for moving e-mails to
designated folders. Writing all the rules all over again will take several
hours, and I'm thinking it would be faster to write some VBA code to do it.

What is the VBA code that creates a rule that moves any e-mail from any
sender at FIRM A to a folder called "FIRM A"? Also, what is the VBA code
that creates a rule for moving any e-mail that I send to FIRM A to the folder
called "FIRM A"?

Thanks for your help.
Ads
  #2  
Old June 23rd 09, 07:22 PM posted to microsoft.public.outlook.program_vba
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default Macro to Write Rules

Is this Outlook 2007? If not there is no VBA that creates a rule. You can do
what a rule does using pure code, but then you would handle the NewMailEx()
event or ItemAdd on the Inbox Items collection, and check each incoming item
for whatever conditions you want and take whatever actions you want entirely
in code.

The sent rule would require handling ItemAdd on the Items collection of the
Sent Items folder.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Stratuser" wrote in message
...
My firm's server crashed and wiped out all my rules for moving e-mails to
designated folders. Writing all the rules all over again will take
several
hours, and I'm thinking it would be faster to write some VBA code to do
it.

What is the VBA code that creates a rule that moves any e-mail from any
sender at FIRM A to a folder called "FIRM A"? Also, what is the VBA code
that creates a rule for moving any e-mail that I send to FIRM A to the
folder
called "FIRM A"?

Thanks for your help.


  #3  
Old June 23rd 09, 07:34 PM posted to microsoft.public.outlook.program_vba
Stratuser
external usenet poster
 
Posts: 4
Default Macro to Write Rules

Unfortunately, I'm a newbie to Outlook VBA. What would be the code for
moving any e-mail to or from Firm_A to the folder named Firm_A (under the
Inbox)? If you can get me this far, I guess I can just alter the code.



"Ken Slovak - [MVP - Outlook]" wrote:

Is this Outlook 2007? If not there is no VBA that creates a rule. You can do
what a rule does using pure code, but then you would handle the NewMailEx()
event or ItemAdd on the Inbox Items collection, and check each incoming item
for whatever conditions you want and take whatever actions you want entirely
in code.

The sent rule would require handling ItemAdd on the Items collection of the
Sent Items folder.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Stratuser" wrote in message
...
My firm's server crashed and wiped out all my rules for moving e-mails to
designated folders. Writing all the rules all over again will take
several
hours, and I'm thinking it would be faster to write some VBA code to do
it.

What is the VBA code that creates a rule that moves any e-mail from any
sender at FIRM A to a folder called "FIRM A"? Also, what is the VBA code
that creates a rule for moving any e-mail that I send to FIRM A to the
folder
called "FIRM A"?

Thanks for your help.



  #4  
Old June 23rd 09, 10:56 PM posted to microsoft.public.outlook.program_vba
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default Macro to Write Rules

Something like this, placed in the ThisOutlookSession class module would
handle incoming emails, but you'd need to know to look in all those
subfolders of Inbox. It takes the part of the sender email address between
the "@" and the first "." as the company name:

Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
Dim oNS As Outlook.NameSpace
Dim oMail As Outlook.MailItem
Dim oMoved As Outlook.MailItem
Dim oTarget As Outlook.MAPIFolder
Dim oInbox As Outlook.MAPIFolder
Dim obj As Object

Dim sIDs() As String
Dim sRecip As String

Dim i As Long
Dim lPos As Long

sIDs = Split(EntryIDCollection, ",")

Set oNS = Application.GetNamespace("MAPI")
Set oInbox = oNS.GetDefaultFolder(olFolderInbox)

For i = LBound(sIDs) To UBound(sIDs)
Set obj = oNS.GetItemFromID(sIDs(i))
If obj.Class = olMail Then
Set oMail = obj
Set obj = Nothing

sRecip = oMail.SenderEmailAddress
lPos = InStr(1, sRecip, "@")
' strip out everything up to and including "@"
sRecip = Right(sRecip, Len(sRecip) - lPos)

lPos = InStr(1, sRecip, ".")
' strip everything from "." on
sRecip = Left(sRecip, lPos - 1)

Set oTarget = oInbox.Folders.Item(sRecip)
If oTarget Is Nothing Then
oInbox.Folders.Add (sRecip)
End If

Set oMoved = oMail.Move(oTarget)
End If
Next
End Sub

For the Sent Items folder handling you'd need something similar, in an
ItemAdd() event handler. You can find examples of ItemAdd() handlers at
www.outlookcode.com, one I can think of offhand for Inbox is "zaphtml", you
can search on that for an example.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Stratuser" wrote in message
news
Unfortunately, I'm a newbie to Outlook VBA. What would be the code for
moving any e-mail to or from Firm_A to the folder named Firm_A (under the
Inbox)? If you can get me this far, I guess I can just alter the code.


  #5  
Old June 23rd 09, 11:40 PM posted to microsoft.public.outlook.program_vba
Stratuser
external usenet poster
 
Posts: 4
Default Macro to Write Rules

Thanks very much. I pasted this code in, and then I got an internal e-mail.
Apparently the e-mail address for internal senders here is a long
unrecognizable string that starts with a "/". I wrote an "IF" statement to
screen out those e-mails, assuming that external e-mails don't have the same
pattern.

Your code seems to create a new folder for each new external e-mail sender.
I think I'll want to specify the folder names in advance, because there are
about 30 specific firms that will send me e-mails, and the list doesn't
change often.

Thanks for your help.


"Ken Slovak - [MVP - Outlook]" wrote:

Something like this, placed in the ThisOutlookSession class module would
handle incoming emails, but you'd need to know to look in all those
subfolders of Inbox. It takes the part of the sender email address between
the "@" and the first "." as the company name:

Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
Dim oNS As Outlook.NameSpace
Dim oMail As Outlook.MailItem
Dim oMoved As Outlook.MailItem
Dim oTarget As Outlook.MAPIFolder
Dim oInbox As Outlook.MAPIFolder
Dim obj As Object

Dim sIDs() As String
Dim sRecip As String

Dim i As Long
Dim lPos As Long

sIDs = Split(EntryIDCollection, ",")

Set oNS = Application.GetNamespace("MAPI")
Set oInbox = oNS.GetDefaultFolder(olFolderInbox)

For i = LBound(sIDs) To UBound(sIDs)
Set obj = oNS.GetItemFromID(sIDs(i))
If obj.Class = olMail Then
Set oMail = obj
Set obj = Nothing

sRecip = oMail.SenderEmailAddress
lPos = InStr(1, sRecip, "@")
' strip out everything up to and including "@"
sRecip = Right(sRecip, Len(sRecip) - lPos)

lPos = InStr(1, sRecip, ".")
' strip everything from "." on
sRecip = Left(sRecip, lPos - 1)

Set oTarget = oInbox.Folders.Item(sRecip)
If oTarget Is Nothing Then
oInbox.Folders.Add (sRecip)
End If

Set oMoved = oMail.Move(oTarget)
End If
Next
End Sub

For the Sent Items folder handling you'd need something similar, in an
ItemAdd() event handler. You can find examples of ItemAdd() handlers at
www.outlookcode.com, one I can think of offhand for Inbox is "zaphtml", you
can search on that for an example.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Stratuser" wrote in message
news
Unfortunately, I'm a newbie to Outlook VBA. What would be the code for
moving any e-mail to or from Firm_A to the folder named Firm_A (under the
Inbox)? If you can get me this far, I guess I can just alter the code.



 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you write a macro to automatically import and map a calenda Dave L Outlook and VBA 4 September 30th 07 02:02 AM
write macro for toolbar? Penny Miller Outlook - Using Forms 11 December 21st 06 07:33 PM
How to write a program/macro to send a mail to a groups of e-mail Adi Outlook and VBA 8 December 4th 06 07:20 AM
Rules an macro Gerd Neumann Outlook and VBA 4 May 17th 06 10:48 PM
$$ looking for someone to write a macro for me [email protected] Outlook and VBA 0 March 16th 06 12:24 AM


All times are GMT +1. The time now is 08:56 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.