How to Monitor Multiple Folders? - vba

I have code in ThisOutlookSession to do something with items I send. It checks the Sent Items folder. It works only for one specified mailbox/folder.
I would like to monitor three mailboxes
I can change the line:
Set Items = AInbox.Items
and it will work, but only for the mailbox I set it to.
Private Sub Application_Startup()
Dim olNs As Outlook.NameSpace
Dim AInbox  As Outlook.MAPIFolder
Dim BInbox  As Outlook.MAPIFolder
Dim CInbox  As Outlook.MAPIFolder
Dim AItems As Items
Dim BItems As Items
Dim CItems As Items
Set olNs = Application.GetNamespace("MAPI")
Set AInbox = GetFolder("a#email.co.ukInbox\Sent Items")
Set BInbox = GetFolder("b#email.com\Inbox\Sent Items")
Set CInbox = GetFolder("c#email.com\Inbox\Sent Items")
Set Items = AInbox.Items
End Sub
 
Public Sub Items_ItemAdd(ByVal Item As Object)
If TypeOf Item Is Outlook.MailItem Then
'Do something
End If
End Sub

You may monitor any number of folders.
Private WithEvents AItems As Items
Private WithEvents BItems As Items
Private WithEvents CItems As Items
then
Set AItems = AInbox.Items
Set BItems = BInbox.Items
Set CItems = BInbox.Items
then
Public Sub AItems_ItemAdd(ByVal Item As Object)
Public Sub BItems_ItemAdd(ByVal Item As Object)
Public Sub CItems_ItemAdd(ByVal Item As Object)

Related

how to move a new mail (Excluding Re: & Fwd: ) to another folder in shared inbox

I'm trying to move the received new mails in shared inbox excluding the (Re: and FWD:) to "In progress folder". When I execute it's not working.
Error popping up in this line olReply.Move fldr
Option Explicit ' Consider this mandatory
' Tools | Options | Editor tab
' Require Variable Declaration
' If desperate declare as Variant
Public WithEvents olItems As Items
Private Sub Application_Startup()
Set olItems = Session.Folders("xxx#xxx.com").Folders("Inbox").Items
End Sub
Private Sub olItems_ItemAdd(ByVal Item As Object)
Dim olReply As MailItem
Dim fldr As Outlook.MAPIFolder
If Item.Class = olMail Then
If Len(Item.ConversationIndex) > 44 Then
Exit Sub
Else
Set fldr = Outlook.Session.Folders("xxx#xxx.com").Folders("In Progress")
olReply.Move fldr
End If
End If
End Sub
I Figured out the code myself and it works perfectly. The below-mentioned code reads through the new mails which hits the shared mailbox and move to another folder if it is a new mail and skips Conversation mails.
Option Explicit ' Consider this mandatory
' Tools | Options | Editor tab
' Require Variable Declaration
' If desperate declare as Variant
Public WithEvents olItems As Items
Private Sub Application_Startup()
Set olItems = Session.Folders("xxxxx#xxxx.com").Folders("Inbox").Items
End Sub
Private Sub olItems_ItemAdd(ByVal Item As Object)
Dim olNameSpace As NameSpace
Set olNameSpace = GetNamespace("MAPI")
Dim olReply As MailItem
Dim olObj As Object
Dim olDestFolder As Folder
If Item.Class = olMail Then
If Len(Item.ConversationIndex) > 44 Then ' Checks if the mail has conversation
Exit Sub
Else
Set olDestFolder = olNameSpace.Folders("xxx#xx.com").Folders("In Progress")'Set destination folder
Item.Move olDestFolder ' move to InProgress folder
End If
End If
End Sub

Listeners on different mailboxes

I am looking to call a set of functions when I receive email to different mailboxes (if a mail arrives to abc#outlook.com perform function1, if a mail arrives to def#outlook.com perform function2)
I have the code below for one mailbox but I am unsure how to expand it to also listen on another mailbox without conflicting. How can I setup listeners for multiple mailboxes?
Any help appreciated. Thank you
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
' default local Inbox
Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub Items_ItemAdd(ByVal item As Object)
'do Stuff to mailitem
End Sub
Just add another WithEvent to watch the other folder:
Private WithEvents Items As Outlook.Items
Private WithEvents Items1 As Outlook.Items
Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
' default local Inbox
Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
Set Items1 = objNS.Folders.Item("def#outlook.com").Folders.Item("Inbox").Folders.Item("ASubFolder").Items
End Sub
Private Sub Items_ItemAdd(ByVal Item As Object)
'do Stuff to mailitem
End Sub
Private Sub Items1_ItemAdd(ByVal Item As Object)
'do stuff.
End Sub

Declaring WithEvents variables - Compilation error: invalid characteristics in Sub or Function"

I have two sets of code. The first code adds a preset BCC address triggered by a button. The second code enables filing of emails, by tagging/categorizing the sent email, copying that sent email and then moving the copy to the folder indicated in pickfolder.
The two codes work separately.
When I paste both codes in ThisOutlookSession, the second one does not work. The error is (loosely translated from Dutch): "compilation error: invalid characteristics in Sub or Function" which relates to all three declarations (Dim WithEvents objInspectors As Inspectors, Dim WithEvents objMyNewMail As MailItem, Dim WithEvents colSentItems As Items)
The full codes:
'button bcc to crm system emailaddress)
Sub AddCRMtoBCC()
Dim objRecip As Recipient
Set oMsg = Application.ActiveInspector.CurrentItem
With oMsg
Set objRecip = oMsg.Recipients.Add("__#__.com")
objRecip.Type = olBCC
objRecip.Resolve
End With
Set oMsg = Nothing
End Sub
'________
'file emails
Dim WithEvents objInspectors As Inspectors
Dim WithEvents objMyNewMail As MailItem
Dim WithEvents colSentItems As Items
Private Sub Application_Startup()
Set objInspectors = Application.Inspectors
Dim NS As Outlook.NameSpace
Set NS = Application.GetNamespace("MAPI")
Set colSentItems = NS.GetDefaultFolder(olFolderSentMail).Items
Set NS = Nothing
End Sub
Private Sub Application_Quit()
Set objInspectors = Nothing
Set objMyNewMail = Nothing
End Sub
Private Sub objInspectors_NewInspector(ByVal Inspector As Inspector)
If Inspector.CurrentItem.Class <> olMail Then Exit Sub
Set objMyNewMail = Inspector.CurrentItem
End Sub
Private Sub objMyNewMail_Send(Cancel As Boolean)
If MsgBox("Are you sure you want to send this message?", vbYesNo + vbQuestion _
, "SEND CONFIRMATION") = vbNo Then
Cancel = True
End If
End Sub
Private Sub colSentItems_ItemAdd(ByVal Item As Object)
If Item.Class = olMail Then
Set Copy = Item.Copy
Set objNS = Application.GetNamespace("MAPI")
Set objFolder = objNS.PickFolder
Copy.Move objFolder
End If
End Sub
When you declare global variables at module level (ThisOutlookSession is a module), all of them should be declared at the top of the module.
thus, move those 3 lines at the top , before the very first sub()
Dim WithEvents objInspectors As Inspectors
Dim WithEvents objMyNewMail As MailItem
Dim WithEvents colSentItems As Items
objMyNewMail_Send() Cancel parameter must be declared ByRef

How to apply "WithEvents" trigger?

I have this code in a module:
Private WithEvents objNewMailItems As Outlook.Items
Public Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set mainInboxItems = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub mainInboxItems_ItemAdd(ByVal item As Object)
Call MandarMail.sendOutlookEmail
' //this send another email...
End Sub
This is my first time using a trigger. Visual Basic for Applications does not recognize this:
Private WithEvents objNewMailItems As Outlook.Items
I'm using Outlook 2013
If written in Outlook this should work.
You've declared objNewMailItems but used mainInboxItems
Dim WithEvents objNewMailItems As Items
Public Sub Application_Startup()
Dim objNS As NameSpace
Set objNS = olApp.GetNamespace("MAPI")
Set objNewMailItems = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub objNewMailItems_ItemAdd(ByVal item As Object)
'This will fire when you receive a new email.
Debug.Assert False
End Sub
Edit - I've found with this that after a while Outlook disables the macros, so have to manually run StartUp each day. Doesn't matter what I try with the Trust Centre settings - it keeps disabling my code.
Thx Darren. I modificated your code and now works fine:
Dim WithEvents objNewMailItems As Items
Public Sub Application_Startup()
Dim objNS As Outlook.NameSpace
Dim olApp As Outlook.Application
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set objNewMailItems = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub objNewMailItems_ItemAdd(ByVal item As Object)
'This will fire when you receive a new email.
MsgBox ("mail recibi")
End Sub

Runtime error in outlook macro - Item.senderName

I have the following macro;
Private WithEvents MySents As Outlook.Items
Private Sub Application_Startup()
Set MySents = Session.GetDefaultFolder(olFolderSentMail).Items
End Sub
Private Sub MySents_ItemAdd(ByVal Item As Object)
Dim objNS As Outlook.NameSpace
Dim targetFolder As Outlook.MAPIFolder
Set objNS = Outlook.GetNamespace("MAPI")
If TypeOf Item Is Outlook.MailItem Then
If Item.SenderName = "Sender 1" Then
Set targetFolder = objNS.Folders("Folder 1").Folders("Sent Items")
Set newItem = Item.Copy
newItem.Move targetFolder
End If
If Item.SenderName = "Sender 2" Then
Set targetFolder = objNS.Folders("Folder 2").Folders("Sent Items")
Set newItem = Item.Copy
newItem.Move targetFolder
End If
End If
End Sub
Last week this worked fine. Now when the macro runs I get a "Runtime error -2147221241 (80040107) The operation failed"
Looking at the debugger it fails on;
If Item.SenderName =
If I have a look at Items in the watch window most properties have "The operation failed" in the values.
Most strange about this is the fact that the message still gets copied anyway.
Can anyone see something silly I am doing?
The SenderName property returns a String indicating the display name of the sender for the Outlook item. It is set after the mail item has been sent. New items (unsent) don't have this property set.
You may consider using the SaveSentMessageFolder property which allows to set a Folder object that represents the folder in which a copy of the e-mail message will be saved after being sent. For example, you may handle the ItemSend event where you can set this property.
Sub SetSentFolder()
Dim myItem As Outlook.MailITem
Dim myResponse As Outlook.MailITem
Dim mpfInbox As Outlook.Folder
Dim mpf As Outlook.Folder
Set mpfInbox = Application.Session.GetDefaultFolder(olFolderInbox)
Set mpf = mpfInbox.Folders.Add("SaveMyPersonalItems")
Set myItem = Application.ActiveInspector.CurrentItem
Set myResponse = myItem.Reply
myResponse.Display
myResponse.To = "Eugene Astafiev"
Set myResponse.SaveSentMessageFolder = mpf
myResponse.Send
End Sub