VBA Not Adding Third Table to Outlook Email

I am using the code below to automatically generate an email and embed one pivot table and two tables. This data comes from three separate Power Queries (one original and two duplicates) with slightly different custom columns. This code works perfectly when adding the pivot and one of the two tables. Later, I realized I needed to add a third table and thought it would be easy to modify this code to include, but for some reason, it’s unable to contain the third table, ‘Defect_Log_Open_2’. I decided to remove the original pivot ‘PivotTable2’ and table ‘Defect_Log_Table’ and try to get the newly added table ‘Defect_Log_Open_2’ image to load into the email by itself. The image of that table appeared briefly, like a flicker, and then disappeared. I am at a loss, and frankly, at my wit’s ends why this is occurring, so I thought a fresh pair of eyes could see something I am missing. Thanks for your input.

Sub EmailPivotTableAndDefectTables_AsTablesAndImagesWithDynamicSizing()
    'Declare Outlook Variables
    Dim oLookApp As Object
    Dim oLookItm As Object
    Dim oLookIns As Object
    
    'Declare Word Variables
    Dim oWrdDoc As Object
    Dim oWrdRng As Object
    
    'Declare Excel Variables
    Dim ws As Worksheet
    Dim pt As PivotTable
    Dim defectLogTable As ListObject
    Dim defectLogOpenTable2 As ListObject
    Dim slicerInfo As String
    Dim slicerCache As slicerCache
    Dim totalDefects As Variant
    Dim inProgressCount As Variant
    Dim testCount As Variant
    Dim completeCount As Variant
    Dim inProgressPct As Double
    Dim testPct As Double
    Dim completePct As Double
    Dim summary As String
    Dim cellE4Text As String
    Dim defectLogURL As String
    Dim emailAddresses As String
    Dim emailRange As Range
    Dim cell As Range
    Dim pic As Object
    
    ' Set worksheet and tables for PivotTable and Defect_Log_Table
    Set ws = ThisWorkbook.Sheets("DefectsTracker")
    Set pt = ws.PivotTables("PivotTable2")
    Set defectLogTable = ThisWorkbook.Sheets("DefectsTable").ListObjects("Defect_Log_Table")
    Set defectLogOpenTable2 = ThisWorkbook.Sheets("OpenDefects").ListObjects("Defect_Log_Open_2")
    
    ' Get image dimensions from cells
    Dim pivotWidth As Double
    Dim pivotHeight As Double
    Dim tableWidth As Double
    Dim tableHeight As Double
    Dim openTableWidth As Double
    Dim openTableHeight As Double
    
    ' Get pivot table dimensions from cells U4 (width) and U2 (height) on DefectsTracker
    pivotWidth = ws.Range("U4").Value
    pivotHeight = ws.Range("U2").Value
    
    ' Get defect log table dimensions from cells G1 (width) and E1 (height) on DefectsTable
    tableWidth = ThisWorkbook.Sheets("DefectsTable").Range("G1").Value
    tableHeight = ThisWorkbook.Sheets("DefectsTable").Range("E1").Value
    
    ' Get defect log open table 2 dimensions from cells G1 (width) and I1 (height) on OpenDefects
    openTableWidth = ThisWorkbook.Sheets("OpenDefects").Range("G1").Value
    openTableHeight = ThisWorkbook.Sheets("OpenDefects").Range("I1").Value
    
    ' Safely get data from PivotTable using GetPivotData
    totalDefects = 0
    inProgressCount = 0
    testCount = 0
    completeCount = 0
    On Error Resume Next
    totalDefects = pt.GetPivotData("[Measures].[Sum of Sum Row]").Value
    inProgressCount = pt.GetPivotData("[Measures].[Sum of In Progress]").Value
    testCount = pt.GetPivotData("[Measures].[Sum of Test]").Value
    completeCount = pt.GetPivotData("[Measures].[Sum of Complete]").Value
    On Error GoTo 0

    ' Manually calculate percentages
    If totalDefects > 0 Then
        inProgressPct = (inProgressCount / totalDefects)
        testPct = (testCount / totalDefects)
        completePct = (completeCount / totalDefects)
    End If

    ' Set the Defect Log URL
    defectLogURL = "https://cornerstonebuildingbrands.sharepoint.com/sites/CBBUSWERPMES/Lists/Defect%20Log/All%20%20Open%20Defects.aspx"

    ' Retrieve slicer information using VisibleSlicerItemsList
    slicerInfo = "<p><strong>Applied Filters</strong></p><ul>"
    On Error Resume Next
    For Each slicerCache In ThisWorkbook.SlicerCaches
        If slicerCache.PivotTables(1).Name = "PivotTable2" Then
            slicerInfo = slicerInfo & "<li>" & slicerCache.SlicerCacheLevels(1).Name & ": "

            ' Use VisibleSlicerItemsList to get the visible slicer items
            If slicerCache.VisibleSlicerItemsList.Count > 0 Then
                slicerInfo = slicerInfo & Join(slicerCache.VisibleSlicerItemsList, ", ")
            Else
                slicerInfo = slicerInfo & "All"
            End If

            slicerInfo = slicerInfo & "</li>"
        End If
    Next slicerCache
    On Error GoTo 0
    slicerInfo = slicerInfo & "</ul>"

    ' Get the value from cell E4 of the worksheet
    cellE4Text = ws.Range("E4").Value

    ' Create summary string with HTML formatting, hyperlink, slicers
    summary = "<p><strong>The Total Number of Defects is " & totalDefects & ".</strong></p>" & _
              "<p><strong>" & inProgressCount & " In Progress (" & Format(inProgressPct, "0%") & _
              "), " & testCount & " in Test (" & Format(testPct, "0%") & "), and " & completeCount & _
              " Complete (" & Format(completePct, "0%") & ").</strong></p>" & _
              "<p>Here are some insights:</p>" & _
              "<ul>" & _
              "<li>Most defects are Complete, indicating that the project is progressing well.</li>" & _
              "<li>Some defects are still in the In Progress and Test phases, so continued monitoring is necessary.</li>" & _
              "</ul>" & _
              "<p>To view the complete listing of all defects for Genesis, please click <a href='" & defectLogURL & "'>Genesis Defect Log</a>.</p>" & _
              slicerInfo & _
              "<p><strong>" & cellE4Text & "</strong></p>" & "<br>" & "<br>"

    ' Get email addresses from the "Contacts" sheet
    Set emailRange = ThisWorkbook.Sheets("Contacts").Range("A2:A1000") ' Adjust this range as per your data
    emailAddresses = ""
    For Each cell In emailRange
        If cell.Value <> "" Then
            emailAddresses = emailAddresses & cell.Value & ";"
        End If
    Next cell

    ' Late Binding: Create an instance of Outlook application
    Set oLookApp = CreateObject("Outlook.Application")

    ' Create new email item
    Set oLookItm = oLookApp.CreateItem(0)

    With oLookItm
        ' Set subject and recipients
        .To = emailAddresses
        .Subject = "Genesis Defect Summary Report"
        
        ' Set HTML body with formatted summary and insights
        .HTMLBody = summary

        ' Display the email for review
        .Display
        
        ' Get the Active Inspector and Word Document for content manipulation
        Set oLookIns = .GetInspector
        Set oWrdDoc = oLookIns.WordEditor
        
        ' Insert PivotTable as an image
        pt.TableRange2.CopyPicture Appearance:=xlScreen, Format:=xlPicture
        Set oWrdRng = oWrdDoc.Content
        oWrdRng.Collapse Direction:=wdCollapseEnd
        oWrdRng.Paste ' Paste the PivotTable image
        
        ' Adjust the size of the pivot table image using dynamic values from cells
        oWrdDoc.InlineShapes(oWrdDoc.InlineShapes.Count).Width = pivotWidth
        oWrdDoc.InlineShapes(oWrdDoc.InlineShapes.Count).Height = pivotHeight
        
        ' Insert a paragraph break
        oWrdRng.Collapse Direction:=wdCollapseEnd
        oWrdRng.InsertParagraphAfter
        
        ' Insert Defect Log table as an image (preserving formatting)
        defectLogTable.Range.CopyPicture Appearance:=xlScreen, Format:=xlPicture
        oWrdRng.Collapse Direction:=wdCollapseEnd
        oWrdRng.Paste ' Paste the Defect_Log_Table image
        
        ' Adjust the size of the defect log table image using dynamic values from cells
        oWrdDoc.InlineShapes(oWrdDoc.InlineShapes.Count).Width = tableWidth
        oWrdDoc.InlineShapes(oWrdDoc.InlineShapes.Count).Height = tableHeight
        
        ' Insert a paragraph break
        oWrdRng.Collapse Direction:=wdCollapseEnd
        oWrdRng.InsertParagraphAfter
        
        ' Insert Defect Log Open 2 table as an image (preserving formatting)
        defectLogOpenTable2.Range.CopyPicture Appearance:=xlScreen, Format:=xlPicture
        oWrdRng.Collapse Direction:=wdCollapseEnd
        oWrdRng.Paste ' Paste the Defect_Log_Open_2 image
        
        ' Adjust the size of the defect log open 2 table image using dynamic values from cells
        oWrdDoc.InlineShapes(oWrdDoc.InlineShapes.Count).Width = openTableWidth
        oWrdDoc.InlineShapes(oWrdDoc.InlineShapes.Count).Height = openTableHeight
        
        ' Insert final paragraph for closing
        oWrdRng.Collapse Direction:=wdCollapseEnd
        oWrdRng.InsertParagraphAfter
        oWrdRng.InsertAfter "Thank you,"
        oWrdRng.InsertParagraphAfter
        oWrdRng.InsertAfter "Project Management Office"
        oWrdRng.InsertParagraphAfter
    End With
End Sub


I reviewed the code to ensure it was formatted correctly to include the third additional table I need embed and I ran debug to ensure there weren’t any compiling issue. This should work and I am at a loss why it doesn’t.

New contributor

gdgonzal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật