Knockout1992
New Member
- Joined
- Mar 20, 2023
- Messages
- 2
- Office Version
- 2021
- Platform
- Windows
Hello all,
I am trying get the following code to selecxt different ranges and send via outlook however I receive a blank email. Any support would be grateful. I think it is something wrong with the set rng1 thro' to set rng but I honestly do not know.
Sub Mail_Selection_Range_Outlook_Body()
Dim rng As Range
Dim rng2 As Range
Dim OutApp As Object
Dim OutMail As Object
Dim StrBody As String
Set rng = Nothing
On Error Resume Next
'Only the visible cells in the selection
Set rng1 = Range("B1:L17")
Set rng2 = Range("B64:L74")
Set rng3 = Range("B91:L101")
Set rng = Union(rng1, rng2, rng3)
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
StrBody = "Hi<br><br>You have had a CAR raised against you for " & Range("J14") & ". Please read the information below and action the required correction WITHIN 1 HOUR. Please fill out the correction info below and reply to this mail. Thank you.<br><br>CAR Reason"
senderlookup = Sheets("Lookup_List").Range("A:B")
receiverlookup = Sheets("Lookup_List").Range("C:D")
cclookup = Sheets("Lookup_List").Range("C:E")
Sender = Application.VLookup(Sheets("CAR_Input").Range("E6"), senderlookup, 2, False)
receiver = Application.VLookup(Sheets("CAR_Input").Range("J12"), receiverlookup, 2, False)
ccd = Application.VLookup(Sheets("CAR_Input").Range("J12"), cclookup, 3, False)
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = receiver
.CC = Sender & "; " & ccd
.BCC = ""
.Subject = "CAR - " & Range("J14") & Range("N1") & Range("J8").Value
.HTMLBody = StrBody & RangetoHTML(rng)
.send
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
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 xublishsource=", _
"align=left xublishsource=")
'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
I am trying get the following code to selecxt different ranges and send via outlook however I receive a blank email. Any support would be grateful. I think it is something wrong with the set rng1 thro' to set rng but I honestly do not know.
Sub Mail_Selection_Range_Outlook_Body()
Dim rng As Range
Dim rng2 As Range
Dim OutApp As Object
Dim OutMail As Object
Dim StrBody As String
Set rng = Nothing
On Error Resume Next
'Only the visible cells in the selection
Set rng1 = Range("B1:L17")
Set rng2 = Range("B64:L74")
Set rng3 = Range("B91:L101")
Set rng = Union(rng1, rng2, rng3)
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
StrBody = "Hi<br><br>You have had a CAR raised against you for " & Range("J14") & ". Please read the information below and action the required correction WITHIN 1 HOUR. Please fill out the correction info below and reply to this mail. Thank you.<br><br>CAR Reason"
senderlookup = Sheets("Lookup_List").Range("A:B")
receiverlookup = Sheets("Lookup_List").Range("C:D")
cclookup = Sheets("Lookup_List").Range("C:E")
Sender = Application.VLookup(Sheets("CAR_Input").Range("E6"), senderlookup, 2, False)
receiver = Application.VLookup(Sheets("CAR_Input").Range("J12"), receiverlookup, 2, False)
ccd = Application.VLookup(Sheets("CAR_Input").Range("J12"), cclookup, 3, False)
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = receiver
.CC = Sender & "; " & ccd
.BCC = ""
.Subject = "CAR - " & Range("J14") & Range("N1") & Range("J8").Value
.HTMLBody = StrBody & RangetoHTML(rng)
.send
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
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 xublishsource=", _
"align=left xublishsource=")
'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