复制范围,包括在Outlook电子邮件正文中粘贴时的格式 [英] Copying range including formatting when pasting in Outlook email body

查看:66
本文介绍了复制范围,包括在Outlook电子邮件正文中粘贴时的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I had search all over of this question and still not get the exact codes for it. I need to copy the color of the pivot table from excel to outlook body. When running the code i got the format but the only problem is the color of the table is turning into black and grey.

请帮助我弄清楚如何设置我需要的确切颜色.

Please help me to figure it out how to put the exact color that i need.

这是我的代码:

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

推荐答案

您将不得不对代码进行一些调整,如下所示:

You will have to tweak the code a bit which should look something like this:

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

如果您想做更多的事情,则必须阅读有关

In case you want to do more, you will have to read more about Word VBA.
This is just a sample on what you can do with Outlook's Word Editor.

这篇关于复制范围,包括在Outlook电子邮件正文中粘贴时的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆