View Full Version : Running Rule for Public folder
oneblondebro
02-22-2017, 10:06 AM
Hi all,
I have set up a rule and used some script to copy the From, To, Received and Email Body to an excel spreadsheet, The rule is supposed to work on all new emails but does not, It only works when I set the rule up and choose the option to run the rule on all the existing emails in the folder, What I really want it to do is export the new emails to the excel spreadsheet. I have a feeling this is because of it being a Public Networked folder, Anyone know of any VBA that could get around this?
Thanks in advance.
skatonni
02-22-2017, 02:44 PM
You could set up ItemAdd code to monitor the Public Folder. https://www.slipstick.com/developer/itemadd-macro/
When an item is added you run the rules. http://www.outlookcode.com/codedetail.aspx?id=1266
You specify the Public folder with an optional parameter. http://msdn.microsoft.com/en-us/library/bb207534(v=office.12).aspx
oneblondebro
02-24-2017, 08:40 AM
Thanks for you reply,
I probably should have mentioned, I am very new to VBA, I have put all of the above into the "ThisOutlookSession" part in the VBA editor like below..
Option Explicit
Private objNS As Outlook.NameSpace
Private WithEvents objItems As Outlook.Items
Private Sub Application_Startup()
Dim objWatchFolder As Outlook.Folder
Set objNS = Application.GetNamespace("MAPI")
'Set the folder and items to watch:
Set objWatchFolder = objNS.GetDefaultFolder(olFolderInbox).Folders("MY INBOX")
Set objItems = objWatchFolder.Items
Set objWatchFolder = Nothing
End Sub
Private Sub objItems_ItemAdd(ByVal Item As Object)
' Your code goes here
MsgBox "Message subject: " & Item.Subject & vbCrLf & "Message sender: " & Item.SenderName & " (" & Item.SenderEmailAddress & ")"
Set Item = Nothing
End Sub
Sub RunAllInboxRules()
Dim st As Outlook.Store
Dim myRules As Outlook.Rules
Dim rl As Outlook.Rule
Dim count As Integer
Dim ruleList As String
'On Error Resume Next
' get default store (where rules live)
Set st = Application.Session.DefaultStore
' get rules
Set myRules = st.GetRules
' iterate all the rules
For Each rl In myRules
' determine if it's an Inbox rule
If rl.RuleType = olRuleReceive Then
' if so, run it
rl.Execute ShowProgress:=True
count = count + 1
ruleList = ruleList & vbCrLf & rl.Name
End If
Next
' tell the user what you did
ruleList = "These rules were executed against the Inbox: " & vbCrLf & ruleList
MsgBox ruleList, vbInformation, "Macro: RunAllInboxRules"
Set rl = Nothing
Set st = Nothing
Set myRules = Nothing
End Sub
I am sure I don't have this set up correctly. Could you have a scan through it please and point me in the right direction?
Many thanks
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.