Reputation: 826
I manage several mail accounts in my Outlook
and I want to check if there is any change happens in any of the mails folder (received a mail). Then check Folder name is "My Out Folder" then it should forward that mail to specified account i.e "[email protected]"
The following code only handle first Mail Account folders. I don't want to use several variables for every mail so I am looking for a way to create class module so that it can detect Change_events in all the emails. only trigger when the folder name is "My Out Folder". Following is in class "ThisOutlookSession":
Const FowardEmailAddress As String = "[email protected]"
Public WithEvents myOutFolder As Outlook.Folders
Private Sub Application_Startup()
Set MyNS = Application.GetNamespace("MAPI")
Set myOutFolder = MyNS.Folders(1).Folders
End Sub
Private Sub myOutFolder_FolderChange(ByVal Folder As Outlook.Folder)
Dim FowardItem As Outlook.MailItem
If Folder.Name = "My Out Folder" Then
Set FowardItem = Folder.Items.GetFirst
AutoForwardAllSentItems FowardItem, FowardEmailAddress
End If
End Sub
code of AutoForwardAllSentItems() is in module:
Sub AutoForwardAllSentItems(Item As Outlook.MailItem, FM$)
Dim strMsg As String
Dim myFwd As Outlook.MailItem
Set myFwd = Item.Forward
myFwd.Recipients.Add FM
myFwd.Send
'myFwd.Display
Set myFwd = Nothing
End Sub
I don't know what is best way to achieved the same results.
Upvotes: 0
Views: 180
Reputation: 49395
You need to subscribe to each folder separately. To iterate over all stores in Outlook you can use the Namespace.Stores property which returns a Stores
collection object that represents all the Store objects in the current profile. Use the Stores
and Store
objects to enumerate all folders and search folders on all stores in the current session. For example:
Sub EnumerateFoldersInStores()
Dim colStores As Outlook.Stores
Dim oStore As Outlook.Store
Dim oRoot As Outlook.Folder
On Error Resume Next
Set colStores = Application.Session.Stores
For Each oStore In colStores
Set oRoot = oStore.GetRootFolder
Debug.Print (oRoot.FolderPath)
EnumerateFolders oRoot
Next
End Sub
Private Sub EnumerateFolders(ByVal oFolder As Outlook.Folder)
Dim folders As Outlook.folders
Dim Folder As Outlook.Folder
Dim foldercount As Integer
On Error Resume Next
Set folders = oFolder.folders
foldercount = folders.Count
'Check if there are any folders below oFolder
If foldercount Then
For Each Folder In folders
Debug.Print (Folder.FolderPath)
EnumerateFolders Folder
Next
End If
End Sub
In your case it seems you just need to get the "My Out Folder" of each store and subscribe to the events. The Store.GetDefaultFolder method returns a Folder object that represents the default folder in the store and that is of the type specified by the FolderType
argument. So, you may get any standard folder and then navigate the required one.
Also you need to distinguish the Items.ItemAdd event which is fired when one or more items are added to the specified collection/folder and the Items.ItemChange event which is fired when an item in the specified collection is changed.
Upvotes: 1