【问题标题】:Send an Outlook email from Excel putting the last row range text in the body message从 Excel 发送 Outlook 电子邮件,将最后一行范围文本放在正文中
【发布时间】:2017-07-24 20:32:25
【问题描述】:

我想从我的 Excel 电子表格中发送一封电子邮件,其中包含一条小消息到正文中,其中包含从 A 列到 G 列的最后一行范围。

我尝试将最后一行范围添加到以下代码中,但没有成功。

你能帮忙吗?

Sub Mail_LastRowRange_Outlook()

Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

strbody = "Thanks for your help" & '**I Would like to insert_
           my last row range from column A to G in here**

On Error Resume Next
With OutMail
    .To = ""
    .CC = ""
    .BCC = ""
    .Subject = "Looking for a solution"
    .Body = strbody
    .Attachments.Add ("C:\test.txt")
    .Send   
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End Sub

【问题讨论】:

    标签: vba excel email outlook


    【解决方案1】:
    Sub test1()
      Dim lastRow As Long
      'get last row from column A of worksheet with 'codename' Sheet1
      lastRow = getLastRow(Sheet1, 1)
    
      Dim r As Range
      Set r = Sheet1.Range("A" & lastRow & ":G" & lastRow)
    
      Dim str As String
      'Join the cell texts with a space - lazy coding...
      str = Join(Application.Transpose(Application.Transpose(r.Value2)), " ")
    
      MsgBox str
    End Sub
    
    
    Public Function getLastRow(ws As Worksheet, Optional col As Long) As Long
      Dim arr As Variant
    
      If col > 0 Then
            arr = Intersect(ws.UsedRange, ws.Columns(col)).Value2
      Else
            arr = ws.UsedRange.Value2
      End If
    
      Dim i As Long, j As Long
      For i = UBound(arr) To 1 Step -1
            For j = UBound(arr, 2) To 1 Step -1
                  If Len(arr(i, j)) > 0 Then
                        getLastRow = i + ws.UsedRange.Row - 1
                        Exit Function
                  End If
            Next j
      Next i
    End Function
    

    上面的函数是THE最强大的函数,用于获取工作表/列中的最后一行实际数据值。其他所有内容都易受攻击,包括 Range.Find("*", , , , , xlPrevious),它易受过滤 ListObject 中的 activeCell 攻击

    下面的函数容易受到一些因素的影响,例如过滤行、最后一行有数据等。

    Public Function getLastRow2(ws As Worksheet, col As Long) As Long
        getLastRow2 = ws.Cells(ws.Rows.Count, col).End(xlUp).Row
    End Function
    

    【讨论】:

    • 嗨 MacroMarc,请问如何将 Sub test1() 放入正文中?
    • 感谢您的大力支持。现在工作正常。在 A 列中,我得到了以数字形式出现在电子邮件中的日期数字。我会尝试解决它。再次感谢
    【解决方案2】:

    确定特定的行,并创建一个范围对象并对其进行迭代,例如:

        Dim rng As range
        Dim item As range
        Dim row As Long
    
        Set rng = range(Cells(row, 1), Cells(row, 7))
    
        For Each item In rng
            strBody = strBody & " " & item
        Next item
    

    【讨论】:

    • 对不起我的问题。具体的row对应的是lastrow吗?
    • 是的,但是你必须找到最后一行。您可以使用上面答案中建议的 MacroMarc 方法,并将结果存储在新的范围对象中,并通过 range.row 查询行值
    • 非常感谢您的回复
    • 如果你的sheet不是sheet1,在MacroMarc的方法中,可以用Sheets("insert_name_of_the_sheet")代替。
    【解决方案3】:

    这应该可以工作,只要从单元格 A1 到最后一行的 A 列中没有空白单元格,并且 A 列中有数据。将“strbody = ....”的行替换为所有这些。

    Cells(1,1).Select
    Selection.End(xlDown).Select
    
    Dim s As String
    s = ActiveCell.Value     'A
    ActiveCell.Offset(0, 1).Select
    s = s & ActiveCell.Value 'B
    ActiveCell.Offset(0, 1).Select
    s = s & ActiveCell.Value 'C
    ActiveCell.Offset(0, 1).Select
    s = s & ActiveCell.Value 'D
    ActiveCell.Offset(0, 1).Select
    s = s & ActiveCell.Value 'E
    ActiveCell.Offset(0, 1).Select
    s = s & ActiveCell.Value 'F
    ActiveCell.Offset(0, 1).Select
    s = s & ActiveCell.Value 'G
    
    strbody = "Thanks for your help" & s
    

    【讨论】:

    • 非常感谢。有时无论如何,我的电子表格中有一些空单元格。你认为我的案子有可能得到解决吗?
    • 无需选择单元格。只需挂上范围对象并使用它。对于最后一行,最好从底部开始使用 xlUp,例如:Cells(sheet1.rows.count, 1).End(xlUp)。然后通过 Offset 得到跨到 G 列的范围
    • 从您知道不会为空的列开始。或者,放入一个虚拟列并关闭它以找到结尾。
    • 完全同意 MacroMarc 关于如何获得最后一行的看法.....希望我之前想到过....
    • 嗨,MacroMarc,很抱歉成为一个傻瓜,你能把你 6 分钟前所说的代码写上,这样我可以更好地理解吗?非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    • 1970-01-01
    • 2018-01-10
    • 1970-01-01
    • 2021-09-27
    相关资源
    最近更新 更多