none
How to make a custom reply in Outlook 2013 to an email? RRS feed

  • Question

  • I get a lot of email that I reply to using the same paragraph of already existing text plus append a word attachment to the email. I'd like to automate this process (maybe execute the process when dropped into a mailbox or an accelerator keystroke combination is entered?) but after looking at the existing functionality provided by Outlook 2013 it is unclear to me that the following can be accomplished: construct a reply that has a canned constant paragraph, a reply signature all followed by the original email plus attach a word file and then send the resulting reply.

    Any suggestions?


    Richard Lewis Haggard


    Thursday, September 24, 2015 5:53 PM

Answers

  • OK, got it all working except for the move original part.

    Private Sub ItemsAutoReplyInboxJobsToReplyTo_ItemAdd(ByVal Item As Object) On Error GoTo ErrorTrap ' Recast the incoming mail object as email. Dim inEmail As Outlook.MailItem Set inEmail = Item ' Create the reply email. Dim replyMailItem As MailItem Set replyMailItem = inEmail.Reply ' Insert the reply text. replyMailItem.Body = "Thanks for <snipperd for brevity>." & replyMailItem.Body ' Attach the file. replyMailItem.attachments.Add "C:\Users\<snipped for brevity>.doc" ' Send the reply. replyMailItem.Send ' Move the original. inEmail.Move "<snipped for brevity>"

    ' Skip over the error message box. Goto AutoReply

    ErrorTrap: MsgBox "Error " & Err.Number & ": " & Err.Description

    End AutoReply: End Sub


    The part that is still failing is the "inEmail.Move..." line. I suspect that the reason for the failure is because of the event that is calling the macro which is

    in response to having dropped the email into an inbox in the first place. Could it be that the system won't let it be moved elsewhere because the email has not yet completed being moved to the inbox?


    Richard Lewis Haggard


    Friday, October 9, 2015 3:39 PM

All replies

  • Hi Richard,

    >> I'd like to automate this process (maybe execute the process when dropped into a mailbox or an accelerator keystroke combination is entered?) <<

    We can develop a macro to automate this process. Here is an example that reply the selection mails in the active explorer:

    Sub Reply()
    Dim replyMailItem As MailItem
    Set replyMailItem = ActiveExplorer.Selection.Item(1).Reply
    replyMailItem.Display
    Application.ActiveInspector.WordEditor.Application.Selection.Range = "Hello Word"
    replyMailItem.Attachments.Add "C:\book1.xml"
    End Sub
    

    In addition, here are an helpful link for Outlook developing:
    Automating Outlook from a Visual Basic Application

    Outlook VBA reference

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, September 25, 2015 2:20 AM
    Moderator
  • Fei,

    Giving the following pieces of code is not a good idea because it may prevent to exceptions in the code:

    Application.ActiveInspector.WordEditor.Application.Selection.Range = "Hello Word"
    replyMailItem.Attachments.Add "C:\book1.xml"

    I always recommend breaking the chain of property or method calls and declare them on separate line of code. So, you can debug the code and see what happens after each property or method calls. Moreover, you will be able to identify the cause of issues if any.  

    Richard, you can develop a VBA macro or COM add-in. The macro is suitable if you don't need to move the code on another PCs. The add-in can be easily deployed to multiple machines and allows to use the power of the BCL from the .Net framework. You may find the following articles helpful:

    Getting Started with VBA in Outlook 2010

    Walkthrough: Creating Your First VSTO Add-In for Outlook

    > maybe execute the process when dropped into a mailbox or an accelerator keystroke combination is entered?)

    You may consider handling the NewMailEx event of the Application class which is fired when a new item is received in the Inbox. Here is what MSDN states for the event:

    This event fires once for every received item that is processed by Microsoft Outlook. The item can be one of several different item types, for example, MailItem, MeetingItem, or SharingItem. The EntryIDsCollection string contains the Entry ID that corresponds to that item. Note that this behavior has changed from earlier versions of the event when the EntryIDCollection contained a list of comma-delimited Entry IDs of all the items received in the Inbox since the last time the event was fired.

    The NewMailEx event fires when a new message arrives in the Inbox and before client rule processing occurs. You can use the Entry ID returned in the EntryIDCollection array to call the NameSpace.GetItemFromID method and process the item. Use this method with caution to minimize the impact on Outlook performance. However, depending on the setup on the client computer, after a new message arrives in the Inbox, processes like spam filtering and client rules that move the new message from the Inbox to another folder can occur asynchronously. You should not assume that after these events fire, you will always get a one-item increase in the number of items in the Inbox.

    Also you may handle the ItemAdd event of the Items class which is fired when one or more items are added to the specified collection. This event does not run when a large number of items are added to the folder at once.

    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  
     Dim myOlAtts As Outlook.Attachments 
     Set myOlMItem = myOlApp.CreateItem(olMailItem)  
     myOlMItem.Save  
     Set myOlAtts = myOlMItem.Attachments  
     ' Add new contact to attachments in mail message  
     myOlAtts.Add Item, olByValue  
     myOlMItem.To = "Sales Team"  
     myOlMItem.Subject = "New contact"  
     myOlMItem.Send  
    End Sub

    P.S. As for the keystrokes, VBA and VSTO don't provide anything for that. You need to use Windows API functions to set up a keyboard hook. See http://blogs.msdn.com/b/vsod/archive/2010/04/09/using-shortcut-keys-to-call-a-function-in-an-office-add-in.aspx .


    Friday, September 25, 2015 5:44 AM
  • After giving this some thought I've decided that I'd like to use the act of dropping an email into a particular inbox as the trigger for the automated process. At the time that the email is dropped it invokes the same Reply process that happens when the user clicks on the Reply button while reading an email. After the Reply has displayed the email in a user editable format then it needs to insert a block of text and append a file attachment. The macro can stop right there and let the user manually click the Send button.

    It might make sense to break this down into smaller problems.

    First, what is the process by which a Reply is generated? How would a VBA macro to trigger the creation and display of the Reply form?

    Next, once the Reply has been generated, how will the macro know how to access the Reply form?

    Next, how would it insert a block of text at the start of the email body and then attach a file to the email as well?


    Richard Lewis Haggard

    Monday, September 28, 2015 4:22 PM
  • I'm halfway there. Using Outlook's 'quick step' feature I created a series of actions that were tied to a hot key. This allowed me to move the email to a specific folder, generate a reply and to insert a boilerplate message in that reply. The only remaining issue is to attach a file to the reply. So, how do I create a macro that attaches a file that is on a hard coded path to the currently open email?

    Richard Lewis Haggard

    Monday, September 28, 2015 7:35 PM
  • Hi Richard,

    If we run the macro above, it will reply the email which selected in the active explorer and insert the "Hello Word" into the body of reply email, add the attachments.

    If you want to Outlook automatically to run, we need to track the event to achieve the goal. Like the code sample provide Eugene, we can use Items.Itemadd event to track the one or more items are added to the specified collection.

    >>Using Outlook's 'quick step' feature I created a series of actions that were tied to a hot key. This allowed me to move the email to a specific folder, generate a reply and to insert a boilerplate message in that reply. The only remaining issue is to attach a file to the reply. So, how do I create a macro that attaches a file that is on a hard coded path to the currently open email?<<

    The is features is like the code I provide in above. We can run the macro above for the mail item in the selection. And to make it use easily, we can also create an command in QAT for the macro.

    Anther way is that we can create a rule to run the macro for the custom action. Here is an helpful thread for your reference:
    https://social.msdn.microsoft.com/Forums/office/en-US/15153b16-eb30-4290-9ef0-bac13a3906cb/starting-a-macro-using-outlook-rule

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, September 29, 2015 6:03 AM
    Moderator
  • I appreciate the reply but it is unclear how any of this would be applicable to the problem that I'm trying to solve. Are you sure this works?

    Where did olMailItem come from?

    How did MyOlMItem.Attaches get loaded when it was just created a few lines earlier?

    Won't setting To and Subject subvert the entire idea behind doing a reply?


    Richard Lewis Haggard

    Friday, October 9, 2015 1:17 PM
  • Nope. Quick Step is not going to do it for me. Too many keystrokes. Too much work. I am going to need a macro that gets triggered when an email is dropped into an email inbox.

    To recap:

    1) Detect when dropped into InBox (this I can do).

    2) Construct a reply which includes the original text plus a paragraph that is always the same.

    3) Attach a file.

    4) Send the reply.

    5) Move the original item out of the InBox and to a different InBox.


    Richard Lewis Haggard

    Friday, October 9, 2015 1:39 PM
  • OK, got it all working except for the move original part.

    Private Sub ItemsAutoReplyInboxJobsToReplyTo_ItemAdd(ByVal Item As Object) On Error GoTo ErrorTrap ' Recast the incoming mail object as email. Dim inEmail As Outlook.MailItem Set inEmail = Item ' Create the reply email. Dim replyMailItem As MailItem Set replyMailItem = inEmail.Reply ' Insert the reply text. replyMailItem.Body = "Thanks for <snipperd for brevity>." & replyMailItem.Body ' Attach the file. replyMailItem.attachments.Add "C:\Users\<snipped for brevity>.doc" ' Send the reply. replyMailItem.Send ' Move the original. inEmail.Move "<snipped for brevity>"

    ' Skip over the error message box. Goto AutoReply

    ErrorTrap: MsgBox "Error " & Err.Number & ": " & Err.Description

    End AutoReply: End Sub


    The part that is still failing is the "inEmail.Move..." line. I suspect that the reason for the failure is because of the event that is calling the macro which is

    in response to having dropped the email into an inbox in the first place. Could it be that the system won't let it be moved elsewhere because the email has not yet completed being moved to the inbox?


    Richard Lewis Haggard


    Friday, October 9, 2015 3:39 PM