guptapradeep433
New Member
- Joined
- Jan 14, 2023
- Messages
- 7
- Office Version
- 2019
- 2016
- Platform
- Windows
Hi.. I have executed this code as its working as per my requirement but issue with ".To " Its selecting only one email id from Range "B2" and applying to all the record whereas in each record their own email id should come. Please help with this
VBA Code:
Sub mailstrangejosh()
Dim OutApp As Object, OutMail As Object
Dim myRng As Range, v As Variant
Dim j As Long, lastRow As Long
Dim strbody As String
Dim outlookmailitem As Object
Dim edress As String
Application.ScreenUpdating = False
lastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
v = Range("A1:X" & lastRow).Value
Set OutApp = CreateObject("Outlook.Application")
With CreateObject("scripting.dictionary")
For j = 2 To lastRow
If Not .exists(v(j, 2)) Then
.Add v(j, 2), Nothing
strbody = "Hello , " & v(j, 20) & "<br></br>" & vbNewLine & vbNewLine & _
"<br> Please find the below banker feedback details. Thank you" & "<br/><br>"
With ActiveSheet
.Range("A1").AutoFilter 2, v(j, 2)
Set myRng = .Range("A1:I" & lastRow).SpecialCells(xlCellTypeVisible)
Set OutMail = OutApp.CreateItem(0)
With OutMail
'.To = v(j, 20)
.To = Range("B2")
.Subject = v(j, 10) & "Banker Feedback"
.HTMLBody = strbody & RangetoHTML(myRng)
.display 'to show
'.Send 'to send
End With
End With
End If
Next j
End With
Range("A1").AutoFilter
Application.ScreenUpdating = True
End Sub
Function RangetoHTML(myRng As Range)
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook
Dim i As Integer
TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
myRng.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
For i = 7 To 12
With .UsedRange.Borders(i)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlMedium
End With
Next i
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=")
TempWB.Close savechanges:=False
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function