EvolvedFromQuincy
New Member
- Joined
- Jan 27, 2022
- Messages
- 4
- Office Version
- 365
- Platform
- Windows
Hi all,
I'm trying to automate sending filtered portions of reports in e-mail bodies sent to the people the report is filtered by. I'm already using Ron de Bruin's excellent range to HTML function, but I need some help making my e-mails more personal by starting with a first name salutation, "Hi 'person,'" I'm hoping there's some way I can isolate the first name from the Last Name, First name column (Column Q) as a variable that I can put into the .HTMLBody of the e-mail.
Also for reference, I need my macro to filter through about 7 different people and send them all custom range to HTML of their own filtered data. I'm not sure if there's a way to loop that, but would be interested if anyone has an elegant solution. Perhaps there's a way to loop through each unique non-empty/#N/A value and apply that as filter? If not, I'm fine with the redundancy of making 7 different 'With OutMails', and I realize the risk of manually having to edit this code each time a person changes roles or leaves the company.
And finally, as an etiquette question, is it normal to leave other peoples' signatures in comments of code? Most of this code is Ron de Bruin's, but at some point I deleted some comments before I realized it might be an issue.
With that, here is a sample of what I have so far:
Thank you!
I'm trying to automate sending filtered portions of reports in e-mail bodies sent to the people the report is filtered by. I'm already using Ron de Bruin's excellent range to HTML function, but I need some help making my e-mails more personal by starting with a first name salutation, "Hi 'person,'" I'm hoping there's some way I can isolate the first name from the Last Name, First name column (Column Q) as a variable that I can put into the .HTMLBody of the e-mail.
Also for reference, I need my macro to filter through about 7 different people and send them all custom range to HTML of their own filtered data. I'm not sure if there's a way to loop that, but would be interested if anyone has an elegant solution. Perhaps there's a way to loop through each unique non-empty/#N/A value and apply that as filter? If not, I'm fine with the redundancy of making 7 different 'With OutMails', and I realize the risk of manually having to edit this code each time a person changes roles or leaves the company.
And finally, as an etiquette question, is it normal to leave other peoples' signatures in comments of code? Most of this code is Ron de Bruin's, but at some point I deleted some comments before I realized it might be an issue.
With that, here is a sample of what I have so far:
VBA Code:
Sub Test_Email()
Application.ReferenceStyle = xlA1
Dim OutApp As Object
Dim OutMail As Object
Dim rng As Range
Dim StrBody As String
Dim SigString As String
Dim Signature As String
Set rng = Nothing
ActiveSheet.ShowAllData
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=17, Criteria1:= _
"Smith, Bob"
Set rng = Selection.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
StrBody = StrBody & "Hi " FirstNameVariable? & ","
StrBody = StrBody & "Something something something "
StrBody = StrBody & "Something else something else something else."
For Each cell In Columns("R").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value Like "?*@?*.?*" Then
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.to = Cells(cell.Row, "R").Value
.cc = ""
.BCC = ""
.Subject = "Subject"
.HTMLBody = StrBody & RangetoHTML(rng) & "<br>" & "Thank you," & "<br><br>" & Signature
.Send
End With
End If
<new filter for different person>
<repeat above>
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
Thank you!