使用VBA的电子邮件Excel范围

使用VBA的电子邮件Excel范围,excel,vba,Excel,Vba,Excel(Office 365)中的此代码将向邮件正文添加Excel范围或表格 关键的一点是,它可以在后台工作,而不必打开邮件客户端(Outlook) 我有工作代码(Outlook在后台)。它只能将特定或合并的单元格链接到邮件正文(本质上是指文本,甚至不是简单的表格) 当链接一个合适的范围(比如说三列五行)时,我得到 运行时错误“13”:类型不匹配 Sub-Email() Dim xOutApp作为对象 将xOutMail作为对象 Dim xMailBody作为字符串 设置xOutApp=Cr

Excel(Office 365)中的此代码将向邮件正文添加Excel范围或表格

关键的一点是,它可以在后台工作,而不必打开邮件客户端(Outlook)

我有工作代码(Outlook在后台)。它只能将特定或合并的单元格链接到邮件正文(本质上是指文本,甚至不是简单的表格)

当链接一个合适的范围(比如说三列五行)时,我得到

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

Sub-Email()
Dim xOutApp作为对象
将xOutMail作为对象
Dim xMailBody作为字符串
设置xOutApp=CreateObject(“Outlook.Application”)
设置xOutMail=xOutApp.CreateItem(0)
xMailBody=“电子邮件正文文本”&vbNewLine&_
范围(“数组”)
出错时继续下一步
用xOutMail
.To=”test@mail.com"
.CC=“”
.BCC=“”
.Subject=“邮件主题”
.Body=xMailBody
邮寄
以
错误转到0
设置xOutMail=Nothing
设置xOutApp=Nothing
端接头
我还有其他的代码,可以从工作表中获取一个范围(某种程度上)并将其作为表发送。它仅在Outlook打开并运行时运行&否则会生成错误消息

Sub-Email2()
将sh设置为工作表
Set sh=ThisWorkbook.Sheets(“Sheet1”)
作为整数的Dim lr
lr=sh.Range(“D”和Application.Rows.Count).End(xlUp).Row
sh.Range(“D5:F”和lr)。选择
使用Selection.Parent.mailevelope.Item
.to=”test@mail.com"
.bcc=“”
.Subject=“”
邮寄
以
端接头

此代码比您演示的代码复杂一点,但它应该可以很好地完成工作。显然,修改它以满足您的需要

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

请注意,行计数变量的类型必须为
Long
,因为Excel的行数超过
Integer
可以处理的行数:
Dim lr As Long
。我建议使用VBA,因为
Integer
没有任何好处你可能会从阅读中受益。非常感谢!乍一看效果不错,但我们需要进一步调整,看看它是如何工作的。以前在Ron De Bruin的网站上,但找不到这个。不过那里有很多好东西。至于代码,不完全确定Tempfile的用途是什么?有人能简单地澄清一下吗?