PDA

View Full Version : [SOLVED] Email Range in Body of Email - Not pasting the data



hmltnangel
09-28-2017, 05:04 AM
Hi folks, not been on in a while as all has been good on the excel front. Sadly I am having an off day and cannot for the life of me get The following to work.

:banghead:

When I click the button assigned to the code - it displays the new email with recipients and subject - however it will not paste in the excel range or teh textstring. Any Suggestions would be welcome :)

I know it will be something silly but for the life of me - I am stumped today


Sub Mail_Selection_Range_Outlook_Body()
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim StrBody As String

StrBody = "Afternoon All," & "<br>" & "<br>" & _
"Please see below for the latest Daily Update." & "<br><br>"

Set rng = Nothing
On Error Resume Next
Set rng = Sheets("Output").Range("B4:W56").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 = "***x"
.CC = ""
.BCC = ""
.Subject = "Daily Update ***X"
.HTMLBody = StrBody & RangetoHTML(rng)
.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)
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"

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

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=")
'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

mdmackillop
09-28-2017, 06:35 AM
Your code works for me. Got data in Output?

hmltnangel
09-28-2017, 06:59 AM
Hahah, nope, it shows a completely blank email. Has all the recipients and subjects in it but no data in the email at all. Is there an outlook setting I may be missing in Outlook?

mdmackillop
09-28-2017, 07:22 AM
Does
.HTMLBody = StrBody insert the value?

mdmackillop
09-28-2017, 07:38 AM
or check that the data is there

ActiveWorkbook.FollowHyperlink TempFile
Stop
Set fso = CreateObject("Scripting.FileSystemObject")

hmltnangel
09-28-2017, 02:25 PM
Does
.HTMLBody = StrBody insert the value?

It doesn't input the text string either. All I get is a totally blank email, with the recipients and subject.

As a side note, it works if I unhide some columns that are hidden.

Kenneth Hobs
09-28-2017, 04:44 PM
When debugging, comment out OnError lines. Step through code with F8.

Is your sheet protected? If so, let the code make changes. e.g.

Sheeet1.Protect "yourPasswordHereOrBlank", UserInterfaceOnly:=True

hmltnangel
09-28-2017, 11:48 PM
Sheets are not protected. It only fails if columns are hidden. If I unhide all columns, then it works fine.

hmltnangel
09-29-2017, 04:17 AM
Yeeeeha. I got it to work by electing its own range in the temp book and deleting what I didnt want in the temp workbook - instead of hiding columns etc.


Sub Mail_Selection_Range_Outlook_Body()
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim StrBody As String

StrBody = "Afternoon All," & "<br>" & "<br>" & _
"Please see below for the latest Daily Update." & "<br><br>"

Set rng = Nothing
On Error Resume Next
Set rng = Sheets("Output").Range("B4:W56").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 = "***x"
.CC = ""
.BCC = ""
.Subject = "Daily Update ***X"
.HTMLBody = StrBody & RangetoHTML(rng)
.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)
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"

Range("B2:W56").Select
Selection.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

With TempWB.Sheets(1)
Columns("P:P").Delete Shift:=xlToLeft
Columns("R:R").Delete Shift:=xlToLeft
On Error GoTo 0
End With
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=")
'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

Kenneth Hobs
09-29-2017, 05:19 AM
If that is the case, then record a macro to unhide/hide if you don't know the syntax.