arsentievalex
New Member
- Joined
- Mar 18, 2020
- Messages
- 15
- Office Version
- 365
- Platform
- Windows
Hello,
I'm trying to set up a macro that would send an outlook email. I've run into an issue when adding a table from sheet2 to the email body code. Basically, what I want to accomplish is an email that contains two tables from sheet1 and sheet2, sheet1 works perfectly, however, sheet2 does not pull through for some reason. I'm using code in two modules, one for email and other for range selection. I'm attaching the codes that I'm using. I believe, the issue is that I need to select sheet2 somewhere but cannot find it. Any help would be greatly appreciated
I'm trying to set up a macro that would send an outlook email. I've run into an issue when adding a table from sheet2 to the email body code. Basically, what I want to accomplish is an email that contains two tables from sheet1 and sheet2, sheet1 works perfectly, however, sheet2 does not pull through for some reason. I'm using code in two modules, one for email and other for range selection. I'm attaching the codes that I'm using. I believe, the issue is that I need to select sheet2 somewhere but cannot find it. Any help would be greatly appreciated
VBA Code:
Sub Email_range()
Dim OutApp As Object
Dim OutMail As Object
Dim count_row, count_col As Integer
Dim table1 As Range
Dim table2 As Range
Dim str1, str2, str3 As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
count_row = WorksheetFunction.CountA(Range("A1", Range("A1").End(xlDown)))
count_col = WorksheetFunction.CountA(Range("A1", Range("A1").End(xlToRight)))
Set table1 = Sheets("Sheet1").Range(Cells(1, 1), Cells(count_row, count_col))
Set table2 = Sheets("Sheet2").Range(Cells(1, 1), Cells(count_row, count_col))
str1 = "<BODY STYLE = font-size12pt/font-family:Calibri>" & _
"Good morning,”<br>
str2 = "<br>Please see below."<br>
str3 = "<br>Best regards."
On Error Resume Next
With OutMail
.to = "[EMAIL]thisistest@test.com[/EMAIL]"
.CC = " "
.Subject = "Daily Report"
.Display
.HTMLBody = str1 & RangetoHTML(table2) & str2 & RangetoHTML(table1) & .HTMLBody
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
VBA Code:
Function RangetoHTML(rng As Range)
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 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
Last edited by a moderator: