将Excel范围粘贴到Outlook

8
我想在Outlook中粘贴一系列单元格。
这是我的代码:
Sub Mail_Selection_Range_Outlook_Body()

Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object

Set rng = Nothing
On Error Resume Next
' Only send the visible cells in the selection.
Set rng = Selection.SpecialCells(xlCellTypeVisible)
Set rng = Sheets("Sheet1").RangeToHtml("D4:D12").SpecialCells(xlCellTypeVisible, xlTextValues)
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 = ThisWorkbook.Sheets("Sheet2").Range("C1").Value
    .CC = ""
    .BCC = ""
    .Subject = "This is the Subject line"
    .HTMLBody = RangeToHtml.rng
    ' In place of the following statement, you can use ".Display" to
    ' display the e-mail message.
    .Display
End With
On Error GoTo 0

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

Set OutMail = Nothing
Set OutApp = Nothing
End Sub

我没有收到任何错误提示,只是无法在Outlook中粘贴范围。
我已经删除了On Error Resume Next。它给了我一个错误提示:

对象不支持此属性或方法。


3
这段代码 Set rng = Sheets("Sheet1").RangeToHtml("D4:D12").SpecialCells(xlCellTypeVisible, xlTextValues) 看起来有问题。假设RangeToHtml是从微软网站上的函数,它返回的是一个字符串,因此你不能在该字符串上调用SpecialCells。去掉On Error Resume Next,你就会看到错误提示。 - Tim Williams
就像@TimWilliams所说的那样,除非你有非常特定的原因需要包含它,否则请删除On Error Resume Next - enderland
谢谢你的回复,Tim和Enderland。我已经按照你们说的删除了On Error Resume Next,但是它给了我一个错误,即“对象不支持此属性或方法”。你有什么解决办法来消除这个错误吗? - Gilbert Jacob
那么错误会带你到哪一行呢? - Andy G
2个回答

20
首先,是 RangeToHTML。脚本将其称为方法,但它不是。这是MVP Ron de Bruin的一种流行的函数。巧合的是,在那些几行被修改之前,该链接指向了你发布的脚本的确切来源。
接下来是Range.SpecialCells。此方法对范围进行操作,并仅返回与给定条件匹配的单元格。在您的情况下,您似乎只关心可见文本单元格。重要的是,它作用于范围,而不是HTML文本。
为了完整起见,我将在下面发布一个工作版本的脚本。我肯定建议忽略它并重新查看Ron the Bruin的优秀原始版本。
Sub Mail_Selection_Range_Outlook_Body()

Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object

Set rng = Nothing
' Only send the visible cells in the selection.

Set rng = Sheets("Sheet1").Range("D4:D12").SpecialCells(xlCellTypeVisible)

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)


With OutMail
    .To = ThisWorkbook.Sheets("Sheet2").Range("C1").Value
    .CC = ""
    .BCC = ""
    .Subject = "This is the Subject line"
    .HTMLBody = RangetoHTML(rng)
    ' In place of the following statement, you can use ".Display" to
    ' display the e-mail message.
    .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)
' By Ron de Bruin.
    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

我已经使用了你的代码来满足我的需求,它运行得很好。我想知道是否可以添加一些电子邮件正文,比如一些文本。我尝试过 .HTMLBody="Hi, ..." & .HTMLBody=RangetoHTML(rng),但是它没有起作用。 - Vinod
1
在 Ron the Bruin 的帖子中找到了答案,谢谢。 - Vinod
@Vinod 你能在邮件正文中添加一些额外的文本吗?除了宏粘贴的文本之外。 - synthaxe
此外,它是否有办法也复制图像? - synthaxe
@synthaxe: 你可以通过关注Ron the Bruin的帖子来了解如何向电子邮件正文中添加附加文本... 我不确定如何在电子邮件中添加图像,除此之外我现在使用Java发送电子邮件。 - Vinod
显示剩余2条评论

6
通常,这个问题是在Ron de Bruin的RangeToHTML函数的上下文中提出的,该函数从Excel.Range创建一个HTML PublishObject,通过FSO提取它,并将结果流HTML插入电子邮件的HTMLBody中。这样做会删除默认签名(RangeToHTML函数有一个helper函数GetBoiler,试图插入默认签名)。
不幸的是,文档不完整的Application.CommandBars方法在Outlook中不可用。
wdDoc.Application.CommandBars.ExecuteMso "PasteExcelTableSourceFormatting"

它将引发运行时错误 6158:

enter image description here

但是我们仍然可以利用Word.Document,通过MailItem.GetInspector方法进行访问,我们可以像这样复制并粘贴来自Excel的选定内容到Outlook电子邮件正文中,保留您的默认签名(如果有的话)。

Dim rng as Range
Set rng = Range("A1:F10") 'Modify as needed

With OutMail
    .To = "xxxxx@xxxxx.com"
    .BCC = ""
    .Subject = "Subject"
    .Display
    Dim wdDoc As Object     '## Word.Document
    Dim wdRange As Object   '## Word.Range
    Set wdDoc = OutMail.GetInspector.WordEditor
    Set wdRange = wdDoc.Range(0, 0)
    wdRange.InsertAfter vbCrLf & vbCrLf
    'Copy the range in-place
    rng.Copy
    wdRange.Paste
End With

请注意,在某些情况下,这可能无法完美地保留列宽或在某些情况下行高,并且虽然它还会复制Excel范围中的形状和其他对象,但这也可能导致一些奇怪的对齐问题,但对于简单的表格和Excel范围,它非常好。

enter image description here


我该如何添加签名?我尝试使用标准的 .HTMLBody = myMailBody & .HTMLBody,但是看不到签名,你知道为什么吗?也许在这种方法中不可能实现? - Muska

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接