Solved

Drag and Drop autoresponse emails - In Outlook - More than one folder.

Posted on 2006-07-21
11
293 Views
Last Modified: 2010-04-30
Relating to question... http://www.experts-exchange.com/Applications/MS_Office/Outlook/Q_21916262.html

Would like to change this code so I can use 3 seperate folders, with 3 seperate template replies.

Private WithEvents olkFolder As Outlook.Items

Private Sub Application_Startup()
    'Change the path of the folder to watch
    Set olkFolder = OpenMAPIFolder("\FolderName\SubFolderName").Items
End Sub

Private Sub Application_Quit()
    Set olkFolder = Nothing
End Sub

Private Sub olkFolder_ItemAdd(ByVal Item As Object)
    Dim olkMessage As Outlook.MailItem
    Set olkMessage = Application.CreateItem(olMailItem)
    With olkMessage
        .Recipients.Add Item.SenderEmailAddress
        'Change the subject as desired
        .Subject = "My Subject"
        'Change the message format as desired
        .BodyFormat = olFormatHTML
        'Change the message body as desired
        .HTMLBody = "My message"
        .Send
    End With
    Set olkMessage = Nothing
End Sub

'Credit where credit is due.
'The code below is not mine.  I found it somewhere on the internet but do
'not remember where or who the author is.  The original author(s) deserves all
'the credit for these functions.
Function OpenMAPIFolder(szPath)
    Dim app, ns, flr, szDir, i
    Set flr = Nothing
    Set app = CreateObject("Outlook.Application")
    If Left(szPath, Len("\")) = "\" Then
        szPath = Mid(szPath, Len("\") + 1)
    Else
        Set flr = app.ActiveExplorer.CurrentFolder
    End If
    While szPath <> ""
        i = InStr(szPath, "\")
        If i Then
            szDir = Left(szPath, i - 1)
            szPath = Mid(szPath, i + Len("\"))
        Else
            szDir = szPath
            szPath = ""
        End If
        If IsNothing(flr) Then
            Set ns = app.GetNamespace("MAPI")
            Set flr = ns.Folders(szDir)
        Else
            Set flr = flr.Folders(szDir)
        End If
    Wend
    Set OpenMAPIFolder = flr
End Function

Function IsNothing(obj)
  If TypeName(obj) = "Nothing" Then
    IsNothing = True
  Else
    IsNothing = False
  End If
End Function

I had a go myself but couldn't get it to work.
0
Comment
Question by:Antonio King
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 

Author Comment

by:Antonio King
ID: 17152525
Sorry the three folders can be called folder1, folder2 and folder3. I can change these.
0
 
LVL 76

Accepted Solution

by:
David Lee earned 250 total points
ID: 17152750
Good morning, Alan-Yeo.  

That's pretty simple.  Only requires a minimal change.  

Private WithEvents olkFolder1 As Outlook.Items, _
    WithEvents olkFolder2 As Outlook.Items, _
    WithEvents olkFolder3 As Outlook.Items

Private Sub Application_Startup()
    'Change the path of the folder to watch
    Set olkFolder1 = OpenMAPIFolder("\FolderName\SubFolderName").Items
    Set olkFolder2 = OpenMAPIFolder("\FolderName\SubFolderName").Items
    Set olkFolder3 = OpenMAPIFolder("\FolderName\SubFolderName").Items
End Sub

Private Sub Application_Quit()
    Set olkFolder1 = Nothing
    Set olkFolder2 = Nothing
    Set olkFolder3 = Nothing
End Sub

Private Sub olkFolder1_ItemAdd(ByVal Item As Object)
    Dim olkMessage As Outlook.MailItem
    Set olkMessage = Application.CreateItem(olMailItem)
    With olkMessage
        .Recipients.Add Item.SenderEmailAddress
        'Change the subject as desired
        .Subject = "My Subject"
        'Change the message format as desired
        .BodyFormat = olFormatHTML
        'Change the message body as desired
        .HTMLBody = "My message"
        .Send
    End With
    Set olkMessage = Nothing
End Sub

Private Sub olkFolder2_ItemAdd(ByVal Item As Object)
    Dim olkMessage As Outlook.MailItem
    Set olkMessage = Application.CreateItem(olMailItem)
    With olkMessage
        .Recipients.Add Item.SenderEmailAddress
        'Change the subject as desired
        .Subject = "My Subject"
        'Change the message format as desired
        .BodyFormat = olFormatHTML
        'Change the message body as desired
        .HTMLBody = "My message"
        .Send
    End With
    Set olkMessage = Nothing
End Sub

Private Sub olkFolder3_ItemAdd(ByVal Item As Object)
    Dim olkMessage As Outlook.MailItem
    Set olkMessage = Application.CreateItem(olMailItem)
    With olkMessage
        .Recipients.Add Item.SenderEmailAddress
        'Change the subject as desired
        .Subject = "My Subject"
        'Change the message format as desired
        .BodyFormat = olFormatHTML
        'Change the message body as desired
        .HTMLBody = "My message"
        .Send
    End With
    Set olkMessage = Nothing
End Sub
0
 
LVL 76

Expert Comment

by:David Lee
ID: 17152754
Leave the OpenMAPIFolder() and IsNothing() as they are.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Antonio King
ID: 17152823
superb, I tried something simliar to the above but mine didnt work :|

Anyway, this will be roled out across our company, just worried people might drop the emails into the wrong folder, which means a rejection email could be sent to them.

Could you possibly do me another script that will ask a confirmation message...

"Are you sure you want to send an application accept email to.... <RECEIPIENT>"

If the user clicks no could the email that was dropped into the folder be moved back to the inbox?

Many thanks! you've been most helpful.
0
 
LVL 76

Expert Comment

by:David Lee
ID: 17153348
No problem.  Change each of olkFolder_ItemAdd subs to look like this:

Private Sub olkFolder1_ItemAdd(ByVal Item As Object)
    Dim olkMessage As Outlook.MailItem
    If MsgBox("Are you sure you want to send an application accept email to " & Item.SenderEmailAddress & "?", vbYesNo, "Confirm Send") = vbYes Then
        Set olkMessage = Application.CreateItem(olMailItem)
        With olkMessage
            .Recipients.Add Item.SenderEmailAddress
            'Change the subject as desired
            .Subject = "My Subject"
            'Change the message format as desired
            .BodyFormat = olFormatHTML
            'Change the message body as desired
            .HTMLBody = "My message"
            .Send
        End With
    End If
    Set olkMessage = Nothing
End Sub
0
 

Author Comment

by:Antonio King
ID: 17153546
BlueDevilFan you legend.

Many thanks for your help.
0
 

Author Comment

by:Antonio King
ID: 17153647
Just tested the script, works a charm except it doesn't seem to move the email back to the inbox if you select no?
0
 
LVL 76

Expert Comment

by:David Lee
ID: 17157281
Sorry, I missed that part.  Use this version instead.

Private Sub olkFolder1_ItemAdd(ByVal Item As Object)
    Dim olkMessage As Outlook.MailItem
    If MsgBox("Are you sure you want to send an application accept email to " & Item.SenderEmailAddress & "?", vbYesNo, "Confirm Send") = vbYes Then
        Set olkMessage = Application.CreateItem(olMailItem)
        With olkMessage
            .Recipients.Add Item.SenderEmailAddress
            'Change the subject as desired
            .Subject = "My Subject"
            'Change the message format as desired
            .BodyFormat = olFormatHTML
            'Change the message body as desired
            .HTMLBody = "My message"
            .Send
        End With
    Else
        Item.Move Session.GetDefaultFolder(olFolderInbox)
    End If
    Set olkMessage = Nothing
End Sub
0
 
LVL 76

Expert Comment

by:David Lee
ID: 17220753
Any update, Alan-Yeo?
0
 

Author Comment

by:Antonio King
ID: 17221791
Oh sorry!

That worked perfectly, cheers!
0
 
LVL 76

Expert Comment

by:David Lee
ID: 17222096
No problem.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question