Hi, David,
Thanks for the suggestion! It got me much closer to where I wanted to be, but still needed a lot of additional code to do what I wanted. This included adding the Function RangetoHTML(rng As Range) code from Ron de Bruin's page, without which the code will never run. At first, I thought I wanted a block of cells that would be in another file to be copied and pasted into the email, then realized that could make a very long email, so I created a macro ("FindGrandTotal") to only pull the information on the Grand Total line plus the approprite column headers.
Here is what I ended up using. I'm hoping someone else out there will find it of some use. I'm picking it up where the
is created. The Function RangetoHTML(rng As Range) code is shown below that.
Dim OutApp As Object
Dim OutMail As Object
Dim rng As Range
Dim strbody, strbody2, strBodyRng As String
FindGrandTotal
Set rng = Range("A1:D2")
strBodyRng = RangetoHTML(rng)
Sheets("Daily Production Aggr by Dist").Select
strbody = "<HTML><BODY><p style='font-family:calibri;font-size:14.5'>" & "Hi, <br><br>" & _
"Here is the Daily Production report for " & Application.Text(ActiveSheet.Range("B1"), "mmmm d, yyyy") & ". " & _
" A summary of the production is below: "
strbody2 = "<HTML><BODY><p style='font-family:calibri;font-size:14.5'>If you have any questions regarding the accuracy of this report, please check to be sure numbers were entered correctly. " & _
"If there are still problems, please contact me. <br><br>" & _
"Thanks,<br><br>" & "Bryan<br><br></p>"
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.to = "Person@MyDomain.com"
.Subject = "Daily Production Report for " & Application.Text(ActiveSheet.Range("B1"), "M/D/YYYY")
.HTMLbody = strbody & strBodyRng & strbody2
.Attachments.Add ActiveWorkbook.FullName
.Display 'or use .Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
ActiveWindow.Close
Application.DisplayAlerts = True
End Sub
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 paste 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).PasteSpecial xlPasteColumnWidths, , 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
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=")
TempWB.Close savechanges:=False
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function
Sub FindGrandTotal()
Cells.Find(What:="Grand Total", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
Selection.End(xlToRight).Select
Range(Selection, Selection.Offset(0, 5)).Copy
Sheets.Add After:=Sheets(Sheets.Count)
Range("A2").Select
ActiveSheet.Paste
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("A1").Select
Sheets("Daily Production").Select
Range("E2:J2").Copy
Sheets("Sheet2").Select
ActiveSheet.Paste
Range("A1:F2").Select
Application.CutCopyMode = False
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
End With
With Selection.Font
.Name = "Calibri"
.Size = 11.5
End With
Columns("A:C").ColumnWidth = 10.86
Columns("D:F").ColumnWidth = 13.14
Range("A1").Select
End Sub
Bookmarks