【问题标题】:Create an email containing the contents of specific cells when another cell has a certain value当另一个单元格具有特定值时,创建包含特定单元格内容的电子邮件
【发布时间】:2022-07-26 03:28:52
【问题描述】:

我正在尝试创建一封电子邮件,用于汇总尚未订购的商品。这个宏将被分配给一个按钮。

我有一个用于请求和跟踪供应订单请求的 Excel 表。
表格的每一行有四个单元格,包含项目信息(供应商、项目描述、零件编号、数量),这些信息由请求订单的人填写。
另一个单元格由订购商品的人填写订购日期。

工作表名称为“Order and Chemical Log”。
包含信息的表称为 Table8。
表格标题位于工作表的第 10 行。
包含项目信息的单元格位于 F、G、H 和 I 列中。
K 列中有一个指示单元格,其中包含“”、“已订购”或“未订购”,这取决于项目信息是否已添加到该行以及单元格中是否存在用于将该项目标记为已订购的日期。

我希望电子邮件的正文包含一个文本字符串(“以下项目尚未订购:”)和包含项目信息的每个项目的新行(来自 F 列中的单元格的值通过 I) 用于未订购的商品(K 列单元格中的值为“未订购”)。

生成电子邮件以告诉某人有要订购的物品的代码:

Sub SendOrderEmail()
Dim EmailApp As Outlook.Application 'Refers to outlook application
Set EmailApp = New Outlook.Application 'Launches outlook
Dim EmailItem As Outlook.MailItem 'Refers to a new Outlook email
Set EmailItem = EmailApp.CreateItem(olMailItem) 'Lauches a new outlook email

On Error Resume Next
With EmailItem
    .To = "test@test.com"
    .Subject = "New Item(s) Added to Order Log"
    .HTMLBody = "Hi, <br> <br>" & vbNewLine & vbNewLine & "I have added new items to the order log to be ordered today.<br><br>" & "Thank you, <br><br>""<br> Email Generated by VBA from the Order Log"
.Display
End With

End Sub

【问题讨论】:

  • 抱歉,我应该将标题修改为“当另一个单元格包含特定值时(“未排序”)”,而不是“当它为空白时”。
  • 有一个可以做的事情的列表,例如,将每一个写入一个数组,为每个附加的顺序创建一个字符串,或者直接复制行.其中,如果数据很简单,那么单字符串方法将是最简单的。您可以在遍历电子表格中的单元格时附加到字符串,添加逗号/分隔符。然后,您可以获取可以在您的电子邮件子例程中使用的字符串(我通常会将其设为全局变量)。
  • 我明白了。我从逻辑上理解你在这里所说的。不幸的是,我不知道如何实现这一点,因为我对 VBA 很陌生,但是,我会看看我能做什么。谢谢!

标签: excel vba outlook


【解决方案1】:

对于一个简单的表格,将 html 逐行构建为字符串。

Option Explicit

Sub SendOrderEmail()

    Dim EmailApp As Outlook.Application 'Refers to outlook application
    Set EmailApp = New Outlook.Application 'Launches outlook
    Dim EmailItem As Outlook.MailItem 'Refers to a new Outlook email
    Set EmailItem = EmailApp.CreateItem(olMailItem) 'Lauches a new outlook email
    
    Dim html As String
    html = getHtml
    If html = "" Then
        MsgBox "No 'not ordered' items found", vbExclamation
        Exit Sub
    End If
    'Debug.Print html
    
    On Error Resume Next
    With EmailItem
        .To = "test@test.com"
        .Subject = "New Item(s) Added to Order Log"
        .HTMLBody = getHtml
        .Display
    End With

End Sub

Function getHtml() As String

    Dim tbl As ListObject, n As Long
    Dim h As String, r As ListRow, c As Range
    
    With Sheets("Order and Chemical Log")
        Set tbl = .ListObjects("Table8")
    End With
    
    h = "<table style=""font:12px Verdana"" border=""1"" cellspacing=""0"" cellpadding=""3"">" & _
        "<tr style=""background-color:#f0f0d0;text-align:center;""><th>Vendor</th><th>Item Description</th>" & _
        "<th>Part Number</th><th>Quantity</th></tr>" & vbLf
    
    Dim i As Long, status As String
    With tbl
        i = .ListColumns("Vendor").Index ' vendor column
        For Each r In .ListRows
            status = LCase(Trim(r.Range(, i + 5))) ' 5 columns along from vendor
            If status = "not ordered" Then
                h = h & "<tr style=""text-align:center"">"
                For Each c In r.Range.Offset(, i - 1).Resize(, 4)
                   h = h & "<td>" & c.Value & "</td>"
                Next
                h = h & "</tr>" & vbLf
                n = n + 1
            Else
                Debug.Print r.Index, "'" & status & "'"
            End If
        Next
    End With
    h = h & "</table>"
    
    If n = 0 Then Exit Function ' no items
    
    getHtml = "Hi, <br/> <br/>" & _
        "I have added these items to the order log to be ordered today.<br/><br/>" & _
        vbLf & h & "<br/><br/>Thank you, <br/><br/> Email Generated by VBA from the Order Log"

End Function

【讨论】:

  • 谢谢!我试过这个。最初它根本没有显示电子邮件。我将 SendOrderEmail 子中的 If html = "" Then 更改为 If html = "Not Ordered" Then。现在它会显示一封带有正确收件人和主题字段的新电子邮件,但是 getHtml 函数不会提取任何数据,并且新电子邮件中没有正文。
  • @ESmS 您的表格是否有超过 4 列?我已经修改了代码以允许这样做。将行改回If html = "" Then。如果仍然没有 html 检查来自 Debug.Print r.Index, "'" &amp; status &amp; "'" 的输出的即时窗口
  • 是的,我的表比 4 列更复杂。这很好用!感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2018-09-11
  • 2021-02-22
  • 1970-01-01
  • 2019-03-19
  • 2018-04-22
  • 1970-01-01
  • 1970-01-01
  • 2023-01-22
相关资源
最近更新 更多