I wrote the code pasted below (copied and pasted some of it) and it works fine, but I need help to edit it. The spreadsheets copy to the email but there are a lot of empty rows between the top and bottom of the table. The amount of data in the table changes daily so some days there are more empty rows than others.
Here is what I need:
- An edited version of this code that scans the data table and gets rid of the empty rows before the table is populated in the email
OR
- An edited version of this code that makes the tables dynamic so that only the rows that have data show up and no deleting empty rows is necessary.
I had to edit the code to not reveal any info about my company, so if it looks a little off, that is why.
Here's the code:
Sub ToOutlook()
Dim OutApp As Object
Dim OutMail As Object
Dim rg1 As Range, rg2 As Range
Dim str1 As String, str2 As String
Dim nextBusinessDay As Date
nextBusinessDay = GetNextBusinessDay(Date)
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Set rg1 = Sheets("DailyActivity").Range(Cells(1, 1), Cells(17, 7))
Set rg2 = Sheets("DailyActivity").Range(Cells(19, 1), Cells(35, 7))
str1 = "<BODY style = font-size:12pt;font-family:Calibri>" & _
"All, <p> Please see below for the activity scheduled for " & Format(nextBusinessDay, "dddd, d/m.")
str2 = "<br> Thanks,"
On Error Resume Next
With OutMail
.To = ""
.CC = ""
.BCC = ""
.Subject = " Activity - " & Format(nextBusinessDay, "d/m")
.Display
.HTMLBody = str1 & RangetoHTML(rg1) & RangetoHTML(rg2) & str2 & .HTMLBody
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
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
Function GetNextBusinessDay(ByVal startDate As Date) As Date
Dim nextDay As Date
nextDay = startDate
Do
nextDay = nextDay + 1 ' Increase the date by one day
' Check if the next day is a weekend (Saturday or Sunday)
If Weekday(nextDay, vbMonday) < 6 Then
' If it's a weekday, exit the loop and return the next business day
Exit Do
End If
Loop
GetNextBusinessDay = nextDay
End Function