VBA to process each email

Status
Not open for further replies.

txgator

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
First post here - was not able to find an answer using search - so here goes -

We have several large group email boxes at my company - and we are sent many emails.

Each customer of our company is assigned to a representative - at the company level. We have setup about 20 rules (not vba - rules) to process each email as it arrives. It is processed and moved to the representatives folder by the domain name. It seems to be working fine - however -

The list of customers is constantly changing and modifying the rules has become a nightmare - adding domains to this rep - deleting domains from this rep - no way to easily update the rules. The rules are also available to be modified by anyone that has access to the group box.

Is it possible to write VBA to process each email that arrives - and have it go through the same process?

If this is possible - I could take the assignment table in Excel - and write VBA code in Excel to build the case statements - and then just copy those statements and paste into the Outlook VBA -

I know - seems like a lot of work - but it will save me a LOT of time in the long run -

Any thoughts?

Thanks.
 

txgator

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Ok - found code that is working - but not sure it will work with large volumes -

Any suggestions? Thanks.

Code:
Private Sub Items_ItemAdd(ByVal item As Object)

  Dim posU As Integer
  Dim Msg As Outlook.MailItem
  Dim olApp As Outlook.Application
  Dim objNS As Outlook.NameSpace
  Set olApp = Outlook.Application
  Set objNS = olApp.GetNamespace("MAPI")
 
  ' default local Inbox
  Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
 
  On Error GoTo ErrorHandler
 
  Set objDestFolder = objNS.Folders("me@mycompany.com").Folders("ADS")
 
  If TypeName(item) = "MailItem" Then
    Set Msg = item
    ' ******************
        sEmail = item.SenderEmailAddress
        senderName = item.Sender

       
       

        posU = InStr(1, sEmail, "@yahoo", 1)
       
       
       
       
        If posU > 0 Then
            I = I + 1
            item.Move objDestFolder
        End If
       
       
       
    ' ******************
  End If
ProgramExit:
  Exit Sub
ErrorHandler:
  MsgBox Err.Number & " - " & Err.Description
  Resume ProgramExit
End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange

txgator

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Thanks Diane.

I did look at that thread as well - and like the thought of using the array to create the domain list and assign those to folders as needed.

I did not even think about using VBA to read in the table from Excel - and create the array as it was read -

If I could set the excel file on a file share - and have VBA read and create the array once a day - that would be even better -

Screen shot of the simple table that could be created in Excel and could be used to create the array is below.
 

Attachments

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
I have never done anything like that and I'm not sure where to start. Outlook would read the book on startup and load the array. I'm not sure if its workable in a rules macro. You'll either need to use a case statement or an array and I think an array is more efficient.
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Sorry, Diane, I'd already read the thread yesterday but was to tired to jump in :)

Reading the data of two columns is pretty fast, so you could even read it on every ItemAdd if that's really necessary. Something like that could be necessary if you want the users to get changes within the session else I'd do it on Outlook's startup as Diane suggested. If it's ok on startup, you could save all the needed target folder references globally, so you don't need to ref it in ItemAdd; that would make the code a little bit faster.

Here's an example of how to open the file: http://www.vboffice.net/smp=84
In your case you don't need to make the workbook visible. Once you've set the range you can use the Offset function to get the next column or row.

For large volumes I'd make the code as fast as possible. You don't need the olApp variable, directly use Application instead. You also don't need to call GetNamespace within Outlook. Do all the tests that could lead to leaving the function as early as possible. For instance, you don't need to ref the target folder, and then leave the function because it's not a mailitem.

This
Code:
If TypeOf item is MailItem
is faster than the string comparison. After checking the type it makes no sense to set a variable declared as MailItem on the item, and then not to use it. Using the Msg variable would be faster.

Also, think about whether you really want the MsgBox in case of an error poppping up. It could occur while nobody is on the desk, and it would stop Outlook from processing anythign else until someone clicks ok.
 

txgator

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Michael - Diane -

I really appreciate all of your feedback on this - it is going to save me so much time by having a workbook drive all of the rules - and eliminating all of the manual changes.

The user environment is setup like this - each user logs into their personal exchange account - and they also attach to one or more group boxes. The group box is where the rules currently exist - and I want to replace those rules with the VBA code I am going to design.

With that in mind - where is the Outlook VBA code stored? Will I have to give each user the code to read in the Excel data? Is there a way to have the code located in one place - and have the users access that code?

Also - within some of the existing rules - there are calls to REDIRECT emails - not forward them. I googled how to redirect using VBA and was surprised to find that VBA does not have a command to redirect - just forward. Any ideas on this one?

Look forward to completing this project - and be able to share the code with all of the users here.

Thanks.
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Outlook VBA is stored in Outlook, it cannot run on the server without Outlook. And you cannot easily deploy the VBA project, it must be installed manually for each user.

What is the difference between a redirect and a forward?
 

txgator

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
It will still be easier to manage - once I get the code written to open the excel file and populate the array.

The differences between automatically forwarding and redirecting an incoming message are as follows:
  • Message forwarding When a rule automatically forwards a message you receive, it leaves a copy of the message in your Inbox or in the folder to which the message was originally delivered. The rule then adds the designation "FW:" to the beginning of the Subject line, changes the message formatting, and then forwards the message to the account specified by the rule. The recipient represented by the account also sees that the message came from you.

  • Message redirection When a rule automatically redirects a message you receive, it also leaves a copy in your Inbox or in the folder to which the message was originally delivered. The rule then sends the message, unchanged, to the account specified by the rule. To the recipient, the message appears as though it came directly from the original sender. There is no indication that the message was delivered by way of your account.
I am wanting to do redirection on some of the emails that come in to the group box -
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
AFAIK you could simulate a redirection only if the sender is in your organization, and if you have the permission to send as that person. If it's an external sender, Outlook cannot redirect.
 
Status
Not open for further replies.
Thread starter Similar threads Forum Replies Date
S Outlook VBA rule script to process both MailItem and MeetingItem Using Outlook 0
L Macro/VBA to Reply All, with the original attachments Outlook VBA and Custom Forms 2
L VBA unknown character Outlook VBA and Custom Forms 2
G Move tasks up/down todo list by VBA Outlook VBA and Custom Forms 1
diver864 vba for a rule to automatically accept meeting requests with 'vacation' in subject, change to all-day event, change to free, don't send reply Outlook VBA and Custom Forms 1
K Use VBA to find Sender and Recipient from Microsfot 365 Journaled Email Items Outlook VBA and Custom Forms 3
J Want to learn VBA Macros for Outlook. What book can you recommend? Outlook VBA and Custom Forms 2
F VBA code to dock Styles whenever I write or edit an email Outlook VBA and Custom Forms 0
C VBA to prompt for Sent folder destination Outlook VBA and Custom Forms 3
B Adding signature to bottom of VBA reply email Outlook VBA and Custom Forms 1
B Change Font and Font size using VBA Outlook VBA and Custom Forms 9
M Outlook 2013 reminder email by using Outlook vba Outlook VBA and Custom Forms 2
D.Moore VBA script fail after Office 365 update Using Outlook 8
R Limiting length of saved attachment in VBA Outlook VBA and Custom Forms 2
S Skype for business meeting vba code Outlook VBA and Custom Forms 1
C How to use VBA to show only items x days old or more Outlook VBA and Custom Forms 1
B VBA to convert email to task, insert text of email in task notes, and attach copy of original email Outlook VBA and Custom Forms 4
D Outlook VBA error extracting property data from GetRules collection Outlook VBA and Custom Forms 10
S Reference Custom Fields with VBA Outlook VBA and Custom Forms 2
PGSystemTester VBA To Change AppointmentItem.BusyStatus From MeetingItem Before Send Using Outlook 0
A VBA macro for 15 second loop in send and received just for 1 specific mailbox Outlook VBA and Custom Forms 1
O Email not leaving Outbox when using Excel VBA to sync Outlook account Outlook VBA and Custom Forms 4
G VBA Macro Calendar Printing Assistant 4
R Help Revising VBA macro to delete email over different time span Outlook VBA and Custom Forms 0
B VBA to Collapse Task Folder Groups Outlook VBA and Custom Forms 1
R Expand VBA Permanent Delete Code Outlook VBA and Custom Forms 6
shrydvd vba to secure zip attachments Outlook VBA and Custom Forms 3
M Adding Subject to this Link-Saving VBA Outlook VBA and Custom Forms 5
N VBA to delete duplicates by message-id on common pst for 2 or more emails Outlook VBA and Custom Forms 0
S Change VBA script to send HTML email instead of text Outlook VBA and Custom Forms 3
M VBA to auto forward message with new subject and body text Outlook VBA and Custom Forms 8
A Custom VBA to sort emails into folders Outlook VBA and Custom Forms 0
L Moving emails with similar subject and find the timings between the emails using outlook VBA macro Outlook VBA and Custom Forms 1
B Outlook Business Contact Manager with SQL to Excel, User Defined Fields in BCM don't sync in SQL. Can I use VBA code to copy 1 field to another? BCM (Business Contact Manager) 0
R VBA for copying sent email to current folder under a shared mailbox Outlook VBA and Custom Forms 17
A Edit subject - and change conversationTopic - using VBA and redemption Outlook VBA and Custom Forms 2
N How can I increase/faster outlook VBA Macro Speed ? Using Outlook 2
N Outlook Email Rule execution through shortcut keys (VBA codes) Using Outlook 1
A VBA Code in Outlook disappears after first use Outlook VBA and Custom Forms 1
B Clear Offline Items (Mail Folder) via VBA Outlook VBA and Custom Forms 1
dweller Outlook 2010 Rule Ignores VBA Script Outlook VBA and Custom Forms 2
D.Moore Folder view settings by VBA macro Outlook VBA and Custom Forms 57
F VBA to ensure a code is entered in Subject title Outlook VBA and Custom Forms 1
B Vba to monitor time to respond to emails using a shared mailbox Outlook VBA and Custom Forms 5
N VBA Script to Open highlighted e-mail and Edit Message Outlook VBA and Custom Forms 5
G Outlook VBA and Google Calendar ("Events") Outlook VBA and Custom Forms 1
B Looking to get the Recipient email address (or even the "friendly name") from an email I am replying to using VBA Outlook VBA and Custom Forms 4
J VBA Outlook : Subject line : Cut and Paste name to heading , number to very end of the body of Email Outlook VBA and Custom Forms 1
M VBA to send reminder email if no response Using Outlook 13
D Using a VBA Custom Form to Send Reoccurring Email Upon Task Completion Outlook VBA and Custom Forms 4
Similar threads


















































Top