首页 > 解决方案 > Excel VBA - 如何发送包含特定单元格值特定文本的电子邮件?

问题描述

目前在 Excel 中处理订单,我一直坚持通过宏按钮发送电子邮件。

现在我在网上看到了很多“如何在 Excel 中使用 VBA 发送电子邮件”,我明白他们在说什么。

我的问题是我希望电子邮件正文不仅显示电子邮件的主体,而且我希望能够在一个单元格中输入一个随机数值,然后宏将其拾取并转储此单元格值以及特定于该单元格的文本细胞。

例如,订购单有 4 个我可以订购的电路板。现在这些将由 A、B、C 和 D 代表

如果我在每个电路板的数量单元格中放入 100,然后单击“订购”按钮。

以下电子邮件如下:

Dear <Name>

I'd like to order 100 of A, 100 of B, 100 of C & 100 of D

Kind Regards,

<Name>

同时,如果突然在我要下的下一个订单中,我只在 A 中放了 20 个,在 C 中放了 60 个,电子邮件将更改以反映此更改:

Dear <Name>

I'd like to order 20 of A & 60 of C

Kind Regards,

<Name>

这些更改是逗号、句号和 et 符号“&”的位置。

谁能指出我如何做到这一点/调整当前教程以获得我需要的结果的大致方向?

编辑:似乎我的表格有一点误解,所以这是当前的设置: 在此处输入图像描述

我想通过按下订单按钮来实现:从对应于类型列中每个单独板的金额列中获取单元格值。

并将这些值纳入电子邮件的主体。关于逗号等特定文本。我认为更像是 if 语句格式,例如如果 B3 有值 && B4 有值但 B5 和 B6 == 0 然后在电子邮件正文中发布 (B3)"(ref no) "+" & "+ (B4)"(参考号)"

等等基于这些单元格中是否有任何值。我不是要求将文本放置在其他单元格中,例如包含“我想要”的单元格 J7 等。我将直接添加到 VBA 脚本中的那种文本类型。

@BBRK-关于我对您的回答的评论,这就是我更改消息正文的意思:

  xMailBody = "Hi <NAME>" & vbNewLine & vbNewLine & _
              "I'd like to order" & vbNewLine & _
              "Kind Regards"
                  On Error Resume Next

此代码显示如下: 在此处输入图像描述

实际上,我希望更改 A 列中引用的文本,并将其替换为硬编码的参考编号,该编号对应于我与供应商的订单。

此外,我希望更改“0 of Vibro”的位置,并将其移至在我说我想订购但不显示在当前位于“亲切的问候”部分旁边的下一行。

@BBRK 这里是当前完整的 VBA:

Private Sub Board_Order()

    Dim xMailBody As String
    Dim xOutApp As Object
    Dim xOutMail As Object
    Dim count_var As Integer
    Dim arr() As String
    Dim arr_val() As Integer
    Dim great_count As Integer
    Dim arr_now As Integer
    On Error Resume Next
    Set xOutApp = CreateObject("Outlook.Application")
    Set xOutMail = xOutApp.CreateItem(0)

    arr_now = 0

    'Gets the count of number of zeroes available in the B3 to last data filled row of that column.
    great_count = Application.WorksheetFunction.CountIf(Range("B3:B6" & Range("B3").End(Excel.XlDirection.xlDown).Row), ">0")

    xMailBody = "Hi <NAME>" & vbNewLine & vbNewLine & _
                "I'd like to order" & vbNewLine & _
                "Kind Regards"
                    On Error Resume Next

    'If orders are there then will go for further processing.
    If great_count <> 0 Then

        'Resizes array according to the count
        ReDim arr(0 To great_count - 1)
        ReDim arr_val(0 To great_count - 1)

        'Loops through the range and input product into the arr and it's value in arr_val
        If great_count > 0 Then
            For i = 3 To Range("B3").End(Excel.XlDirection.xlDown).Row
                If Range("B" & i).Value > 0 Then
                    arr(arr_now) = Range("A" & i).Value
                    arr_val(arr_now) = Range("B" & i).Value
                    arr_now = arr_now + 1
                End If
            Next i
        End If

        'Looping through each element in the array to get the desired result.
            If great_count = 1 Then
                xMailBody = xMailBody + " " + CStr(arr_val(j)) + " of " + arr(j)
            Else
                For j = 0 To UBound(arr)
                    If j = 0 Then
                        xMailBody = xMailBody + " " + CStr(arr_val(j)) + " of " + arr(j)
                    ElseIf j = UBound(arr) Then
                        xMailBody = xMailBody + " & " + CStr(arr_val(j)) + " of " + arr(j)
                    Else
                        xMailBody = xMailBody + " , " + CStr(arr_val(j)) + " of " + arr(j)
                    End If
                Next j
            End If
    End If

    With xOutMail
            .To = "marcbrooks991@hotmail.com"
            .CC = ""
            .BCC = ""
            .Subject = "Test email send by button clicking"
            .Body = xMailBody
            .Display   'or use .Send
    End With
    On Error GoTo 0
    Set xOutMail = Nothing
    Set xOutApp = Nothing

End Sub

标签: excelvbaemailref

解决方案


好吧,您可以将文本和数字组合在一起以获得您想要的内容:

=A1&" "&A2&" "&A3

其中单元格 A1 包含“我想要”单元格 A2 包含值 100 单元格 A3 包含“可变电阻器”

结果是:我想要 100 个可变电阻器


推荐阅读