Solved

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

Posted on 2006-07-21
11
290 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:Alan-Yeo
  • 6
  • 5
11 Comments
 

Author Comment

by:Alan-Yeo
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:Alan-Yeo
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:Alan-Yeo
ID: 17153546
BlueDevilFan you legend.

Many thanks for your help.
0
 

Author Comment

by:Alan-Yeo
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:Alan-Yeo
ID: 17221791
Oh sorry!

That worked perfectly, cheers!
0
 
LVL 76

Expert Comment

by:David Lee
ID: 17222096
No problem.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DIR issue 7 54
VBA error replacing data 6 39
VB6 Compile Compatibility Issue 4 102
I need help using System.Web.HttpUtility.HtmlEncode in my VB.Net application 3 76
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

776 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