【问题标题】:Copying range including formatting when pasting in Outlook email body复制范围包括在 Outlook 电子邮件正文中粘贴时的格式
【发布时间】:2018-03-21 14:17:36
【问题描述】:

我已经搜索了这个问题,但仍然没有得到它的确切代码。 我需要将数据透视表的颜色从 excel 复制到 Outlook 正文。运行代码时,我得到了格式,但唯一的问题是表格的颜色变成了黑色和灰色。

请帮我弄清楚如何放置我需要的确切颜色。

这是我的代码:

Sub AUTO_MAIL()
    Dim rng As Range, rng2 As Range, rng3 As Range, rng4 As Range, sub1 As Range, sub2 As Range, sub3 As Range, sub4 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 = Sheets("Data Entry").PivotTables(1).TableRange1
    Set rng2 = Sheets("ACN Workflow").PivotTables(1).TableRange1
    Set rng3 = Sheets("L'Oreal Workflow").PivotTables(1).TableRange1
    Set rng4 = Sheets("MTD Volume").PivotTables(1).TableRange1
    Set sub1 = Sheets("Data Entry").Range("A1:E1").SpecialCells(xlCellTypeVisible)
    Set sub2 = Sheets("ACN Workflow").Range("A1:G1").SpecialCells(xlCellTypeVisible)
    Set sub3 = Sheets("L'Oreal Workflow").Range("A1:G1").SpecialCells(xlCellTypeVisible)
    Set sub4 = Sheets("MTD Volume").Range("A1:B1").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)

    On Error Resume Next
    With OutMail
        .To = ""
        .CC = ""
        .BCC = ""
        .Subject = "Step+ Volume Tracker, Data Entry/Workflow Ageing Report and Rejection Report |"
        .HTMLBody = "<b>Dear All,</b><br><br>" & "Please see below summary of invoices and links to the <b>Volume Tracker</b> and <b>Ageing Report</b> (Data Entry and Workflow).<br>" & RangetoHTML(sub4) & vbCrLf & RangetoHTML(rng4) & vbCrLf & RangetoHTML(sub3) & vbCrLf & RangetoHTML(rng3) & vbCrLf & RangetoHTML(sub2) & vbCrLf & RangetoHTML(rng2) & vbCrLf & RangetoHTML(sub1) & vbCrLf & 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)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2013
    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.SpecialCells(xlCellTypeVisible).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

【问题讨论】:

  • 如果您的 Outlook 版本是 2010 或更高版本,您可以尝试here 发布的解决方案。我也在那里发布了我自己的 1 个。如果您是后期绑定,您只需将类型更改为 wdFormatOriginalFormatting 或 16。
  • 另外,您可能需要检查 Office 的当前主题。它可能会有所不同,因此复制后会显示不同的颜色。
  • 有效!非常感谢,但是我如何调整它的大小,因为当粘贴到 Outlook 正文中时它变得更小并且需要手动调整大小,是否有修复表格大小的代码?再次感谢您。
  • 我也想问一下,如果我可以添加更多的表来复制?我试图添加更多工作表,但它只是与第一个表重叠。非常感谢

标签: vba excel


【解决方案1】:

你必须稍微调整一下代码,看起来应该是这样的:

Sub due()

    Dim ol As Object 'Outlook.Application
    Dim olEmail As Object 'Outlook.MailItem
    Dim olInsp As Object 'Outlook.Inspector
    Dim wd As Object 'Word.Document
    Dim rCol As Collection, r As Range, i As Integer

     '/* if outlook is running use GO, create otherwise */
    Set ol = GetObject(Class:="Outlook.Application")
    Set olEmail = ol.CreateItem(0) 'olMailItem

    Set rCol = New Collection
    With rCol
        .Add Sheet1.Range("A1:B6") '/* add your ranges the same sequence */
        .Add Sheet2.Range("A1:B6") '/* as you want them added in the body */
    End With

    With olEmail
        .To = ""
        '/* bonus basic html */
        .HTMLBody = "<html><body style=""font-family:calibri"">" & _
                    "<p><b>Dear Deer,</b><br><br> She see seas." & _
                    "</p></body></html>"

        Set olInsp = .GetInspector
        If olInsp.EditorType = 4 Then 'olEditorWord
            Set wd = olInsp.WordEditor
            For i = 1 To rCol.Count '/* iterate all ranges */
                Set r = rCol.Item(i): r.Copy
                wd.Range.InsertParagraphAfter
                wd.Paragraphs(wd.Paragraphs.Count).Range.PasteAndFormat 16
                '16 - wdFormatOriginalFormatting
            Next
        End If
        wd.Range.InsertParagraphAfter
        wd.Paragraphs(wd.Paragraphs.Count).Range.Text = "Regards, Patricia"
        wd.Paragraphs.Last.Range.Sentences.Last.Font.Bold = True
        .Display
    End With

End Sub

如果您想做更多,您将不得不阅读更多关于Word VBA 的信息。
这只是一个示例,说明您可以使用 Outlook 的 Word 编辑器执行哪些操作。

【讨论】:

  • 哇!!非常感谢您的帮助 :) 最后我能够以准确的外观运行它来表现出色。再次感谢。上帝保佑
  • @PatriciaKarenPecadizo Walang anuman。顺便说一句,this is how we say thank you at StackOverflow.
  • 嗨@L42 我想问一下如何才能使段落的某些部分变粗。
  • 我如何在第一个表格之后添加文本? @L42
猜你喜欢
  • 2020-07-19
  • 2014-01-24
  • 2020-04-05
  • 2021-09-27
  • 1970-01-01
  • 2013-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多