复制范围,包括在Outlook电子邮件正文中粘贴时的格式

时间:2018-03-08 01:03:38

标签: excel vba excel-vba

我已经搜索了这个问题并且仍然没有得到它的确切代码。 我需要将数据透视表的颜色从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

1 个答案:

答案 0 :(得分: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编辑器可以做什么的一个示例。