Sub testing()
Dim xOutApp As Object, xOutMail As Object
Dim xMailBody As String
Dim rng As Range
Dim lastrow As Long, lastrow2 As Long, x As Long
If ActiveSheet.AutoFilterMode = True Then ActiveSheet.AutoFilterMode = False
Range("A1:E1").AutoFilter
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
Range("E2:E" & lastrow).Copy Range("E" & lastrow + 10)
lastrow2 = Cells(Rows.Count, 5).End(xlUp).Row
Range("E" & lastrow + 10 & ":E" & lastrow2).RemoveDuplicates Columns:=1
lastrow2 = Cells(Rows.Count, 5).End(xlUp).Row
xMailBody = "Good Afternoon," & vbNewLine & vbNewLine & _
"Below is the available balance as of October 31, 2022, for your respective indirect cost account(s)." & vbNewLine & vbNewLine & _
"As a reminder, please note that IDC funds do not expire (available funds roll-over year to year). Non-Research (NR) IDC funds can be used to cover the cost of office supplies, educational materials, travel, etc. which support your teaching, research, and other activities at the University. Research (R) IDC funds, though, can only be used to cover the costs of items that support your research at the University." & vbNewLine & vbNewLine & _
"Please review your available balance(s) and contact our office if you have any questions." & vbNewLine & vbNewLine & _
"Thank you," & vbNewLine & _
"OGC"
For x = lastrow + 10 To lastrow2
Rows(1).AutoFilter field:=5, Criteria1:=Range("E" & x)
Set rng = Range("A1:E" & lastrow)
eto = Range("E" & x)
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
With xOutMail
.To = eto
.CC = ""
.BCC = ""
.Subject = "Indirect Cost Account Balance(s)"
.htmlbody = xMailBody & "<br>" & RangetoHTML(rng) & .htmlbody
.Display
End With
Set xOutMail = Nothing
Set xOutApp = Nothing
eto = ""
Next x
Range("E" & lastrow + 10 & ":E" & lastrow2).ClearContents
If ActiveSheet.AutoFilterMode = True Then ActiveSheet.AutoFilterMode = False
End Sub
Function RangetoHTML(rng)
Dim obj As Object
Dim txtstr As Object
Dim File As String
Dim wb As Workbook
File = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
Set wb = Workbooks.Add(1)
With wb.Sheets(1)
rng.Copy
.Cells(1, 1).PasteSpecial Paste:=8
.Cells(1, 1).PasteSpecial xlPasteValues, , False, False
.Cells(1, 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
With wb.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=File, _
Sheet:=wb.Sheets(1).Name, _
Source:=wb.Sheets(1).UsedRange.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With
Set obj = CreateObject("Scripting.FileSystemObject")
Set txtstr = obj.GetFile(File).OpenAsTextStream(1, -2)
RangetoHTML = txtstr.readall
txtstr.Close
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
"align=left x:publishsource=")
Application.EnableEvents = False
wb.Close savechanges:=False
Kill File
Set txtstr = Nothing
Set obj = Nothing
Set wb = Nothing
End Function