简体   繁体   English

VBA 从 Excel 发送电子邮件

[英]VBA send emails from Excel

I have a simple Table in Excel.我在 Excel 中有一个简单的表。 It is built as follows:它的构建如下:

|Information on food|
|date: April 28th, 2021|
|Person|Email|Apples|Bananas|Bread|
|------|-----|------|-------|-----|
|Person_A|person_A@mailme.com|3|8|9|
|Person_B|person_B@mailme.com|10|59|11|
|Person _C|person_C@maime.com|98|12|20|

There is also a date field in the table.表中还有一个日期字段。 For a test, this could be set to todays date.对于测试,这可以设置为今天的日期。 Based on this information, I am looking for a VBA code which prepares an email to each of the listed persons and is telling them what they have eaten on the specific date.基于这些信息,我正在寻找一个 VBA 代码,它为每个列出的人准备一个 email 并告诉他们他们在特定日期吃了什么。 So I need to access several fields in the table, and at the same time loop through the emails.所以我需要访问表中的几个字段,同时循环浏览电子邮件。 Then I would like VBA to open Outlook and prepare the Emails.然后我想 VBA 打开 Outlook 并准备电子邮件。 Ideally not send them yet, so I can take a final look before I send the mails.理想情况下还没有发送它们,所以我可以在发送邮件之前进行最后的查看。 It would be fine to access certain cells specifically via ranges etc. I am using Excel/Outlook 2016 if that matters.可以通过范围等专门访问某些单元格。如果这很重要,我正在使用 Excel/Outlook 2016。 The question is how this can be achieved in VBA?问题是如何在 VBA 中实现这一点?

Assuming the data is a named table and title/date are above the corner of the table as shown in your example.假设数据是一个命名表,并且标题/日期在表的角落上方,如您的示例所示。 Also all the rows of the table have valid data.此外,表的所有行都有有效数据。 The emails are prepared and shown but not sent (unless you change the code where shown).电子邮件已准备好并显示但未发送(除非您更改显示的代码)。

Option Explicit

Sub EmailMenu()

    Const TBL_NAME = "Table1"
    Const CSS = "body{font:12px Verdana};h1{font:14px Verdana Bold};"

    Dim emails As Object, k
    Set emails = CreateObject("Scripting.Dictionary")

    Dim ws As Worksheet, rng As Range
    Dim sName As String, sAddress As String
    Dim r As Long, c As Integer, s As String, msg As String
    Dim sTitle As String, sDate As String

    Set ws = ThisWorkbook.Sheets("Sheet1")
    Set rng = ws.ListObjects(TBL_NAME).Range
    sTitle = rng.Cells(-1, 1)
    sDate = rng.Cells(0, 1)
        
    ' prepare emails
    For r = 2 To rng.Rows.Count

        sName = rng.Cells(r, 1)
        sAddress = rng.Cells(r, 2)
        If InStr(sAddress, "@") = 0 Then
            MsgBox "Invalid Email: '" & sAddress & "'", vbCritical, "Error Row " & r
            Exit Sub
        End If

        s = "<style>" & CSS & "</style><h1>" & sDate & "<br>" & sName & "</h1>"
        s = s & "<table border=""1"" cellspacing=""0"" cellpadding=""5"">" & _
                "<tr bgcolor=""#ddddff""><th>Item</th><th>Qu.</th></tr>"
        For c = 3 To rng.Columns.Count
            s = s & "<tr><td>" & rng.Cells(1, c) & _
                    "</td><td>" & rng.Cells(r, c) & _
                    "</td></tr>" & vbCrLf
        Next
        s = s & "</table>"
        ' add to dictonary
        emails.Add sAddress, Array(sName, sDate, s)
    Next

    ' confirm
    msg = "Do you want to send " & emails.Count & " emails ?"
    If MsgBox(msg, vbYesNo) = vbNo Then Exit Sub

    ' send emails
    Dim oApp As Object, oMail As Object, ar
    Set oApp = CreateObject("Outlook.Application")
    For Each k In emails.keys
        ar = emails(k)
        Set oMail = oApp.CreateItem(0)
        With oMail
            .To = CStr(k)
            '.CC = "email@test.com"
            .Subject = sTitle
            .HTMLBody = ar(2)
            .display ' or .send
        End With
    Next
    oApp.Quit
    
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM