-
How to position charts and tables to outlook and how to resize different charts
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
PublicSub Insert_Charts_In_New_Email()
Dim outApp AsObject'Outlook.Application
Dim outMail AsObject'Outlook.MailItem
Dim wEditor AsObject'Word.Document
Dim wRange AsObject'Word.Range
Dim chartsSheet AsObject
Dim chartObj As ChartObject
Dim chartWidthCm AsSingle, chartHeightCm AsSingle
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 IsNothingThen
MsgBox "The selection is not a range or the sheet is protected. "& _
vbNewLine &"Please correct and try again.", vbOKOnly
ExitSub
EndIf
With Application
.EnableEvents =False
.ScreenUpdating =False
EndWith
Set outApp = CreateObject("Outlook.Application")
Set outMail = outApp.CreateItem(0)
With outMail
.To=""
.CC =""
.BCC =""
.Subject ="This is the Subject line"
.HTMLBody = RangetoHTML(rng)& RangetoHTML1(rng2)& RangetoHTML2(rng3)& RangetoHTML3(rng4)
' In place of the following statement, you can use ".Display" to
' display the e-mail message.
.Display
EndWith
OnErrorGoTo0
With Application
.EnableEvents =True
.ScreenUpdating =True
EndWith
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 ""
wRange.Collapse 0
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 ""
wRange.Collapse 0
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 ""
wRange.Collapse 0
EndSub
Function RangetoHTML(rng As Range)
' By Ron de Bruin.
Dim fso AsObject
Dim ts AsObject
Dim TempFile AsString
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
OnErrorResumeNext
.DrawingObjects.Visible =True
.DrawingObjects.Delete
OnErrorGoTo0
EndWith
'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)
EndWith
'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
EndFunction
Function RangetoHTML1(rng2 As Range)
' By Ron de Bruin.
Dim fso AsObject
Dim ts AsObject
Dim TempFile AsString
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
OnErrorResumeNext
.DrawingObjects.Visible =True
.DrawingObjects.Delete
OnErrorGoTo0
EndWith
'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)
EndWith
'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
EndFunction
Function RangetoHTML2(rng3 As Range)
' By Ron de Bruin.
Dim fso AsObject
Dim ts AsObject
Dim TempFile AsString
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
OnErrorResumeNext
.DrawingObjects.Visible =True
.DrawingObjects.Delete
OnErrorGoTo0
EndWith
'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)
EndWith
'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
EndFunction
Function RangetoHTML3(rng4 As Range)
' By Ron de Bruin.
Dim fso AsObject
Dim ts AsObject
Dim TempFile AsString
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
OnErrorResumeNext
.DrawingObjects.Visible =True
.DrawingObjects.Delete
OnErrorGoTo0
EndWith
'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)
EndWith
'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
EndFunction
PrivateSub Insert_Resized_Chart(thisChartObject As ChartObject, newWidthCm AsSingle, newHeightCm AsSingle, wordRange AsObject)
'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 AsSingle
Dim currentHeight AsSingle
'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
EndWith
'Insert chart into email
thisChartObject.Chart.ChartArea.Copy
wordRange.PasteSpecial ,,,,4'DataType:=wdPasteBitmap
'Restore original dimensions
With chartShape
.Width = currentWidth
.Height = currentHeight
EndWith
EndSub
-
How do you suggest we test this code to determine its results?
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
-
Forum Rules