Computer >> คอมพิวเตอร์ >  >> ซอฟต์แวร์ >> Office

วิธีการส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่

บทความนี้จะแสดงวิธีที่ดีที่สุดในการ ส่งอีเมลโดยอัตโนมัติ จาก Excel ตาม วันที่ . โดยปกติ การส่งอีเมล . เป็นเรื่องง่าย จาก Outlook หรือ Gmail แต่เมื่อคุณต้องการส่ง อีเมล ในช่วงเวลาหนึ่งหรือวันที่ คุณสามารถใช้ Microsoft Excel ได้อย่างมีประสิทธิภาพ

ในชุดข้อมูล เรามี ที่อยู่อีเมล (ทั้งหมดนี้เป็นที่อยู่จำลอง ยกเว้น admin@wsxdn.com ซึ่งเป็นของฉัน ฉันใช้ที่อยู่ของฉันเพื่อแสดงให้คุณเห็นว่าวิธีการทำงานอย่างไร

วิธีการส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่

3 วิธีในการส่งอีเมลอัตโนมัติจาก Excel ตามวันที่

1. การใช้ Excel VBA เพื่อส่งอีเมลโดยอัตโนมัติตามวันที่กำหนด

เราสามารถส่ง อีเมลโดยอัตโนมัติ โดยใช้ Microsoft Visual Basic สำหรับแอปพลิเคชัน (VBA ) ตาม วันที่ . คุณสามารถสร้างรายการ วันที่ เมื่อคุณต้องการส่ง อีเมล ไปยังผู้รับที่เกี่ยวข้อง ทำตามขั้นตอนด้านล่างเพื่อความเข้าใจที่ดีขึ้น ในส่วนนี้ เราจะทำงานกับ วันที่ . เท่านั้น .

ขั้นตอน:

  • ขั้นแรก ไปที่ นักพัฒนา >> Visual Basic 

วิธีการส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่

  • จากนั้น หน้าต่าง VBA จะเปิด เลือก แทรก >> โมดูล
  • วิธีการส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่พิมพ์รหัสต่อไปนี้ใน โมดูล VBA .
Sub SendEmail01()
Dim Range_Select As Range
Dim Date_Range As Range
Dim Cell_Address As String
Dim Subject, Email_From, Email_To, Cc, Bcc, Email_Text As String
Dim Email_Obj, Single_Mail As Object
On Error Resume Next
Cell_Address = ActiveWindow.RangeSelection.Address
Set Range_Select = Application.InputBox("Select a range:", _
"Message Box", Cell_Address, , , , , 8)
If Range_Select Is Nothing Then Exit Sub
For Each Date_Range In Range_Select
If Date_Range.Value = Date Then
Subject = Application.InputBox("Subject: ", "Message", , , , , , 2)
Email_From = Application.InputBox("Send from: ", "Message Box", , , , , , 2)
Email_To = Application.InputBox("Send to: ", "Message Box", , , , , , 2)
If Email_To = "" Then Exit Sub
Cc = Application.InputBox("CC: ", "Message Box", , , , , , 2)
Bcc = Application.InputBox("BCC: ", "Message Box", , , , , , 2)
Email_Text = Application.InputBox("Message Body: ", "Message Box", , , , , , 2)
Set Email_Obj = CreateObject("Outlook.Application")
Set Single_Mail = Email_Obj.CreateItem(0)
With Single_Mail
.Subject = Subject
.To = Email_To
.Cc = Cc
.Bcc = Bcc
.Body = Email_Text
.send
End With
End If
Next
End Sub

วิธีการส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่

คำอธิบายโค้ด

  • ขั้นแรก เราได้ประกาศตัวแปรที่จำเป็นบางอย่าง:Range_Select และ Date_Range เป็น ช่วง; เซลล์_ที่อยู่ , เรื่อง , Email_From , Email_To , สำเนา , สำเนาลับ , Email_Text เป็น สตริง; Email_Obj และ Single_Mail เป็น วัตถุ .
  • จากนั้นเราตั้งค่า Range_Select ไปยัง InputBox โดยสามารถเลือก ช่วง ของเซลล์ผ่าน กล่องข้อความ .
  • หลังจากนั้น เราใช้ Value ในคำสั่ง VBA IF และยังตั้งค่า เรื่อง , Email_From และ Email_To ไปยัง InputBox .
  • หากไม่มี ที่อยู่อีเมล (Email_To=“” ) ขั้นตอนย่อย จะออก . มิฉะนั้น เราจะใส่ตัวแปรเพิ่มเติมใน คำสั่ง IF . ใหม่ . สำเนา และ สำเนาลับ อ้างถึง สำเนาคาร์บอน และ สำเนาตาบอด ของ อีเมล ที่เราอยากส่งต่อให้คนอื่น
  • จากนั้นเราสร้าง Outlook Application Object ซึ่งเรากำหนดโดย Email_Obj . นอกจากนี้เรายังใช้ Single_Mail เพื่อ สร้าง รายการ สำหรับ Email_Obj .
  • หลังจากนั้น เราจะกำหนด เรื่อง , Email_To , สำเนา , สำเนาลับ , Email_Text เป็นส่วนที่จำเป็นของ อีเมล โดย พร้อมคำชี้แจง . นอกจากนี้เรายังใส่ .Send คำสั่งในคำสั่งนี้

  • ตอนนี้ กลับไปที่ชีตของคุณและเรียกใช้ มาโคร ชื่อ SendEmail01 เนื่องจากเป็นชื่อของ มาโคร . ปัจจุบันของคุณ .

วิธีการส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่

  • หลังจากดำเนินการนี้ คุณจะเห็น กล่องข้อความ ป๊อปอัปแจ้งให้คุณเลือกช่วงวันที่ . เลือก ช่วง และคลิก ตกลง .

วิธีการส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่

  • จากนั้นอีก กล่องข้อความ จะปรากฏขึ้น พิมพ์หัวเรื่องแล้วคลิก ตกลง .

วิธีการส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่

  • คุณจะเห็นห่วงโซ่ของ กล่องข้อความ แจ้งให้คุณใส่ข้อมูลที่จำเป็นในการส่ง อีเมล . เพียงทำตามภาพด้านล่าง
  • ใส่ที่อยู่อีเมล .ของคุณ .

วิธีการส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่

  • พิมพ์ ที่อยู่อีเมล ที่คุณจะส่ง อีเมล . ของคุณ .

วิธีการส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่

  • แทรก CC ที่อยู่ของผู้รับ .

วิธีการส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่

  • ใส่ BCC ผู้รับใน กล่องข้อความ ถ้าคุณต้องการ. ฉันไม่ได้เลือกเลย

วิธีการส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่

  • พิมพ์ข้อความของคุณ

วิธีการส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่

  • หลังจบ กล่องข้อความ คุณอาจเห็น กล่องคำเตือน จาก Microsoft Outlook . คลิก อนุญาต .

วิธีการส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่

การดำเนินการนี้จะส่ง อีเมล ไปยังที่อยู่ที่คุณใส่ในกล่องข้อความที่เกี่ยวข้อง .
นี่คือ อีเมล main หลัก .

วิธีการส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่

และด้านล่างนี้คือ สำเนาคาร์บอน ของ อีเมล .

วิธีการส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่

ดังนั้นคุณสามารถ ส่ง . ได้โดยอัตโนมัติ อีเมล จาก Excel ตาม วันที่ . 

อ่านเพิ่มเติม:มาโคร Excel เพื่อส่งอีเมลโดยอัตโนมัติ (ตัวอย่างที่เหมาะสม 3 ตัวอย่าง)

2. การส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่ต่อไปนี้

In this section, we will send Emails based on the following or future dates . Suppose you want to send someone an Email prior to 3 days or 7 days. Let’s go through the following discussion on this matter.

ขั้นตอน:

  • First, go to Section 1 to see how to open a VBA Module .
  • Then type the following code in the VBA Module .
Public Sub SendEmail02()
Dim Date_Range As Range
Dim Mail_Recipient As Range
Dim Email_Text As Range
Dim Outlook_App_Create As Object
Dim Mail_Item As Object
Dim Last_Row As Long
Dim VB_CR_LF, Email_Body, Date_Range_Value, Send_Value, Subject As String
Dim i As Long
On Error Resume Next
Set Date_Range = Application.InputBox("Please choose the date range:", "Message Box", , , , , , 8)
If Date_Range Is Nothing Then Exit Sub
Set Mail_Recipient = Application.InputBox("Please select the Email addresses:", "Message Box", , , , , , 8)
If Mail_Recipient Is Nothing Then Exit Sub
Set Email_Text = Application.InputBox("Select the Email Text:", "Message Box", , , , , , 8)
If Email_Text Is Nothing Then Exit Sub
Last_Row = Date_Range.Rows.Count
Set Date_Range = Date_Range(1)
Set Mail_Recipient = Mail_Recipient(1)
Set Email_Text = Email_Text(1)
Set Outlook_App_Create = CreateObject("Outlook.Application")
For i = 1 To Last_Row
Date_Range_Value = ""
Date_Range_Value = Date_Range.Offset(i - 1).Value
If Date_Range_Value <> "" Then
If CDate(Date_Range_Value) - Date <= 7 And CDate(Date_Range_Value) - Date > 0 Then
Send_Value = Mail_Recipient.Offset(i - 1).Value
Subject = Email_Text.Offset(i - 1).Value & " on " & Date_Range_Value
VB_CR_LF = "<br><br>"
Email_Body = "<HTML><BODY>"
Email_Body = Email_Body & "Dear " & Send_Value & VB_CR_LF
Email_Body = Email_Body & "Text : " & Email_Text.Offset(i - 1).Value & VB_CR_LF
Email_Body = Email_Body & "</BODY></HTML>"
Set Mail_Item = Outlook_App_Create.CreateItem(0)
With Mail_Item
.Subject = Subject
.To = Send_Value
.HTMLBody = Email_Body
.Display
End With
Set Mail_Item = Nothing
End If
End If
Next
Set Outlook_App_Create = Nothing
End Sub

วิธีการส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่

Code Explanation

  • First, we declared some necessary variables:Date_Range , Mail_Recipient and Email_Text เป็น ช่วง; Outlook_App_Create and Mail_Item as Object; Last_Row and i เป็น ยาว; VB_CR_LF (Visual Basic Carriage Return Line Feed ), Email_Body , Date_Range_Value , Send_Value , Subject as String .
  • Then we set Date_Range to an InputBox where it can select a range จาก วันที่ via a Message Box . An If Statement is used to terminate Sub Procedure if the Date_Range is Nothing .
  • We also set Mail_Recipient and Email_Text to InputBox
  • After that, we create an Outlook Application Object which we define by Outlook_App_Create .
  • A date interval is introduced. In this code, the duration is 7 . Suppose you want someone to be reminded about an event or greetings 7 days after today which is 19th May . If the Emails you want to send are within the dates between 20th to 26th May , the recipient will receive the Email . Otherwise, it will not reach.
  • Then we put some commands to format the Email_Body . And also define the necessary parts of the Email by the With Statement .
  • We used the .Display command here so that Outlook will open these Email drafts and we can check if any other messages need to be sent. You can use the .Send command if you don’t want to see the message drafts. But do not use these two commands simultaneously .

  • Now, go back to your sheet and run the Macro named SendEmail02 as it is the name of your current Macro .

วิธีการส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่

  • After executing this operation, you will see a message box pop up telling you to select the date range . Select the range และคลิก ตกลง .

วิธีการส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่

  • Then, another message box will pop up and it will tell you to select the Email address range . Select the range และคลิก ตกลง .

วิธีการส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่

  • After that, select the range of text messages in the message box และคลิก ตกลง .

วิธีการส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่

  • You will see the Email drafts for 21st May to 24th May . Here, the ID admin@wsxdn.com is an actual Email ID , so we sent the corresponding Email to this address to show you the example. You can put a CC recipient if you want.

วิธีการส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่

  • Let’s check my Emails . This can be sent to Spam box too. So check everywhere.

วิธีการส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่

Thus you can automatically send email based on following dates

อ่านเพิ่มเติม: Send Reminder Email Automatically from an Excel Worksheet Using VBA

การอ่านที่คล้ายกัน

  • How to See Who Is in a Shared Excel File (With Quick Steps)
  • Enable Share Workbook in Excel
  • How to Share Excel File for Multiple Users
  • Send Bulk Email from Outlook Using Excel (3 Ways)
  • How to Apply Macro to Send Email from Excel with Attachment

3. Automatically Sending Email to a Single Address Based on a Date

We can also send an Email automatically single address based on a date . You can make a list of dates when you want to send the Emails to their corresponding recipients. Let’s go through the process below for a better understanding. In this section, we will only work with the dates .

ขั้นตอน:

  • First, go to Section 1 to see how to open a VBA Module .
  • พิมพ์รหัสต่อไปนี้ใน โมดูล VBA .
Option Explicit
Sub SendEmail03()
Dim Date_Range As Range
Dim rng As Range
Set Date_Range = Range("B5:B10")
For Each rng In Date_Range
If rng.Value = Date Then
Dim Subject, Send_From, Send_To, _
Cc, Bcc, Body As String
Dim Email_Obj, Single_Mail As Variant
Subject = "Hello there!"
Send_From = "admin@wsxdn.com"
Send_To = "admin@wsxdn.com"
Cc = "admin@wsxdn.com"
Bcc = ""
Body = "Hope you are enjoying the article"
On Error GoTo debugs
Set Email_Obj = CreateObject("Outlook.Application")
Set Single_Mail = Email_Obj.CreateItem(0)
With Single_Mail
.Subject = Subject
.To = Send_To
.Cc = Cc
.Bcc = Bcc
.Body = Body
.send
End With
End If
Next
Exit Sub
debugs:
If Err.Description <> "" Then MsgBox Err.Description
End Sub

วิธีการส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่

Code Explanation

  • First, we declared some necessary variables:rng and Date_Range เป็น ช่วง .
  • Then we define the range for Date_Range .
  • After that, we use Value in a VBA IF Statement and also set Subject , Send_From , Send_To , Cc , Bcc and Body to String . Also, we set Email_Obj and Single_Mail as Variant .
  • Then we set the text value for Subject , Send_From , Send_To , Cc and Bcc .
  • Later, we create an Outlook Application Object which we define by Email_Obj . Also, we use Single_Mail to create an item for Email_Obj .
  • After that, we define Subject , Send_To , Cc , Bcc , Body as the necessary parts of an Email by a With Statement . We also put the .Send command in this statement.

  • Now, go back to your sheet and run the Macro named SendEmail01 as it is the name of your current Macro .

วิธีการส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่

  • After that, you may see a warning box from Microsoft Outlook . คลิก อนุญาต .

วิธีการส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่

This operation will send the Email to the addresses that you put in the corresponding message boxes .

This is the main Email .

วิธีการส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่

And below here is the Carbon Copy of the Email .

วิธีการส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่

Thus you can automatically send an Email from Excel based on the date . 

อ่านเพิ่มเติม: How to Send Email Automatically When Condition Met in Excel

ภาคปฏิบัติ

In this section, I’m giving you the dataset that we used in this article so that you can practice on your own.

วิธีการส่งอีเมลโดยอัตโนมัติจาก Excel ตามวันที่

สิ่งที่ควรจำ

Always open and log into your Outlook  account to run the VBA  codes in this article.

บทสรุป

The bottom line is, that you will learn effective methods on how to automatically send Email from Excel based on date . If you have any better ideas or feedback, please share them in the comment box. This will help me enrich my upcoming articles.

บทความที่เกี่ยวข้อง

  • [Solved]:Share Workbook Not Showing in Excel (with Easy Steps)
  • How to Send Email from Excel List (2 Effective Ways)
  • How to Send an Editable Excel Spreadsheet by Email (3 Quick Methods)
  • Macro to Send Email from Excel (5 Suitable Examples)
  • Macro to Send Email from Excel with Body (3 Useful Cases)
  • Excel Macro:Send Email to an Address in Cell (2 Easy Ways)