Sub Flag()
'
' Flag Macro
'
'
msg = "E-mails will be automatically generated and sent for any Audits Scheduled that have reached their Flag date." & vbCrLf & "" & vbCrLf & "Do you wish to proceed?"
ans = MsgBox(msg, vbYesNo)
Select Case ans
Case vbYes
Sheets("Automatic Emails").Select
Range("A2:K30").Select
Selection.ClearContents
Range("A1").Select
Sheets("Aerospace Schedule").Select
For Each lo In ActiveSheet.ListObjects
lo.AutoFilter.ShowAllData
Next lo
Cells.Select
ActiveSheet.ListObjects("Table2").Range.AutoFilter Field:=12, Criteria1:= _
"Y"
LastRowColumnA = Cells(Rows.Count, 1).End(xlUp).Row
Range("A2:K" & LastRowColumnA).Select
Selection.Copy
Sheets("Automatic Emails").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Range("L2").Select
LastRowColumnA = Cells(Rows.Count, 1).End(xlUp).Row
Range("L2:L" & LastRowColumnA).Formula = "=INDEX(C[2],MATCH(RC[-8],C[1],FALSE))"
Columns("G:G").Select
Selection.NumberFormat = "m/d/yyyy"
With Selection
.HorizontalAlignment = xlCenter
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Columns("J:J").Select
Selection.NumberFormat = "m/d/yyyy"
With Selection
.HorizontalAlignment = xlCenter
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Columns("K:K").Select
Selection.NumberFormat = "m/d/yyyy"
With Selection
.HorizontalAlignment = xlCenter
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("B1").Select
'EMAIL CODE
Call sendmail
msg = "Reminder e-mails sent"
Case vbNo
GoTo Quit:
End Select
Quit:
End Sub
Sub sendmail()
Dim OutlookApp As Object, MItem As Object
Dim i As Long, sh As Worksheet, rng As Range, lr As Long
Set sh = Sheets("Automatic Emails")
lr = sh.Range("B" & Rows.Count).End(xlUp).Row
Set rng = sh.Range("B1:K" & lr)
For i = 2 To lr
Set OutlookApp = CreateObject("Outlook.Application")
Set MItem = OutlookApp.CreateItem(0)
With MItem
.To = sh.Range("L" & i).Value
.Subject = sh.Range("Q1").Value
.HTMLBody = sh.Range("Q2").Value & RangetoHTML(rng) & _
"<br>[This is an Automated Message - Do not reply]<br>" & _
"Quality Department"
.Display
.Send
End With
Next
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