如何将mulple excel范围作为图片同时粘贴到outlook

时间:2017-06-07 17:44:29

标签: excel vba excel-vba outlook

请帮我解决这个问题。我是这个领域的新手,我想从excel中粘贴多组范围并将其粘贴到尺寸已修改的Outlook电子邮件中。请帮我。提前致谢!

这是我目前的代码:

Sub EmailSend()

    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object
    Dim StrBody As String

    StrBody = "Please see our current Report"

    Set rng = Nothing
    On Error Resume Next
    'Only the visible cells in the selection
    'Set rng = Selection.SpecialCells(xlCellTypeVisible)
    'You can also use a fixed range if you want
    Set rng = Range("C4:D8").SpecialCells(xlCellTypeVisible)
    On Error GoTo 0

    If rng Is Nothing Then
        MsgBox "The selection is not a range or the sheet is protected" & _
               vbNewLine & "please correct and try again.", vbOKOnly
        Exit Sub
    End If

    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

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

    On Error Resume Next

    With OutMail
        .To = "x"
        .CC = "x"
        .BCC = "x"
        .Subject = "RRF for Vendor Sourcing - " & Cells(3, 2)
        .HTMLBody = StrBody & RangetoHTML(rng)
        .Display   'or use .Display
    End With
    On Error GoTo 0

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub

1 个答案:

答案 0 :(得分:0)

这行代码定义了您的范围。

 Set rng = Range("C4:D8").SpecialCells(xlCellTypeVisible)

您可以简单地定义另一个范围:

Dim rng2 As Range     
Set rng2 = Range("J4:N8").SpecialCells(xlCellTypeVisible)

然后使用第二个范围再次调用RangetoHTML():

RangetoHTML(rng2)

将返回另一个html字符串,然后你可以像这样连接:

 .HTMLBody = StrBody & RangetoHTML(rng) & "<br>" & RangetoHTML(rng2)