Solved

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

Posted on 2006-07-21
11
288 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
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…

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now