在outlook中粘贴特定的excel范围

Gil*_*cob 6 excel outlook vba excel-vba

我正在处理我想要自动化的Excel报告,但是,单元格的范围未粘贴在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
Run Code Online (Sandbox Code Playgroud)

我没有收到任何错误,只是它没有在outlook中粘贴范围.谢谢.

Pau*_*Jan 17

首先,RangeToHTML.脚本将其称为方法,但事实并非如此.这是MVP Ron de Bruin的热门功能.巧合的是,链接指向您发布的脚本的确切来源,那几行得到了屠杀修改之前.

使用Range.SpecialCells.这种方法在一个范围内操作,并仅返回那些符合给定标准的细胞.在您的情况下,您似乎只对可见文本单元格感兴趣.重要的是,它在Range上运行,而不是在HTML文本上运行.

为了完整起见,我将在下面发布一个工作版本的脚本.我一定会建议无视它和罗恩熊先生重温优秀的原创.

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
Run Code Online (Sandbox Code Playgroud)


Dav*_*ens 6

通常,这个问题是在 Ron de Bruin 函数的上下文中提出的,该函数从 中RangeToHTML创建 HTML ,通过 FSO 提取该 HTML,并将生成的流 HTML 插入到电子邮件的 中。这样做会删除默认签名(该函数有一个尝试插入默认签名的辅助函数)。PublishObjectExcel.RangeHTMLBodyRangeToHTMLGetBoiler

不幸的是,这种记录不足的Application.CommandBars方法无法通过 Outlook 获得:

wdDoc.Application.CommandBars.ExecuteMso "PasteExcelTableSourceFormatting"
Run Code Online (Sandbox Code Playgroud)

它将引发运行时 6158:

在此输入图像描述

但我们仍然可以利用可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
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述