【问题标题】:Excel VBA - How can I send email with text specific to certain cell values?Excel VBA - 如何发送包含特定单元格值特定文本的电子邮件?
【发布时间】:2019-03-19 06:22:48
【问题描述】:

目前正在处理 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)"(ref no)"

等等取决于这些单元格中是否有任何值。 我不要求将文本放置在其他单元格中,例如包含“我想要”的单元格 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

【问题讨论】:

  • 您需要考虑很多事情。 1 如果 qty 单元格为空怎么办? 2 有多少商品可以订购? 3 您有一个设定范围,然后循环遍历范围并根据您在 qty 字段中的内容连接值将有所帮助。
  • 理想情况下,如果您的项目列表可以增加,那么垂直排列它们会有所帮助。 ItemsCol AQtyCol B。这样你就可以遍历范围内的每个项目。

标签: excel vba email ref


【解决方案1】:

根据您的解释,我在 Excel 中开发了一个 VBA 方法。

这将帮助您生成所需的连接字符串。

在编辑器中复制并粘贴下面的 Excel VBA 代码并尝试运行它。

Sub Generate_String()
Dim HTML_body As String
Dim count_var As Integer
Dim arr() As String
Dim arr_val() As Integer
Dim great_count As Integer
Dim arr_now As Integer
Dim xMailBody As String
Dim xOutApp As Object
Dim xOutMail As Object

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:B" & Range("B3").End(Excel.XlDirection.xlDown).Row), ">0")

HTML_body = "Hi <Name>," & vbnewline & vbnewline & "I'd like to order"

'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
            HTML_body = HTML_body + " " + CStr(arr_val(j)) + " of " + arr(j)
        Else
            For j = 0 To UBound(arr)
                If j = 0 Then
                    HTML_body = HTML_body + " " + CStr(arr_val(j)) + " of " + arr(j)
                ElseIf j = UBound(arr) Then
                    HTML_body = HTML_body + " & " + CStr(arr_val(j)) + " of " + arr(j)
                Else
                    HTML_body = HTML_body + " , " + CStr(arr_val(j)) + " of " + arr(j)
                End If
            Next j
        End If
Else
    HTML_body = "No Orders"
End If

HTML_body = HTML_body & vbnewline & "Kind Regards" & vbnewline & "<Name>"
With xOutMail
        .To = "marcbrooks991@hotmail.com"
        .CC = ""
        .BCC = ""
        .Subject = "Test email send by button clicking"
        .Body = HTML_body
        .Display   'or use .Send
End With
End Sub

根据您的需要修改它。 希望我能帮到你。

我已根据您的需要编辑了代码。问题是您在处理之前添加了“Kind Regards”文本。

EDIT2:

通过验证检查任何行的金额列是否为空白

Sub Generate_String()
Dim HTML_body As String
Dim count_var As Integer
Dim arr() As String
Dim arr_val() As Integer
Dim great_count As Integer
Dim arr_now As Integer
Dim rng_Body As Range
Dim xMailBody As String
Dim xOutApp As Object
Dim xOutMail As Object
Dim validate_pass As Boolean

'Checks if the any of the range has blank values
'if blank value is found it will make validate_pass variable to false.
Set rng_Body = Range("B3:B" & Range("B3").End(Excel.XlDirection.xlDown).Row)
validate_pass = True
For Each r In rng_Body
    If Trim(r.Text) = "" Then
        validate_pass = False
        Exit For
    End If
Next r

'If validate_pass variable is false then throws out error message.
'Else it will go through the normal procedure to sent out emails.
If validate_pass = False Then
    MsgBox "Your appropriate error message if any of the amount value found blank", vbCritical
Else
    Set xOutApp = CreateObject("Outlook.Application")
    Set xOutMail = xOutApp.CreateItem(0)

    arr_now = 0

    Set rng_Body = Range("B3:B" & Range("B3").End(Excel.XlDirection.xlDown).Row)


    '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:B" & Range("B3").End(Excel.XlDirection.xlDown).Row), ">0")

    HTML_body = "Hi <Name>," & vbNewLine & vbNewLine & "I'd like to order"

    '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
                HTML_body = HTML_body + " " + CStr(arr_val(j)) + " of " + arr(j)
            Else
                For j = 0 To UBound(arr)
                    If j = 0 Then
                        HTML_body = HTML_body + " " + CStr(arr_val(j)) + " of " + arr(j)
                    ElseIf j = UBound(arr) Then
                        HTML_body = HTML_body + " & " + CStr(arr_val(j)) + " of " + arr(j)
                    Else
                        HTML_body = HTML_body + " , " + CStr(arr_val(j)) + " of " + arr(j)
                    End If
                Next j
            End If
    Else
        HTML_body = "No Orders"
    End If

    HTML_body = HTML_body & vbNewLine & "Kind Regards" & vbNewLine & "<Name>"
    With xOutMail
            .To = "marcbrooks991@hotmail.com"
            .CC = ""
            .BCC = ""
            .Subject = "Test email send by button clicking"
            .Body = HTML_body
            .Display   'or use .Send
    End With
    MsgBox "Order Email has been Sent"
End If


End Sub

【讨论】:

  • 干杯,效果很好。好奇的问题,你如何改变提到类型的文字?因为我需要将其更改为一组参考编号,而不是参考 A 列中的单元格。另外,我该如何更改消息的正文?我将发布一个编辑说明我的意思。
  • 您可以发布您的整个 VBA 代码以供参考吗?需要看看你创造了什么。
  • 您是否使用 Outlook 来生成邮件?
  • 是的,我正在使用 Outlook 生成邮件。
  • 那么电子邮件中的 是什么?
【解决方案2】:

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

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

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

导致: 我想要100个可变电阻

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 2022-07-26
    相关资源
    最近更新 更多