Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-19-2015, 10:27 PM
Bursal Bursal is offline VBA rule to forward messages from specific folder and forward Windows 7 64bit VBA rule to forward messages from specific folder and forward Office 2010 64bit
Novice
VBA rule to forward messages from specific folder and forward
 
Join Date: Aug 2014
Posts: 6
Bursal is on a distinguished road
Question VBA rule to forward messages from specific folder and forward

Hi

I am volunteer with a not for profit organisation. We receive a lot of emails that need to forwarded to contacts on various contact groups. If we could set this up it would save the group so much time.

The way I envisage this working is to create folders such as Newsletters and Officers. When an email arrives that needs to be forwarded the officer right clicks the email and moves it to the relevant folder, let's say Newsletters. Once in the Newsletters folder the email is forwarded to all contacts on the Newsletter distribution group. The email is then moved to the Newsletter folder on the server.



I am sure this is possible but VBA code is not a skill of mine so I would be grateful of and help.

Thanks for reading my post and I hope you can help me.

Bursal
Reply With Quote
  #2  
Old 09-20-2015, 12:09 AM
gmayor's Avatar
gmayor gmayor is offline VBA rule to forward messages from specific folder and forward Windows 7 64bit VBA rule to forward messages from specific folder and forward Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

What you ask is quite straightforward. You need to set up events for adding to the various folders. e.g. Put the following code in the ThisOutlookSession module and then run the macro Application_Startup (this will normally run when you restart Outlook, but you can run it manually).
Then when you move an item into the Newsletters or Officers folders (sub folders of Inbox) it will create a forward message for the moved item.

Because we don't know who you want to send the messages to, the messages are merely displayed so that you can add a recipient and any text, but these can be added in code to olItem and the message sent instead of being displayed.
Code:
Option Explicit
Private WithEvents NewsLetters As Outlook.Items
Private WithEvents Officers As Outlook.Items
Private olItem As Outlook.MailItem

Private Sub Application_Startup()
Dim olApp As Outlook.Application
    Set olApp = Outlook.Application
    Set NewsLetters = GetNS(olApp).GetDefaultFolder(olFolderInbox).folders("NewsLetters").Items
    Set Officers = GetNS(olApp).GetDefaultFolder(olFolderInbox).folders("Officers").Items
lbl_Exit:
    Exit Sub
End Sub

Private Sub Newsletters_ItemAdd(ByVal item As Object)
    On Error GoTo err_Handler
    Set olItem = item.Forward
    olItem.Display
lbl_Exit:
    Exit Sub
err_Handler:
    MsgBox Err.Number & " - " & Err.Description
    GoTo lbl_Exit
End Sub

Private Sub Officers_ItemAdd(ByVal item As Object)
    On Error GoTo err_Handler
    olItem.Display
lbl_Exit:
    Exit Sub
err_Handler:
    MsgBox Err.Number & " - " & Err.Description
    GoTo lbl_Exit
End Sub

Function GetNS(ByRef app As Outlook.Application) As Outlook.NameSpace
    Set GetNS = app.GetNamespace("MAPI")
lbl_Exit:
    Exit Function
End Function
Save the changes at the prompt on leaving Outlook.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 09-20-2015, 05:46 PM
Bursal Bursal is offline VBA rule to forward messages from specific folder and forward Windows 7 64bit VBA rule to forward messages from specific folder and forward Office 2010 64bit
Novice
VBA rule to forward messages from specific folder and forward
 
Join Date: Aug 2014
Posts: 6
Bursal is on a distinguished road
Default

Thank you Graham,

I am not in the office until the end of the week but can setup a test here at home. I see it easy to set folder so that's easy to add to as needed.

The first I noted is app.GetNamespace( "MAPI"), we use POP3 accounts.

As we will be using Outlook Contact Groups with the same name as the Inbox folder can that be made as a selection.

The other thing is we use BCC for all our distribution groups.

Thanks
again
Di
Reply With Quote
  #4  
Old 09-20-2015, 09:30 PM
gmayor's Avatar
gmayor gmayor is offline VBA rule to forward messages from specific folder and forward Windows 7 64bit VBA rule to forward messages from specific folder and forward Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The GetNamespace line is required!

If your distribution list names are the same as the folder names then change the folder related code as follows, but while testing ensure that Outlook is not configured to send immediately of you will send the test message to the contact group:

Code:
Private Sub Newsletters_ItemAdd(ByVal item As Object)
    On Error GoTo err_Handler
    Set olItem = item.Forward
    olItem.BCC = "Newsletters"
    olItem.sEnd
lbl_Exit:
    Exit Sub
err_Handler:
    MsgBox Err.Number & " - " & Err.Description
    GoTo lbl_Exit
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 09-22-2015, 04:37 AM
Aramiu Aramiu is offline VBA rule to forward messages from specific folder and forward Windows 7 64bit VBA rule to forward messages from specific folder and forward Office 2003
Novice
 
Join Date: Sep 2015
Posts: 8
Aramiu is on a distinguished road
Default

Hello,

I was looking for a code like this to help me forward and move messages in the same time. I am new to vba and I am trying to identify how this code cand be modified so that it can work with folders from Outlook's personal folders, saved on the local disk, not on the server... Is there any way I can do such a think?

If so, I have another problem: the folder that will be the object of this macro, also contains items (messages) that are auto-forwarded and moved by an outlook rule. This rule can't cover all the conditions that require moving and forwarding the specified items, so some of the messages have to be treated manually. That's why this code is perfect for what I need, but I have no ideea how can it be modified so that he will ignore messages that where moved and forwarded by the outlook rule...Can you please help me with an advise/solution?
Reply With Quote
  #6  
Old 09-22-2015, 06:40 AM
gmayor's Avatar
gmayor gmayor is offline VBA rule to forward messages from specific folder and forward Windows 7 64bit VBA rule to forward messages from specific folder and forward Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The macro as posted works with local folders, I suspect it will also double up the messages forwarded by your rule, so if that is the case, remove the forwarding from the rule and let the macro handle it.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #7  
Old 09-22-2015, 10:02 PM
Aramiu Aramiu is offline VBA rule to forward messages from specific folder and forward Windows 7 64bit VBA rule to forward messages from specific folder and forward Office 2003
Novice
 
Join Date: Sep 2015
Posts: 8
Aramiu is on a distinguished road
Default

Thank you for your reply. How should I modify the code so it will follow paths towards any of my pst folders? GetDefaultFolder has only options for outlook folders, I didn't find anything regarding local folders from pst
Reply With Quote
  #8  
Old 09-22-2015, 10:16 PM
gmayor's Avatar
gmayor gmayor is offline VBA rule to forward messages from specific folder and forward Windows 7 64bit VBA rule to forward messages from specific folder and forward Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Local folders in PST are Outlook folders? As in the original example you can access any Outlook folder from VBA e.g. in that example, the 'Newsletters' folder is a sub folder of Inbox

Code:
GetNS(olApp).GetDefaultFolder(olFolderInbox).folders("NewsLetters")
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #9  
Old 09-22-2015, 10:17 PM
Aramiu Aramiu is offline VBA rule to forward messages from specific folder and forward Windows 7 64bit VBA rule to forward messages from specific folder and forward Office 2003
Novice
 
Join Date: Sep 2015
Posts: 8
Aramiu is on a distinguished road
Default

Got it! Thanks!
Reply With Quote
  #10  
Old 09-22-2015, 10:18 PM
Aramiu Aramiu is offline VBA rule to forward messages from specific folder and forward Windows 7 64bit VBA rule to forward messages from specific folder and forward Office 2003
Novice
 
Join Date: Sep 2015
Posts: 8
Aramiu is on a distinguished road
Default

It looks like this:

Code:
Set Coduri = GetNS(olApp).Folders("1.CNA TEMPORAR INBOX SENT ALTE NEIMPORTANTE").Folders("PV MENTENANTA").Items
Reply With Quote
  #11  
Old 09-22-2015, 10:27 PM
Aramiu Aramiu is offline VBA rule to forward messages from specific folder and forward Windows 7 64bit VBA rule to forward messages from specific folder and forward Office 2003
Novice
 
Join Date: Sep 2015
Posts: 8
Aramiu is on a distinguished road
Default

Although, I don't seem to find a way to access anothe folder inside "PV MENTENANTA", for example...
Reply With Quote
  #12  
Old 09-22-2015, 10:30 PM
Aramiu Aramiu is offline VBA rule to forward messages from specific folder and forward Windows 7 64bit VBA rule to forward messages from specific folder and forward Office 2003
Novice
 
Join Date: Sep 2015
Posts: 8
Aramiu is on a distinguished road
Default

Figured it out.
Reply With Quote
  #13  
Old 09-22-2015, 10:37 PM
Aramiu Aramiu is offline VBA rule to forward messages from specific folder and forward Windows 7 64bit VBA rule to forward messages from specific folder and forward Office 2003
Novice
 
Join Date: Sep 2015
Posts: 8
Aramiu is on a distinguished road
Default

It doesn't bypass the rule I need the code to work together with the rule. Isn't there any possibility to obtain such a thing? The rule moves the messages from INBOX to a local folder, after it forwards them, and leaves them unread. Can something be done with the code so that it will ignore unread messages? That will probably be sufficient for the code to work with the rule.
Reply With Quote
  #14  
Old 09-22-2015, 10:49 PM
gmayor's Avatar
gmayor gmayor is offline VBA rule to forward messages from specific folder and forward Windows 7 64bit VBA rule to forward messages from specific folder and forward Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

It will ignore unread messages if you include a check for it e.g.

Code:
Private Sub Newsletters_ItemAdd(ByVal item As Object)
    If Not item.UnRead Then
        On Error GoTo err_Handler
        Set olItem = item.Forward
        olItem.BCC = "Newsletters"
        olItem.sEnd
    End If
lbl_Exit:
    Exit Sub
err_Handler:
    MsgBox Err.Number & " - " & Err.Description
    GoTo lbl_Exit
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #15  
Old 09-23-2015, 12:25 AM
Aramiu Aramiu is offline VBA rule to forward messages from specific folder and forward Windows 7 64bit VBA rule to forward messages from specific folder and forward Office 2003
Novice
 
Join Date: Sep 2015
Posts: 8
Aramiu is on a distinguished road
Default

It is perfect! Thank you very much!
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA rule to forward messages from specific folder and forward Rule to move sent messages to a specific folder LouisOscar Outlook 1 08-17-2011 05:42 AM
Auto Forward Emails - Specific Time Periods Only Dav Outlook 0 06-22-2011 12:03 PM
VBA rule to forward messages from specific folder and forward Create Rule to forward message to mobile# JoyConner Outlook 1 04-21-2011 05:44 PM
Forward Unread Messages after X Minutes? BamaBrad Outlook 2 03-13-2011 07:57 AM
Code to auto-forward weekend messages Whang Outlook 0 06-16-2010 02:18 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:02 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft