excel 如何使字符串主体中的数据范围变量电子邮件输入每个值作为它自己的行

zf9nrax1  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(110)

我试图创建一个宏电子邮件的范围内的细胞,但我希望细胞输出到电子邮件作为有自己的行在这一点上它是给我一个电子邮件与每一个值一个接一个,我想每个细胞作为自己的行
以前我有它输入每个单元格单独,但我得到了一个连续的行错误

`Sub Mail_small_Text_Outlook()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Working in Office 2000-2016
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    
    strbody = build_body(ActiveWorkbook.Sheets("Sheet2").Range("B45:C65")) & vbNewLine & _
    "2nd Shift Trippers" & vbNewLine & _
              build_body(ActiveWorkbook.Sheets("Sheet2").Range("f45:g65"))
            `your text`  
    
    On Error Resume Next
    With OutMail
        .To = "namen@somewhere.org"
        .CC = ""
        .BCC = ""
        .Subject = Date & vbCrLf & "Trippers"
        .Body = strbody
        'You can add a file like this
        '.Attachments.Add ("C:\test.txt")
        .Send   'or use .Display
    End With
    On Error GoTo 0
    
    Set OutMail = Nothing
    Set OutApp = Nothing
    
    End Sub`


  Function build_body(rng As Range, Optional delimiter As String = " ") As String
Dim cel As Range
Dim tmpStr As String
For Each cel In rng.Cells
    If tmpStr <> "" Then tmpStr = tmpStr & delimiter & cel.Value
    If tmpStr = "" Then tmpStr = cel.Value
Next cel
Debug.Print tmpStr
build_body = tmpStr
End Function
dgtucam1

dgtucam11#

单元格的值需要用vbNewLine分隔。
你调用函数build_body,它有两个参数:单元格范围和分隔符。要使用新行分隔每个单元格,请使用分隔符“vbNewLine”。

strbody = build_body(ActiveWorkbook.Sheets("Sheet2").Range("B45:C65")) & vbNewLine & _
"2nd Shift Trippers" & vbNewLine & _
          build_body(ActiveWorkbook.Sheets("Sheet2").Range("f45:g65"), vbNewLine)
        `your text`

相关问题