电子邮件Excel表VBA但缺少标题

时间:2019-02-19 23:58:56

标签: excel vba

我正在使用Ron de Bruin出色的VBA代码通过Outlook通过电子邮件发送Excel 。问题是我只在电子邮件中获取了实际数据范围,却错过了必要的标题。

我还进行了一些过滤,可能会导致空白范围,因此不应发送出去(当前代码通过If rng Is Nothing Then很好地实现了它

编辑:提出潜在的方向

我可以定义一个新范围Range1,其中包括Table1及其标题。经过测试,代码可以正常运行,并且还包含标头,但是即使过滤提供0行,Range1在技术上也从不为空。

Set rng = Sheets("Sheet1").Range("Range1").SpecialCells(xlCellTypeVisible)

我可以以某种方式进行调整以适应这一点吗?

If rng Is Nothing Then

标头现在将提供1行,并且rng永远不会Nothing。可以将此代码调整为退出带有1可见行而不是Nothing的代码吗?

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("Sheet1").Range("Table1").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 = "test@mail.com"
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .HTMLBody = "HARDCODED HEADER?" & RangetoHTML(rng)
        .Display   'or use .Send
    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

1 个答案:

答案 0 :(得分:1)

代替使用此

Set rng = Sheets("Sheet1").Range("Table1").SpecialCells(xlCellTypeVisible)

替换为

Set rng = Sheets("Sheet1").ListObjects("Table1").Range

您使用的第一个仅复制表的数据。我输入的第二个代码将获取表头的整个范围+表中的数据