Tôi nhận được nhiệm vụ gửi email đến người dùng được liệt kê trong một bảng tính Excel. Mỗi email phải chứa một số thông tin riêng cho từng người dùng. Tôi đã thử làm điều này bằng macro VBA trong Excel gửi email từ hồ sơ Outlook đã cấu hình trên máy tính. Đây là giải pháp của tôi.
Giả sử bạn có file Excel với các cột sau:
Email | Full Name | Last Password Change Date | Account status

Nhiệm vụ của tôi là gửi email như mẫu này đến từng người dùng trong danh sách:
Subject: Your account status on woshub.com domain
Body: Dear %FullUsername%,
Your account in woshub.com domain is in %status% state
The date and time of the last password change is %pwdchange%
Tạo macro mới: View -> Macros. Chỉ định tên macro (send_email) và nhấn Create:

Sao chép và dán mã sau vào trình soạn thảo VBA xuất hiện (tôi đã thêm chú thích liên quan). Để tự động hóa việc gửi email, tôi sẽ sử dụng hàm CreateObject ("Outlook.Application") cho phép tạo và sử dụng đối tượng Outlook bên trong script VBA.
Quan trọng. Hồ sơ Outlook phải được cấu hình trên máy tính gửi. Hộp thư này (và địa chỉ email) sẽ được dùng để gửi email.
Sub send_email()
Dim olApp As Object
Dim olMailItm As Object
Dim iCounter As Integer
Dim Dest As Variant
Dim SDest As String
' Subject
strSubj = "Your account status on woshub.com domain"
On Error GoTo dbg
' Create a new Outlook object
Set olApp = CreateObject("Outlook.Application")
For iCounter = 1 To WorksheetFunction.CountA(Columns(1))
' Create a new item (email) in Outlook
Set olMailItm = olApp.CreateItem(0)
strBody = ""
useremail = Cells(iCounter, 1).Value
FullUsername = Cells(iCounter, 2).Value
Status = Cells(iCounter, 4).Value
pwdchange = Cells(iCounter, 3).Value
'Make the body of an email
strBody = "Dear " & FullUsername & vbCrLf
strBody = strBody & " Your account in woshub.com domain is in" & Status & “ state” & vbCrLf
strBody = strBody & "The date and time of the last password change is" & pwdchange & vbCrLf
olMailItm.To = useremail
olMailItm.Subject = strSubj
olMailItm.BodyFormat = 1
' 1 – text format of an email, 2 - HTML format
olMailItm.Body = strBody
olMailItm.Send
Set olMailItm = Nothing
Next iCounter
Set olApp = Nothing
dbg:
'Display errors, if any
If Err.Description <> "" Then MsgBox Err.Description
End Sub

Lưu file Excel này dưới dạng .xlsm (định dạng workbook Excel hỗ trợ macro). Để gửi email, chọn thủ tục đã tạo (macro) và nhấn Run.

Macro sẽ duyệt qua tất cả các hàng trên worksheet Excel lần lượt, tạo và gửi email đến từng người nhận trong danh sách.