PDA

View Full Version : [SOLVED] Send e-mail with multiple range in body of e-mail



thepast
04-04-2018, 06:30 AM
I have downloaded a macro from Ron de Bruin.
Only the macro does not what I want.....

In the attached files are two buttons, one for printing and the other for e-mailing
With the button for e-mailing I have a problem.
I don't get 2 ranges in the body of the e-mail.
The body of the e-mail should looks like the printed version.

Is there someone who can take a look what I am doing wrong?

Thanks for al the help.

SamT
04-08-2018, 03:16 PM
Moderator Bump

Logit
04-08-2018, 06:56 PM
.
Replace your existing code with the following. Paste all of this code in a Routine Module :



Option Explicit
Sub CopyRows()
Dim i As Integer
Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Sheets("Sheet1")
ws1.Range("A1:AF22").Copy


Dim wbTemp As Workbook: Set wbTemp = ActiveWorkbook


Mail_Selection_Range_Outlook_Body

End Sub


Sub Mail_Selection_Range_Outlook_Body()
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim ctr As Integer
Dim lEndRow
Dim rngStr As String
Dim Value As String


Set rng = Nothing
' Only send the visible cells in the selection.
Set rng = Sheets("Sheet1").Range("A1:AF22")


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

Dim strbody As String
Dim HTML As String

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


strbody = Range("e2").Text & vbNewLine & vbNewLine & _
""

On Error Resume Next


With OutMail
.To = "mail@myself.nl"
.CC = ""
.BCC = ""
'.SentOnBehalfOfName = "todayshow@stuhrling.com"
.Subject = "Please reply before the meeting."
.HTMLBody = strbody & "<br><br>" & "<br><br>" & RangetoHTML(rng)




' In place of the following statement, you can use ".Display" to
' display the e-mail message.
.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"
'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




Also, as long as you use an ActiveX button to activate the macros ... in the Sheet1 Module, paste this macro :



Private Sub CommandButton2_Click()
CopyRows
End Sub

thepast
04-09-2018, 07:41 AM
Thanks Logit,

It works and was exactly what I needed.

Logit
04-09-2018, 10:09 AM
.
You are welcome.