Sub Test()
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim AtLoc As Integer
Dim L As Integer
Dim i As Integer
Set mailRng = Range(Range("D2"), Range("D" & Rows.Count).End(xlUp))
Set linkRng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
Set rng = Range("A1:B4")
For Each mailAddr In mailRng
For Each Cell In linkRng
s = Cell.Hyperlinks(1).Address
'https://www.coursera.org/lecture/excel-vba-for-creative-problem-solving-part-2/example-extracting-email-addresses-from-mixed-string-formats-Rydqz
'Extracts the current email address in the link
L = Len(s)
AtLoc = InStr(s, "@")
For i = AtLoc - 1 To 1 Step -1
If Mid(s, i, 1) = "=" Then
StartLoc = i + 1
Exit For
ElseIf i = 1 Then
StartLoc = 1
End If
Next i
For i = AtLoc + 1 To L
If Mid(s, i, 1) = "&" Then
EndLoc = i - 1
Exit For
ElseIf i = L Then
EndLoc = L
End If
Next i
Email = Mid(s, StartLoc, EndLoc - StartLoc + 1)
'Replaces the current address with the one in Column D and rewrites the link
Addr = Cell.Address(0, 0)
Hlink = Cell.Hyperlinks(1).Address
Hlink = Replace(Hlink, Email, mailAddr)
ActiveSheet.Hyperlinks.Add Range(Addr), Address:=Hlink, TextToDisplay:="Link"
Next
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = mailAddr
.CC = ""
.BCC = ""
.Subject = "Test"
.HTMLBody = RangetoHTML(rng)
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
Next
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
Function RangetoHTML(rng As Range)
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook
Dim i As Integer
Dim Hlink As Hyperlink
TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
Application.ScreenUpdating = False
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
.UsedRange.EntireColumn.AutoFit
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
For i = 7 To 12
With .UsedRange.Borders(i)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlMedium
End With
Next i
End With
'https://stackoverflow.com/questions/35268674/how-to-preserve-retain-hyperlinks-in-email-body-when-using-rangetohtml-from-exce
For Each Hlink In rng.Hyperlinks
TempWB.Sheets(1).Hyperlinks.Add _
Anchor:=TempWB.Sheets(1).Range(Hlink.Range.Address), _
Address:=Hlink.Address, _
TextToDisplay:=Hlink.TextToDisplay
Next Hlink
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
Application.ScreenUpdating = True
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function