I would like to send a mail to different team mail addresses when a new mail is drag-and-dropped to their respective folder.
If someone drag and drop a mail into SubFolderTeam1, a mail will be sent to MailTeam1.
Same when we drag and drop a mail into SubFolderTeam2, a mail will be sent to MailTeam2.
My folder structure:
- Inbox
- Parent Folder:
- SubFolderTeam1
- SubFolderTeam2
Team mail addresses:
- MailTeam1
- MailTeam2
The Parent Folder containing the Sub Folders is at the same level as the Default Folder "Inbox".
I have tried something based on the answers of this question: How do I trigger a macro to run after a new mail is received in Outlook?
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)
On Error GoTo ErrorHandler
Dim Msg As Outlook.MailItem
If TypeName(item) = "MailItem" Then
Set Msg = item
' ******************
'do the magic please
' ******************
End If
ProgramExit:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ProgramExit
End Sub
Combined with the below.
Sub Send_Emails()
Dim OutlookApp As Outlook.Application
Dim OutlookMail As Outlook.MailItem
Set OutlookApp = New Outlook.Application
Set OutlookMail = OutlookApp.CreateItem(olMailItem)
With OutlookMail
.BodyFormat = olFormatHTML
.Display
.HTMLBody = "Dear Team1" & "<br>" & "<br>" & "Please do you job. Thanks" & .HTMLBody
.To = "MailTeam1@gmail.com"
.Subject = "Test Subject"
.Send
End With
End Sub
I tried to insert the second structure of code within the first one.
I tried to run them separately: first code on a Class Module, the second on a classic Module.
And several other things that have no real sense in our dimension.