![]() |
#1
|
|||
|
|||
![]()
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 |
#2
|
||||
|
||||
![]()
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
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
![]()
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 |
#4
|
||||
|
||||
![]()
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 |
#5
|
|||
|
|||
![]()
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? |
#6
|
||||
|
||||
![]()
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 |
#7
|
|||
|
|||
![]()
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
|
#8
|
||||
|
||||
![]()
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 |
#9
|
|||
|
|||
![]()
Got it! Thanks!
![]() |
#10
|
|||
|
|||
![]()
It looks like this:
Code:
Set Coduri = GetNS(olApp).Folders("1.CNA TEMPORAR INBOX SENT ALTE NEIMPORTANTE").Folders("PV MENTENANTA").Items |
#11
|
|||
|
|||
![]()
Although, I don't seem to find a way to access anothe folder inside "PV MENTENANTA", for example...
![]() |
#12
|
|||
|
|||
![]()
Figured it out.
![]() |
#13
|
|||
|
|||
![]()
It doesn't bypass the rule
![]() |
#14
|
||||
|
||||
![]()
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 |
#15
|
|||
|
|||
![]()
It is perfect! Thank you very much!
|
![]() |
Thread Tools | |
Display Modes | |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
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 |
![]() |
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 |