1

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.

This is my codes:

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
L42
  • 19,427
  • 11
  • 44
  • 68
aicirtap
  • 111
  • 1
  • 14
  • If your version of Outlook is 2010 or up you can try the solutions posted [here](https://stackoverflow.com/a/48897463/2685412). I posted 1 of my own there too. You just have to change the type to `wdFormatOriginalFormatting` or 16 if you are late binding. – L42 Mar 08 '18 at 02:07
  • Also, you might want to check Office' current theme. It might be different so it is showing a different color once copied. – L42 Mar 08 '18 at 02:52
  • It works! thank you so much but how can i resize it because when pasted in outlook body it became smaller and need to resize manually, is there a code to fix the size of the table? thank you again. – aicirtap Mar 08 '18 at 03:53
  • I would like to ask also if how can i add more tables to copy? I tried to add more sheets but it just overlap the first table. Thank you so much – aicirtap Mar 08 '18 at 04:02

1 Answers1

1

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.

L42
  • 19,427
  • 11
  • 44
  • 68
  • wow!! thank you so much for your help :) finally i am able to run it with the exact appearance to excel. Thanks again. God bless – aicirtap Mar 09 '18 at 00:31
  • @PatriciaKarenPecadizo Walang anuman. Btw, [this is how we say thank you at StackOverflow](https://stackoverflow.com/help/someone-answers). – L42 Mar 09 '18 at 00:42
  • hi @L42 i would like to ask if how can i make some part of the paragraph be bold. – aicirtap May 30 '18 at 08:54
  • also how can i add a text after the first table ? @L42 – aicirtap May 30 '18 at 09:05