电子邮件Excel范围与VBA

时间:2019-02-14 00:02:20

标签: excel vba

我需要有关Excel(Office 365)中的VBA代码的帮助,当发送邮件时,该代码会采用Excel 范围并将其添加到邮件正文中。至关重要的是它可以在后台工作而无需打开邮件客户端(Outlook)。

我有一个有效的代码(在后台使用Outlook非常有效)。缺点是我只能将特定或合并的单元格链接到邮件正文(本质上是指文本,甚至不是简单的表)。链接适当的范围(假设有3列和5行)时,出现错误消息

  

“运行时错误'13':类型不匹配”

Sub Email()

Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String

Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
xMailBody = "E-MAIL BODY TEXT" & vbNewLine & _
Range("ARRAY")

On Error Resume Next

With xOutMail
    .To = "test@mail.com"
    .CC = ""
    .BCC = ""
    .Subject = "Mail subject"
    .Body = xMailBody
    .Send
End With
On Error GoTo 0

Set xOutMail = Nothing
Set xOutApp = Nothing

End Sub

我一直在运行另一个代码,该代码确实能够从工作表中获取一个范围(一定范围)并将其作为表格发送出去,但是可悲的是,它仅在Outlook也打开并运行并产生错误消息时才运行

Sub Email2()

Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Sheet1")

Dim lr As Integer

lr = sh.Range("D" & Application.Rows.Count).End(xlUp).Row

sh.Range("D5:F" & lr).Select

With Selection.Parent.MailEnvelope.Item
.to = "test@mail.com"
.bcc = ""
.Subject = ""
.send

End With
End Sub

关于调整现有代码或采用新方法的任何提示吗?

1 个答案:

答案 0 :(得分:1)

此代码比您显示的要复杂一些,但可以很好地完成工作。显然,对其进行修改以适合您的需求。

Sub Mail_Selection_Range_Outlook_Body()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Don't forget to copy the function RangetoHTML in the module.
'Working in Excel 2000-2016
    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object

    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 = Sheets("YourSheet").Range("D4:D12").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 = "ron@debruin.nl"
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .HTMLBody = RangetoHTML(rng)
        .Send   '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


Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook

    TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

    'Copy the range and create a new workbook to past the data in
    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With

    'Publish the sheet to a htm file
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With

    'Read all data from the htm file into RangetoHTML
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.readall
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")

    'Close TempWB
    TempWB.Close savechanges:=False

    'Delete the htm file we used in this function
    Kill TempFile

    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function