Consulting

Results 1 to 2 of 2

Thread: How to position charts and tables to outlook and how to resize charts so they have di

  1. #1
    VBAX Newbie
    Joined
    Jun 2017
    Posts
    3
    Location

    How to position charts and tables to outlook and how to resize charts so they have di

    I want table 1 (rng) to be position at the top before the charts and how do I resize charts where they have different sizes each specifically?
    Currently I have charts on top and tables below the charts



    Public Sub Insert_Charts_In_New_Email()

    Dim outApp As Object 'Outlook.Application
    Dim outMail As Object 'Outlook.MailItem
    Dim wEditor As Object 'Word.Document
    Dim wRange As Object 'Word.Range
    Dim chartsSheet As Object
    Dim chartObj As ChartObject
    Dim chartWidthCm As Single, chartHeightCm As Single
    Dim rng As Range
    Dim rng2 As Range
    Dim rng3 As Range
    Dim rng4 As Range

    'Required chart dimensions in the email

    chartWidthCm = 12.93
    chartHeightCm = 7.95

    'Sheet1 contains the charts

    Set chartsSheet = Sheets("Defects")
    Set chartsSheet2 = Sheets("Test Execution (Manual)")
    Set chartsSheet3 = Sheets("Ageing JIRAs")
    Set chartsSheet4 = Sheets("JIRA_List")
    Set chartsSheet5 = Sheets("Summary-Guidelines")


    Set rng = Nothing
    Set rng2 = Nothing
    Set rng3 = Nothing
    Set rng4 = Nothing
    ' Only send the visible cells in the selection.

    Set rng = Sheets("Summary-Guidelines").Range("B3:F23").SpecialCells(xlCellTypeVisible)
    Set rng2 = Sheets("Test Execution (Manual)").Range("A57:L63").SpecialCells(xlCellTypeVisible)
    Set rng3 = Sheets("Defects").Range("A60:F63").SpecialCells(xlCellTypeVisible)
    Set rng4 = Sheets("JIRA_List").Range("A10:P175").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)
    'Set wEditor = outApp.ActiveInspector.WordEditor
    ' Set wRange = wEditor.Application.ActiveDocument.Content


    With outMail
    .To = ""
    .CC = ""
    .BCC = ""
    .Subject = "This is the Subject line"
    .HTMLBody = RangetoHTML(rng) & RangetoHTML1(rng2) & RangetoHTML2(rng3) & RangetoHTML3(rng4) '& Insert_Resized_Chart(chartsSheet)
    ' 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

    Set outApp = CreateObject("Outlook.Application")
    Set outMail = outApp.CreateItem(0)


    Set wEditor = outApp.ActiveInspector.WordEditor
    Set wRange = wEditor.Application.ActiveDocument.Content

    'Ensure subsequent inserts and pastes appear above automatic email signature

    wRange.Collapse 1 'Direction:=wdCollapseStart

    wRange.InsertAfter "Text at top" & vbNewLine
    wRange.Collapse 0 'Direction:=wdCollapseEnd


    Set chartObj = chartsSheet2.ChartObjects("Chart 1")
    Insert_Resized_Chart chartObj, chartWidthCm, chartHeightCm, wRange
    wRange.InsertBefore vbCr
    wRange.InsertAfter "" '& Time & vbNewLine
    wRange.Collapse 0 'Direction:=wdCollapseEnd



    Set chartObj = chartsSheet.ChartObjects("Chart 2")
    Insert_Resized_Chart chartObj, chartWidthCm, chartHeightCm, wRange


    Set chartObj = chartsSheet.ChartObjects("Chart 3")
    Insert_Resized_Chart chartObj, chartWidthCm, chartHeightCm, wRange
    wRange.InsertParagraphAfter
    wRange.InsertAfter "" '& Time & vbNewLine
    wRange.Collapse 0 'Direction:=wdCollapseEnd


    Set chartObj = chartsSheet.ChartObjects("Chart 1")
    Insert_Resized_Chart chartObj, chartWidthCm, chartHeightCm, wRange


    Set chartObj = chartsSheet3.ChartObjects("Chart 1")
    Insert_Resized_Chart chartObj, chartWidthCm, chartHeightCm, wRange
    wRange.InsertParagraphAfter
    wRange.InsertAfter "" '& Time & vbNewLine
    wRange.Collapse 0 'Direction:=wdCollapseEnd



    End Sub

    Function RangetoHTML(rng As Range)
    ' By Ron de Bruin.
    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.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 xublishsource=", _
    "align=left xublishsource=")

    '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
    Function RangetoHTML1(rng2 As Range)
    ' By Ron de Bruin.
    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
    rng2.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)
    RangetoHTML1 = ts.ReadAll
    ts.Close
    RangetoHTML1 = Replace(RangetoHTML1, "align=center xublishsource=", _
    "align=left xublishsource=")

    '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
    Function RangetoHTML2(rng3 As Range)
    ' By Ron de Bruin.
    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
    rng3.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)
    RangetoHTML2 = ts.ReadAll
    ts.Close
    RangetoHTML2 = Replace(RangetoHTML2, "align=center xublishsource=", _
    "align=left xublishsource=")

    '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

    Function RangetoHTML3(rng4 As Range)
    ' By Ron de Bruin.
    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
    rng4.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)
    RangetoHTML3 = ts.ReadAll
    ts.Close
    RangetoHTML3 = Replace(RangetoHTML3, "align=center xublishsource=", _
    "align=left xublishsource=")

    '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

    Private Sub Insert_Resized_Chart(thisChartObject As ChartObject, newWidthCm As Single, newHeightCm As Single, wordRange As Object)

    'Arguments
    'thisChartObject - the ChartObject to be resized
    'newWidthCm - new width in centimeters
    'newHeighCm - new height in centimeters
    'wordRange - the current position in the email, as a Word.Range object

    Dim chartShape As Shape
    Dim currentWidth As Single
    Dim currentHeight As Single

    'Get the chart as a Shape

    Set chartShape = thisChartObject.Parent.Shapes(thisChartObject.Name)

    'Change chart to new dimensions

    With chartShape
    currentWidth = .Width
    currentHeight = .Height
    .Width = Application.CentimetersToPoints(newWidthCm)
    .Height = Application.CentimetersToPoints(newHeightCm)
    Debug.Print "Before: "; currentWidth; currentHeight, "After: "; .Width; .Height
    End With

    'Insert chart into email

    thisChartObject.Chart.ChartArea.Copy
    wordRange.PasteSpecial , , , , 4 'DataType:=wdPasteBitmap

    'Restore original dimensions

    With chartShape
    .Width = currentWidth
    .Height = currentHeight
    End With


    End Sub

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Please read our FAQ
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •