Hi there so I am working on project and I want to add charts from excel into the body of the email using VBA. Now the code is working and I am able to copy over one chart, however when I add a for loop around it, the output is just the same three charts and the other charts end up being added as attachments.
If someone could help me output multiple charts in the body that'd be great. I've added the code below:
UserChartOpt = Application.InputBox("Would you like to add charts to the email?", "User Chart Option.", , , , , , 2)
If UserChartOpt = "Yes" Or UserChartOpt = "yes" Then
'This line will be used later to help loop when attaching the charts into the body of the email (as of now doesn't change anything)
NumbOfChart = Application.InputBox("How many charts do you want to insert?", "Number of Charts:", , , , , , 2)
xChartName = ChartName
'errorCode = 29
'If xChartName = "" Or ChartSheet = "" Then GoTo ErrorMsgs
Set xChartSheet = ThisWorkbook.Sheets(ChartSheet)
Set xChartObj = xChartSheet.ChartObjects(xChartName)
For Each xChartObj In xChartSheet.ChartObjects
ChartPath = ThisWorkbook.path & "\" & Environ("Charts") & VBA.Format(VBA.Now(), "DD_MM_YYYY") & ".bmp"
Pth = "<p align='Left'><img src= ""cid:" & Mid(ChartPath, InStrRev(ChartPath, "\") + 1) & """ width = 350 height = 200> <br> <br>"
xChartObj.Chart.Export ChartPath
.attachments.Add ChartPath
tempBody = Pth & tempBody
Next xChartObj
'.Display
'Kill ChartPath
'errorCode = 11
If HTMLBodyRange = "" Then
.HTMLBody = RangetoHTML(Sheets(HTMLBodySheet).UsedRange)
Else
.HTMLBody = RangetoHTML(Sheets(HTMLBodySheet).Range(HTMLBodyRange))
End If
'Just to help email look more professional, however the third line is what allows the charts to display as an image rather than an attachment
OpeningMsg = "<font size='2.5' color='black'> Hi There," & "<br> <br>" & "Please find the chart below: " & "<br> <br> </font>"
ClosingMsg = "<br><br>" & "<font size =2.5' color='black'> Cheers, </font>"
.HTMLBody = OpeningMsg & tempBody & .HTMLBody & ClosingMsg