Run a script rule: Autoreply using a template

Chia sẻ bởi:hands
★★★★★
Quảng cáo

A security update disabled the Run a script option in the rules wizard in Outlook 2010 and all newer Outlook versions. See [URL='www.slipstick.com/outlook/rules/outlook-run-a-script-rules/']Run-a-Script Rules Missing in Outlook for more information and the registry key to fix restore it.
This macro will reply to the person who sent the message. It includes the original message, any text entered by in the macro, plus the content of the template.
Because this is a run a script rule, all messages meeting the condition will be replied to, unlike the autoreply rule which is one message per Outlook session.

If you want to include the original subject, use (or add) item.subject to the .Subject field, otherwise, this should have most of the bases covered and point you in the right direction for things I didn't include. Remove the parts you don't need and you should be good to go….

Keep in mind that the original message is Item, from Item As Outlook.MailItem. oRespond is the message you are sending back. You need to use the Item fields if you want to include fields from the original message in the response.

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

' Use this for a real reply
' Set oRespond = Item.Reply

' This sends a response back using a template
Set oRespond = Application.CreateItemFromTemplate("C:pathtotemplate.oft")

With oRespond
    .Recipients.Add Item.SenderEmailAddress
    .Subject = "Your Subject Goes Here"
    .HTMLBody = "Your reply text goes here." & vbCrLf & _
              "---- original body below ---" & vbCrLf & _
               Item.HTMLBody & vbCrLf & _
             "---- Template body below ---" & _
               vbCrLf & oRespond.HTMLBody

' 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
Set oRespond = Nothing
End Sub

Autoreply to Reply To Address, Not Sender

If you use a web form that comes from a generic address with a Reply to address set, you can use a simple macro to Reply to the message. Unlike the autoreply rule which replies to the sender, this reply is the same as hitting the Reply button yourself.

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

Set oRespond = Item.Reply

' use for testing
 oRespond.Display
 ' oRespond.Send

End Sub

Reply to a web form generated message

If you want to reply to a person who fills out a web form but the email that is generated is from a generic email address and their address is not in the Reply to field, you can use RegEx to get the address from the message body.

Sub SendNew(Item As Outlook.MailItem)
   Dim Reg1 As Object
    Dim M1 As Object
    Dim M As Object
    Dim strAddress As String

Set Reg1 = CreateObject("VBScript.RegExp")

With Reg1
       .Pattern = "(([w-.]*@[w-.]*)s*)"
       .IgnoreCase = True
       .Global = False
    End With

If Reg1.Test(Item.Body) Then

Set M1 = Reg1.Execute(Item.Body)
        For Each M In M1
           strAddress = M.SubMatches(1)
        Next
    End If

Dim objMsg As MailItem
 Set objMsg = Application.CreateItemFromTemplate("C:pathtotemplate.oft")

objMsg.Recipients.Add strAddress

' Copy the original message subject
 objMsg.Subject = "Thanks: " & Item.Subject

' use for testing
 objMsg.display

' objMsg.Send

End Sub

Reply with a template

When you want to reply with a template, you need to "pick up" information from the selected message. This macro works with an open or selected item and needs the GetCurrentItem function found [URL='www.slipstick.com/developer/outlook-vba-work-with-open-item-or-select-item/']here. If you will only ever use it with selected items (or open items) you can change the code to use current item or selection. More information is available at [URL='www.slipstick.com/developer/outlook-vba-work-with-open-item-or-select-item/']Outlook VBA: work with open item or selected item.

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

' need function fromhttps://slipstick.me/e8mio
Set Item = GetCurrentItem()

' This sends a response back using a template
Set oRespond = Application.CreateItemFromTemplate("C:Testtemplate.oft")

With oRespond
    .Recipients.Add Item.SenderEmailAddress
    .subject = "Your Subject Goes Here"
    .HTMLBody = oRespond.HTMLBody & vbCrLf & _
              "---- original message below ---" & vbCrLf & _
               Item.HTMLBody & vbCrLf

' 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
Set oRespond = Nothing
End Sub

Testing a Run a Script macro

To test the macros without sending yourself messages, select a message and run this macro. Don't forget to change YourMacroName to the macro name first.

Sub RunScript()
Dim objApp As Outlook.Application
Dim objItem As Object ' MailItem
Set objApp = Application
Set objItem = objApp.ActiveExplorer.Selection.Item(1)

'macro name you want to run goes here
YourMacroName objItem

End Sub

More Run a Script Samples:

  • [URL='www.slipstick.com/outlook/calendar/autoaccept-a-meeting-request-using-rules/']Autoaccept a Meeting Request using Rules
  • [URL='www.slipstick.com/outlook/rules/add-category-accepted-meetings/']Automatically Add a Category to Accepted Meetings
  • [URL='www.slipstick.com/outlook/email/blocking-new-top-level-domains/']Blocking Mail From New Top-Level Domains
  • [URL='www.slipstick.com/developer/macro-convert-rtf-messages-plain-text-html-format/']Convert RTF Messages to Plain Text Format
  • [URL='www.slipstick.com/outlook/rules/create-rule-to-expire-mail-after-a-number-of-days/']Create a rule to delete mail after a number of days
  • [URL='www.slipstick.com/outlook/rules/create-task-email-rule/']Create a Task from an Email using a Rule
  • [URL='www.slipstick.com/developer/code-samples/create-outlook-appointment-from-message/']Create an Outlook Appointment from a Message
  • [URL='www.slipstick.com/developer/code-samples/create-appointment-email-automatically/']Create Appointment From Email Automatically
  • [URL='www.slipstick.com/outlook/rules/delegates-meeting-requests-rules/']Delegates, Meeting Requests, and Rules
  • [URL='www.slipstick.com/developer/code-samples/delete-attachments-messages/']Delete attachments from messages
  • [URL='www.slipstick.com/developer/forward-meeting-details-to-another-address/']Forward meeting details to another address
  • [URL='www.slipstick.com/outlook/rules/change-font-outlooks-rss-feeds/']How to Change the Font used for Outlook's RSS Feeds
  • [URL='www.slipstick.com/outlook/email/process-mail-business-hours/']How to Process Mail After Business Hours
  • [URL='www.slipstick.com/outlook/calendar/meeting-cancellation-calendar/']Keep Canceled Meetings on Outlook's Calendar
  • [URL='www.slipstick.com/developer/print-attachments-as-they-arrive/']Macro to Print Outlook email attachments as they arrive
  • [URL='www.slipstick.com/developer/move-messages-cc-address/']Move messages CC'd to an address
  • [URL='www.slipstick.com/developer/code-samples/open-hyperlinks-email-message/']Open All Hyperlinks in an Outlook Email Message
  • [URL='www.slipstick.com/developer/fun-arrays-one-macro-many-replies/']Outlook AutoReplies: One Script, Many Responses
  • [URL='www.slipstick.com/outlook/rules/outlooks-rules-and-alerts-run-a-script/']Outlook's Rules and Alerts: Run a Script
  • [URL='www.slipstick.com/developer/process-messages-received-day-week/']Process messages received on a day of the week
  • [URL='www.slipstick.com/outlook/email/read-outlook-messages-plain-text/']Read Outlook Messages using Plain Text
  • [URL='www.slipstick.com/outlook/rules/run-script-rule-watch-updates/']Receive a Reminder When a Message Doesn't Arrive?
  • [URL='www.slipstick.com/developer/run-a-script-rule-autoreply-using-a-template/']Run a script rule: Autoreply using a template
  • [URL='www.slipstick.com/outlook/rules/run-script-rule-reply-message/']Run a script rule: Reply to a message
  • [URL='www.slipstick.com/outlook/rules/send-a-new-message-when-a-message-arrives/']Run a Script Rule: Send a New Message when a Message Arrives
  • [URL='www.slipstick.com/outlook/rules/run-rules-now-using-macro/']Run Rules Now using a Macro
  • [URL='www.slipstick.com/outlook/rules/outlook-2016-run-a-script-rules/']Run-a-Script Rules Missing in Outlook
  • [URL='www.slipstick.com/outlook/archive-outlook/save-incoming-messages-hard-drive/']Save all incoming messages to the hard drive
  • [URL='www.slipstick.com/developer/code-samples/save-rename-outlook-email-attachments/']Save and Rename Outlook Email Attachments
  • [URL='www.slipstick.com/developer/save-attachments-to-the-hard-drive/']Save Attachments to the Hard Drive
  • [URL='www.slipstick.com/developer/code-samples/save-outlook-email-pdf/']Save Outlook Email as a PDF
  • [URL='www.slipstick.com/outlook/email/sort-messages-sender-domain/']Sort messages by Sender domain
  • [URL='www.slipstick.com/developer/talking-reminders/']Talking Reminders
  • [URL='www.slipstick.com/outlook/rules/create-a-rule-with-wildcards/']To create a rule with wildcards
  • [URL='www.slipstick.com/developer/vba-copy-outlook-email-excel-workbook/']Use a Macro to Copy Data in an Email to Excel
  • [URL='www.slipstick.com/developer/delete-older-messages-new-message-arrives/']Use a Rule to delete older messages as new ones arrive
  • [URL='www.slipstick.com/developer/code-samples/use-a-run-a-script-rule-to-mark-messages-read/']Use a run a script rule to mark messages read
  • [URL='www.slipstick.com/developer/code-samples/use-vba-move-messages-based-values-fields/']Use VBA to move messages with attachments

www.slipstick.com/developer/run-a-script-rule-autoreply-using-a-template/

Khoá học Trưởng phòng nhân sự
Khóa học SprinGO phù hợp

Khoá học Trưởng phòng nhân sự

Nguồn nhân lực là một trong Tứ trụ kinh doanh của doanh nghiệp, có tác động tới sự tồn tại và phát triển bền...

Xem khóa học
★★★★★ 5 ★ 1 👤 30 ▥ 0
Quảng cáo

Bạn nên đọc

Leave a Reply

Your email address will not be published. Required fields are marked *

Quảng cáo

Cũ vẫn chất

Xem thêm