Outlook Auto Reply without using Rule
-
Monday, July 30, 2012 7:31 PM
I have one mailbox with one primary and one secondary account. I need to send an automatic reply when the secondary account receives emails.
All Replies
-
Monday, July 30, 2012 8:39 PM
How that should to work without rule? In post client.
You should to make it on server.
Oskar Shon, Office System MVP
Press
if Helpful; Answer when a problem solved -
Tuesday, July 31, 2012 12:01 PMI tried that. Can it be done with a script? This email account is used for a help desk/service calls and it needs to reply immediately with an acknowledgement email.
mjc
-
Tuesday, July 31, 2012 12:58 PMShould be able to make it work with a script. You'll probably have to create an application-level event handler and attach it to the Folder.Items_ItemAdd Event of the folder into which the emails from the secondary account are delivered. Check to make sure that the added item is in fact a new message received at that account (examining MailItem.Recipients is probably the easiest approach) and then proceed to construct and send your reply accordingly.
-
Tuesday, July 31, 2012 3:17 PM
I have built a lot of Access databases but when it comes to Oulook I am lost. This is what I have but I do not know where to put it.
Public WithEvents myOlApp As Outlook.Application
Private Sub myOlApp_ItemSend(Item as Object, Cancel as Boolean)
Dim myClass as New EventClassModule
Sub Register_Event_Handler()
Set myClass.myOlApp = CreateObject("Outlook.Application")
Set myItem = Application.CreateItem(olMailItem)
Set myRecipient = myItem.Recipients.Add ("Lejeune RVAN")
myRecipient.Type = olCC
End Sub
Public WithEvents myOlItems As Outlook.Items
Public Sub Initialize_handler()
Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderContacts).Items
End Sub
Private Sub myOlItems_ItemAdd(ByVal Item As Object)
Dim myOlMItem As Outlook.MailItem
Set myOlMItem = myOlApp.CreateItem(olMailItem)
With myOlMItem
.BodyFormat = olFormatHTML
.HTMLBody = "<HTML><H2>The body of this message will appear in HTML.</H2><BODY> Please enter the message text here. </BODY></HTML>"
myOlMItem.Send
End Sub
mjc
-
Tuesday, July 31, 2012 5:44 PM
I'll try to summarize concisely the requirements for this:
All of the actual event handling code must be located inside a Class Module; let's name it myEventClass. Within the code for myEventClass, you have to Dim custom identifiers for the objects whose events you want to trap. For example, to trap the _ItemAdd event on .GetDefaultFolder(olFolderInbox).Items, you'd need:
Private WithEvents myItems as Items Private Sub myItems_ItemAdd(Item as Object, Cancel as Boolean) ' Code responding to the event goes here End Sub
(I typically use Private declarations as my default and only relax the scope access if I need the functionality.)
Within the Class Module you also have to properly attach the custom objects (here, myItems) to the actual objects in Outlook that will be firing the events. I typically write a separate init subroutine for this instead of using the myEventClass_Initialize Event because it affords more control over when the initialization happens. (You have to make sure you code the Sub safely in the event it gets executed more than once on a given instance of the class, however.) If you know at the time of writing the code which Outlook object each custom object is going to be attached to, the init Sub can look like this:
Public Sub Initialize() Set myItems = Application.Session.GetDefaultFolder(olFolderInbox).Items End Sub
If you want run-time control over which Outlook objects are bound, then just add parameters:
Public Sub Initialize(Optional tgtItems As Items = Nothing) If tgtItems Is Nothing Then Set myItems = Application.Session.GetDefaultFolder(olFolderInbox).Items Else Set myItems = tgtItems End If End Sub
Again, all of the above code will be located within the Class Module. Once the Class Module code is in place, you have to set Outlook up so that there's always a properly configured instance of the class instantiated and in memory. The most straightforward way I know of to do this is to put code like the following in the code for the 'ThisOutlookSession' object:
Dim mec as New myEventClass Private Sub Application_Startup() mec.Initialize End Sub
(The 'New' keyword here is crucial: without it, you're just telling VBA the type of "mec"; with it, VBA actually creates a new object of type "myEventClass" with which you can work.)
Hope this helps,
Brian- Proposed As Answer by VBAToolsMVP Wednesday, August 01, 2012 7:20 AM
-
Tuesday, July 31, 2012 5:50 PM
Brian you are a beautiful person.
Thank you so much for your time, consideration, and understanding.
I will let you know how it works for me.
Thanks again~
mjc
-
Wednesday, August 01, 2012 2:28 PM
Good Day Brian~
I could not get it to work for me. I believe that it is because it is an account that is not the primary. It was added on to my email for me to manage and even when I respond to emails received in that inbox it sends with my primary, named email address.
Thank again for all that you did for me~
mjc
-
Thursday, August 02, 2012 12:11 AM
The Items_ItemAdd event shouldn't care which account the emails were received through, unless there's some sort of funny bounce in the way they have the forwarding set up.
Are you saying that you were trying to send the auto-reply through the tech support account, but the message wouldn't send properly?
-
Thursday, August 02, 2012 12:58 PM
I am trying to get the auto-reply to come from the tech inbox. I receive a message with that address in the "To" line and I want it to send a message that confirms it immediately. I have tried to set a rule but I have to click the "Run Rules Now" button to make that work. I do not know nor have I tried to do much in Outlook 2010 so I am lost.
Thank you for your patience with me~
mjc
-
Thursday, August 02, 2012 1:06 PM
Huh, it seems strange to me that the Rule wouldn't automatically run -- I assume you tried a Rule set as 'run on messages I receive through a specified account,' with the tech support account selected as the specified account?
Not to worry, these things can be subtle to set up at times.
-
Friday, August 03, 2012 2:06 PM
This is what I have so far:
Public WithEvents myOlApp As Outlook.Application
__________________________________
Private Sub myOlApp_ItemSend(Item As Object, Cancel As Boolean)
Dim myClass As New EventClassModule
Sub Register_Event_Handler()
Set myClass.myOlApp = CreateObject("Outlook.Application")
Set myItem = Application.CreateItem(olMailItem)
Set myRecipient = myItem.Recipients.Add("Jon Grande")
myRecipient.Type = olCC
End Sub_____________________________________
Public WithEvents myOlItems As Outlook.Items
Public Sub Initialize_handler()
Set myOlItems = Application
End Sub______________________________________
Private Sub myOlItems_ItemAdd(ByVal Item As Object)
Dim myOlMItem As Outlook.MailItem
Dim strEmailBodySet myOlMItem = myOlApp.CreateItem(olMailItem)
strEmailBody = "Dear Patron,"
strEmailBody = strEmailBody & "<br>" & "Thank you for contacting RVAN Customer Support. This message is to confirm receipt of your email. In order to more efficiently support you a service ticket must be issued."
strEmailBody = strEmailBody & "<br>" & "To receive a ticket tracking number, please click on the link below and complete the submission form:"
strEmailBody = strEmailBody & "<br>" & [RVAN Customer Support] 'This is a link
strEmailBody = strEmailBody & "<br>" & "Thank you again for contacting us."
strEmailBody = strEmailBody & "<br>" & ""
strEmailBody = strEmailBody & "<br>" & "Sincerely,"
strEmailBody = strEmailBody & "<br>" & "Customer Support"
With myOlMItem
.BodyFormat = olFormatHTML
.Subject = "RVAN Customer Support RPL#" & [RP#] 'Like autonumber
.HTMLBody = strEmailBody
.SendEnd With
End Sub
_______________________________________
I don't know how to work in the autonumber or the link is it the same as Access?
Dim objItem As Object
Dim objReply As Outlook.MailItem
Dim varCount As Variant
Dim intLoc As Integer
Dim strSubject As String
Dim strConversation As String
On Error Resume Next
Set objItem = GetCurrentItem()
If objItem.Class = olMail Then
strConversation = objItem.ConversationTopic
strSubject = objItem.Subject
Set objReply = objItem.Reply
If Left(UCase(strSubject), 3) <> "RE[" Then
' handle first reply
objReply.Subject = "Re[2]: " & strSubject
Else
' handle other replies
intLoc = InStr(strSubject, "]")
varCount = Mid(strSubject, 4, intLoc - 4)
' increment number depending on whether
' original message is even or odd
If varCount Mod 2 = 1 Then
varCount = varCount + 1
Else
varCount = varCount + 2
End If
objReply.Subject = "Re[" & varCount & "]: " & strConversation
End If
objReply.Display
End IfAlso what goes in class and what goes in macro?
Thank you in advance~
mjc
-
Friday, August 03, 2012 2:39 PM
Ah, this "RE[##]" numbering aspect is new... I think I see what you're trying to accomplish, though. Is the "RE[##]" number incrementing different than the ".Subject = "RVAN Customer Support RPL#" & [RP#] 'Like autonumber" number? What is the meaning of the number that would appear in "RPL#xxxx"?
-----
What are you trying to do with the code in the Application_ItemSend event? Add yourself as a CC to every email that goes out? If all you're trying to do is be CC-ed on the tech support reply emails, it would be easier just to move this code block (modifications possibly required) to the myOlItems_ItemAdd event.
If you want every single email that you send to have you CC-ed on it, then I think it's proper to have this code in the myOlApp_ItemSend event. You don't have to create a custom event handler for the Application object, though; just go to the code for "ThisOutlookSession", select "Application" from the left side dropdown above the code window (see pic), and "ItemSend" from the right side dropdown. The code would then just go in that event, no other initialization required.
-----
If the emails from the tech support account are arriving in your Inbox, within your "Public Sub Initialize_handler()" you should have:
Set myOlItems = Session.GetDefaultFolder(olFolderInbox).Items
-----
To avoid ambiguity, it should be
Dim strEmailBody As String
-----
To include a link in the HTML, the tag structure is:
<a href="http://link.to.ticket.generation.form">[RVAN Customer Support]</a>- Edited by Brian Skinn Friday, August 03, 2012 2:41 PM Forgot to add pic
-
Friday, August 03, 2012 2:55 PM
the "RE[##]" number and the ".Subject = "RVAN Customer Support RPL#" & [RP#] number are the same thing I have not changed it yet.
The meaning of the number that would appear in "RPL#xxxx" is RPL is a prefix for reply because I have a database that assigns ticket numbers and RPL+the autonumber will indicate the reply order only.
No I do not want to cc.
Thanks for the clarification I will place your code and let you view it when I finish.
Thanks again~
mjc
-
Friday, August 03, 2012 6:18 PM
This is what I have so far without the autonumber but it seems like I have to much and it should be more simple:
Private WithEvents olInboxItems As Items
Private Sub Application_NewMail()
Set olInboxItems = _Session.GetDefaultFolder(olFolderInbox).ItemsEnd SubPrivate Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If TypeName(Item) <> "MailItem" Then Exit Sub
Dim srchString As String, NewText As String
srchString = "LejRVAN@usmc-mccs.org"
If InStr(1, Item.To, srchString, vbTextCompare) Then
Dim olApp As Outlook.Application
Dim myClass As New EventClassModule
Dim oNS As NameSpace
Dim oFolder As MAPIFolder
Dim oOlMItem As Object
Dim strEmailBody As String
Dim oReply As MailItem
Set olApp = Outlook.Application
Set oNS = GetNamespace("MAPI")
Set oFolder = onMAPI.GetDefaultFolder(olFolderInbox)
Set oReply = ofFolder.Items.GetLast
Set myOlMItem = Application.CreateItem(olMailItem)
strEmailBody = "Dear Patron,"
strEmailBody = strEmailBody & "<br>" & "Thank you for contacting RVAN Customer Support. This message is to confirm receipt of your email. In order to more efficiently support you a service ticket must be issued."
strEmailBody = strEmailBody & "<br>" & "To receive a ticket tracking number, please click on the link below and complete the submission form:"
strEmailBody = strEmailBody & "<br>" & "<a> href=P:\RVAN\Help Desk\HelpzLive.accdb</a>"
strEmailBody = strEmailBody & "<br>" & "Thank you again for contacting us."
strEmailBody = strEmailBody & "<br>" & ""
strEmailBody = strEmailBody & "<br>" & "Sincerely,"
strEmailBody = strEmailBody & "<br>" & "Customer Support"
With oNewMail.BodyFormat = olFormatHTML.Subject = "RVAN Customer Support RPL#" & [RP#] 'Like autonumber.HTMLBody = strEmailBody.SendEnd With
End If
End If
End Sub
Public WithEvents myOlItems As Outlook.Items
Public Sub Initialize_handler()
Set myOlItems = Session.GetDefaultFolder(olFolderInbox).Items
End Sub
mjc
- Proposed As Answer by Leo_GaoModerator Monday, August 06, 2012 8:03 AM
- Marked As Answer by Leo_GaoModerator Friday, August 10, 2012 1:33 AM
-
Monday, August 06, 2012 1:22 PM
I took what you wrote, pared it down, and restructured it a bit for clarity. I didn't try to implement the autonumber at all; what you posted before is a good approach, so I figure you can get that working yourself. I tested a modified version of the code below in my Outlook and it works fine. I think the InStr call on Item.To should work properly for what you're trying to do; give it a shot and see.
This code goes in a Class Module called 'myOlItems' (comment out or delete any existing code):
Option Explicit Private WithEvents myOlItemsObj As Items Public Sub Initialize_handler() Set myOlItemsObj = Session.GetDefaultFolder(olFolderInbox).Items End Sub Private Sub myOlItemsObj_ItemAdd(ByVal Item As Object) Const srchString As String = "LejRVAN@usmc-mccs.org" Dim myMI As MailItem Dim strEmailBody As String If Item.Class <> olMail Then Exit Sub ' Don't process item if not a MailItem If Not InStr(Item.To, srchString) > 0 Then Exit Sub ' Don't process if string not found Set myMI = Application.CreateItem(olMailItem) ' Create the new item ' Compose the body strEmailBody = "Dear Patron," strEmailBody = strEmailBody & "<br><br>" & "Thank you for contacting RVAN Customer Support. This message is to confirm receipt of your email. In order to more efficiently support you a service ticket must be issued." strEmailBody = strEmailBody & "<br><br>" & "To receive a ticket tracking number, please click on the link below and complete the submission form:" strEmailBody = strEmailBody & "<br><br>" & "<a href=""P:\RVAN\Help Desk\HelpzLive.accdb"">P:\RVAN\Help Desk\HelpzLive.accdb</a>" strEmailBody = strEmailBody & "<br><br>" & "Thank you again for contacting us." strEmailBody = strEmailBody & "<br><br>" & "Sincerely," strEmailBody = strEmailBody & "<br>" & "Customer Support" ' Set the body, subject, etc. for the new MailItem, and send With myMI .Recipients.Add Item.SenderEmailAddress ' Send the reply to the original email's sender .BodyFormat = olFormatHTML .Subject = "RVAN Customer Support RPL#" & "[RP#]" 'Like autonumber .HTMLBody = strEmailBody .Send End With ' Trigger a Send/Receive so the message goes out immediately Session.SendAndReceive showProgressDialog:=False End SubThis code goes in the ThisOutlookSession object:
Option Explicit Dim myOI As New myOlItems Private Sub Application_Startup() myOI.Initialize_handler End Sub
Put the code in place, close and reopen Outlook, and give it a shot...- Proposed As Answer by Leo_GaoModerator Thursday, August 09, 2012 3:24 AM
- Marked As Answer by Leo_GaoModerator Friday, August 10, 2012 1:33 AM

