learningVBA321
New Member
- Joined
- Jul 10, 2017
- Messages
- 30
Hello, not sure how to best title this, but I have a sheet from which I loop through each row and create an email for each row. Attachments are based on the Division name. Currently, it creates an email for every row, so if one person under Name has, say 8 divisions, they will receive 8 emails, each with a different attachment. This is annoying people, so I want to have it now loop (maybe nested?) and if if finds the same Name, then create one email for that Name, with all their Divisions attached. To make it easier, I have set the list so that any dupe Names are all grouped together. In this example, I would want it to create one email to the Name Sample Sample1, with attachments for Widgets and Doorknobs. Then for the rest, they would each get their usual one email. I have tried for hours to get this to work, but simply do not have enough VBA knowledge to make this work. I can do it in Excel itself with formulas, basically saying that if A2=A3, then do this. But I need help to get this to happen in VBA. Please see the image and the code I have working currently.
Thanks!
Apparently I cannot add a picture unless it is a URL? So here is how it looks:
A B C
Name Email Division
Sample1 sample1@org.com Widgets
Sample1 sample1@org.com Doorknobs
Sample2 sample2@org.com Brooms
Sample3 sample3@org.com Mops
Here is the code:
Thanks!
Apparently I cannot add a picture unless it is a URL? So here is how it looks:
A B C
Name Email Division
Sample1 sample1@org.com Widgets
Sample1 sample1@org.com Doorknobs
Sample2 sample2@org.com Brooms
Sample3 sample3@org.com Mops
Here is the code:
Code:
Sub DivisionRpt()
'
'
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Dim strdir As String
Dim strFilename As String
Dim sigString As String
Dim strBody As String
Dim strName As String
Dim strName1 As String
Dim strDept As String
Dim strName2 As String
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
sigString = Environ("appdata") & _
"\Microsoft\Signatures\Division.htm"
If Dir(sigString) <> "" Then
signature = GetBoiler(sigString)
Else
signature = ""
End If
strdir = "z:\"
strBody = "[FONT=calibri]Please review the attached report for your division.
"
For Each cell In Columns("B").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value Like "?*@?*.?*" And _
LCase(Cells(cell.Row, "C").Value) = "yes" Then
Set OutMail = OutApp.CreateItem(0)
With OutMail
strName = Cells(cell.Row, "a").Value
strName1 = Cells(cell.Row, "d").Value
strName2 = Left(strName, InStr(strName & " ", " ") - 1)
strFilename = Dir("z:\" & strName1 & "*")
.To = cell.Value
.Subject = "Monthly Budget Deficit Report for " & strName1
.HTMLBody = "[FONT=calibri]" & "Dear " & strName2 & ",
" & signature
.Attachments.Add strdir & strFilename
.Display 'Or use Send
End With
Set OutMail = Nothing
End If
Next cell
End Sub
[/FONT][/FONT]