PDA

View Full Version : [SOLVED] Emailing from Excdel only rows with data showing



oam
03-06-2015, 05:10 PM
I have a list of employees in data page in Excel 2007 and the data page is linked to the report page by formulas.
I need to be able to email only the completed rows of names in the report page and Ron de Bruin’s email code does a good job of this but the code is seeing the formula links in the report page and is selecting the entire page. I tried a formula that would give me the cell addresses of the Non-Blank cell and placed the address into a cell but Ron’s code does not see the cell addresses as “addresses” but as data and returns the content of the cell (example: A1:A8). If I could get the code to see the address as an address as opposed to data Ron’s code should work.

Is there a way to email from Excel 2007 only the completed rows of names by using Ron de Bruin’s email code and omit the formulas?

Thank you for any and all help on this matter.

Yongle
03-07-2015, 04:55 AM
How about a possible workaround until you get a proper solution?
You say "If I could get the code to see the address as an address as opposed to data Ron’s code should work."
Give the code what it wants, run the code, then change everything back to what it was before.

Macro to Copy and Paste values on addresses range
Run your email macro
Macro to re-instate the original formula

If nothing else it will prove (or disprove) your theory

oam
03-07-2015, 10:34 AM
I was able to determine the Non-Blank cell by using the formula shown below. Which with a little manipulation I came up with the cell ranges (example: A1:A8) and place the results in cell “F4” then referenced the cell in the email macro. What I need the email macro to look at cell reference "F4" and uses the contents of that cell as the email Range and not data.

Hope this makes sense




MAX(ROW(A1:A52)*(A1:A52<>"")) to find last filled row








Sub Mail_Selection_Range_Outlook_Body()
'Don't forget to copy the function RangetoHTML in the module.
'Working in Excel 2000-2013
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object

Set rng = Nothing
On Error Resume Next
'Only the visible cells in the selection
Set rng = Selection.SpecialCells(xlCellTypeVisible)
'You can also use a fixed range if you want
'Set rng = Sheets("DataSheet").Range("F4").SpecialCells(xlCellTypeVisible)
On Error GoTo 0

If rng Is Nothing Then
MsgBox "The selection is not a range or the sheet is protected" & _
vbNewLine & "please correct and try again.", vbOKOnly
Exit Sub
End If

With Application
.EnableEvents = False
.ScreenUpdating = False
End With

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
With OutMail
.To = ""
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.HTMLBody = RangetoHTML(rng)
.Send 'or use .Display
End With
On Error GoTo 0

With Application
.EnableEvents = True
.ScreenUpdating = True
End With

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-2013
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 GetBoiler(ByVal sFile As String) As String
'Dick Kusleika
Dim fso As Object
Dim ts As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)
GetBoiler = ts.readall
ts.Close
End Function