将Excel范围粘贴到Outlook电子邮件正文

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将Excel范围粘贴到Outlook电子邮件正文相关的知识,希望对你有一定的参考价值。

我想在Excel工作表中复制固定范围内的数据并粘贴到电子邮件正文中。

以下是我提出的代码。但是我无法粘贴指定范围A11:H12。

Private Sub CommandButton1_Click()
    On Error GoTo ErrHandler

' SET Outlook APPLICATION OBJECT.
Dim objOutlook As Object
Set objOutlook = CreateObject("Outlook.Application")

' CREATE EMAIL OBJECT.
Dim objEmail As Object
Set objEmail = objOutlook.CreateItem(olMailItem)

With objEmail
    .To = "email"
    .Subject = "test"
    .Body = ActiveSheet.Range("A11:H12").Select
    .Display        ' DISPLAY MESSAGE.
End With

' CLEAR.
Set objEmail = Nothing:    Set objOutlook = Nothing

ErrHandler:
    '
End Sub
答案

正如Ron在评论中所说的那样。波纹管代码和功能可以解决问题。复制它们

Private Sub CommandButton1_Click()

' SET Outlook APPLICATION OBJECT.
Dim rng As Range
Dim objOutlook As Object
Set objOutlook = CreateObject("Outlook.Application")

Set rng = Nothing
On Error Resume Next
Set rng = ActiveSheet.Range("A11:H12").SpecialCells(xlCellTypeVisible)
On Error GoTo 0

' CREATE EMAIL OBJECT.
Dim objEmail As Object
Set objEmail = objOutlook.CreateItem(olMailItem)

With objEmail
.To = "email"
.Subject = "test"
.htmlBody = RangetoHTML(rng)
.Display        ' DISPLAY MESSAGE.
End With

' CLEAR.
Set objEmail = Nothing:
Set objOutlook = 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
另一答案

码:

    Sub sendEmail()
    'call outlook
    Dim MyOlapp As Object, MyItem As Object
    Set MyOlapp = CreateObject("Outlook.Application")
    Set MyItem = MyOlapp.CreateItem(olMailItem)
        'ajust range of sheet
        Range("A11:H12").Select
        Selection.Copy

    With MyItem
        'ajust number of sheet
        .To = Sheet17.[b1].Value 'e-mail adress
        .Subject = Sheet17.[b2].Value 'subject of e-mail
        .Body = Sheet17.[b3].Value 'body of e-mail
        .Display
        SendKeys ("^{DOWN}")
        SendKeys ("^{DOWN}")
        SendKeys ("%m")
        SendKeys ("v")
        SendKeys ("s")
        SendKeys ("{UP}")
        SendKeys ("{UP}")
        SendKeys ("{ENTER}")
        SendKeys ("{ENTER}")
        SendKeys ("%m")
        SendKeys ("q")
        SendKeys ("{ENTER}")


    End With
    End Sub

以上是关于将Excel范围粘贴到Outlook电子邮件正文的主要内容,如果未能解决你的问题,请参考以下文章

将范围复制为图像并粘贴到 Outlook 中(结果小/模糊)

在 Excel 中使用 VBA,如何在 Outlook 中粘贴表格然后将表格转换为文本?

使用 VBA 宏选择和复制 Outlook 电子邮件正文

VBA 在 Outlook 中粘贴带有图表的特定 excel 范围

将 Excel 范围内的超链接传输到 Outlook 电子邮件

将单元格范围从excel复制到电子邮件的主题和正文