PDA

View Full Version : Macro to run rules in Outlook Shared mailbox



bigj_309
04-19-2016, 04:51 AM
Hi Guys,

So I have managed to cobble together a Macro that catergorises email based on who it was sent to (using rules I've defined in outlook)

As such I'm using this script in the ThisOutlookSession

Dim i As Long
Private WithEvents olInboxItems As Items

Private Sub Application_Startup()
Dim objNS As NameSpace
Set objNS = Application.Session
Set olInboxItems = objNS.Folders("SHAREDMAILBOX").Folders("Inbox").Items
Set objNS = Nothing
End Sub



Sub RunRules()

Dim olRules As Outlook.Rules
Dim myRule As Outlook.Rule
Dim olRuleNames() As Variant
Dim name As Variant

olRuleNames = Array("1", "2", "3", "4", "5", "6")

Set olRules = Application.Session.DefaultStore.GetRules()

For Each name In olRuleNames()
For Each myRule In olRules
' Rules we want to run
If myRule.name = name Then
myRule.Execute ShowProgress:=True
End If
Next
Next
End Sub

and then in my module 1 I have :


Function GetFolderPath(ByVal FolderPath As String) As Outlook.Folder
Dim oFolder As Outlook.Folder
Dim FoldersArray As Variant
Dim i As Integer

On Error GoTo GetFolderPath_Error
If Left(FolderPath, 2) = "\\" Then
FolderPath = Right(FolderPath, Len(FolderPath) - 2)
End If
'Convert folderpath to array
FoldersArray = Split(FolderPath, "SHAREDMAILBOX")
Set oFolder = Application.Session.Folders.Item(FoldersArray(0))
If Not oFolder Is Nothing Then
For i = 1 To UBound(FoldersArray, 1)
Dim SubFolders As Outlook.Folders
Set SubFolders = oFolder.Folders
Set oFolder = SubFolders.Item(FoldersArray(i))
If oFolder Is Nothing Then
Set GetFolderPath = Nothing
End If
Next
End If
'Return the oFolder
Set GetFolderPath = oFolder
Exit Function

GetFolderPath_Error:
Set GetFolderPath = Nothing
Exit Function
End Function

The problem I have it that when I run the Macro it runs on my inbox and not on the shared mailbox.

I thought the above where I've told it to access SHAREDMAILBOX would sort this but as I'm new to VBA I don't know where I'm going wrong!

Any help would be appreciated.

bigj_309
04-20-2016, 04:58 AM
Anyone? I'm stumped, I've tried changing all the variables that I can see that may affect it.....

Any help would be greatly appreciated!!