Outlook 2013: select multiple emails and autoreply using template

534 views Asked by At

I am trying to get this code to work.

I want to select multiple emails from my inbox and send a auto reply using a template.

I am getting a run-time error: Object variable or With Block variable not set.

Any help would be appreciated. Also I would like to add a msg box telling me how many items were sent.

Option Explicit

Sub ReplywithTemplate()
Dim Item As Outlook.MailItem
Dim oRespond As Outlook.MailItem


For Each Item In ActiveExplorer.Selection

' This sends a response back using a template
Set oRespond = Application.CreateItemFromTemplate("C:\Users\Accounting\AppData\Roaming\Microsoft\Templates\scautoreply.oft")

With oRespond

    .Recipients.Add Item.SenderEmailAddress
    .Subject = Item.Subject

    ' includes the original message as an attachment
    .Attachments.Add Item

    ' use this for testing, change to .send once you have it working as desired
    .Display
End With
On Error Resume Next                                                 
Next
Set oRespond = Nothing

End Sub
1

There are 1 answers

1
Eugene Astafiev On

I have noticed the following lines of code:

 For Each oRespond In ActiveExplorer.Selection

 ' This sends a response back using a template
 Set oRespond = Application.CreateItemFromTemplate("C:\Users\Accounting\AppData\Roaming\Microsoft\Templates\scautoreply.oft")

 With oRespond

You need to use a new variable for creating an auto-reply email from a template because the selected Outlook item is missed (replaced with a newly created one).

So, basically you can create an item from a template, add recipients from the selected Outlook item and call the Send method. Or you can use the Reply method of the selected item in Outlook, copy the required properties from a template and call the Send method. It is up to you which way is to choose.

Finally, you may find the Getting Started with VBA in Outlook 2010 article helpful.