Query in excel to Outlook mail take rows with name until next name

esben

New Member
Joined
Feb 15, 2018
Messages
8
Dear all,

Im trying to avoid sending mails out every week with new tasks for people. Therefor i try to create a code that can look at my query data and sent it to the correct person in outlook.

I would like to make a code which takes the header and table data for rows with specific name and blanks until next name.

The table looks like:

HeaderHeaderHeaderHeaderNot included header
NameDataDataDataData
DataData
NameDataDataDataData
DataData
DataData

I wrote some code a long time ago for sending mail for every Name on NameList.

I do not think i requires a whole lot of change to the code - but it is a bit behind me so i hope someone has a quick eye for it.

VBA Code:
Sub Overdue_Tasks()

Dim EApp As Object
Set EApp = CreateObject("Outlook.Application")

Application.ScreenUpdating = False

Dim EItem As Object

Dim NameList As Range
Set NameList = Range("A2", Range("a2").End(xlDown))

'Table header.
    Dim iColumnsCount, iColCnt As Integer
    Dim sTableHeads As String
    iColumnsCount = ActiveSheet.UsedRange.Columns.Count - 1
    
    For iColCnt = 1 To iColumnsCount
'Table header concatenated with HTML <th> tags.
        If (sTableHeads) = "" Then
            sTableHeads = "<th>" & ActiveSheet.Cells(1, iColCnt) & "</th>"
        Else
            sTableHeads = sTableHeads & "<th>" & ActiveSheet.Cells(1, iColCnt) & "</th>"
        End If
    Next iColCnt

' The list of names in excel sheet

'For Name = 2 To NameList.Count
For Name = 2 To 6

' Table data.

    Dim iRowsCount, iRows As Integer
    Dim sTableData As String
    iRowsCount = ActiveSheet.Rows.Count
     
    sTableData = "<tr>"
    For iColCnt = 1 To iColumnsCount
        If (sTableData) = "" Then
            sTableData = "<td>" & ActiveSheet.Cells(Name, iColCnt) & "</td>"
        Else
            sTableData = sTableData & "<td>" & ActiveSheet.Cells(Name, iColCnt) & "</td>"
        End If
    
    Next iColCnt
    sTableData = sTableData & "</tr>"
    

' Add CSS style to the table.
    
    Dim sTableStyle As String
    sTableStyle = "<style> table.edTable { width: 75%; font: 18px calibri; } table, table.edTable th, table.edTable td { border: solid 1px #000000; border-collapse: collapse; padding: 3px; text-align: center; } table.edTable td { background-color: #ffffff; color: #000000; font-size: 14px; } table.edTable th { background-color : #ffffff; color: #000000; } tr:hover td { background-color: #000000; color: #ffffff; } </style>"
    
    Set EItem = EApp.CreateItem(0)
    
    With EItem
        
        .To = ActiveSheet.Cells(Name, 1) & "@xxxxxx.com"
        
        '.CC = "AEIR@lundbeck.com"
        .Subject = "Overdue QN task " & ActiveSheet.Cells(Name, 1)
        .HTMLbody = "Dear " & ActiveSheet.Cells(Name, 1) & "," & "<br>" & "<br>" _
                    & sTableStyle & "<table class='edTable'><tr>" & sTableHeads & "</tr>" & sTableData & "</tr>" & "</table>" & "<br>" & "<br>" _
                    & Signature

            .Display
            'SendKeys "^{ENTER}"
        
    End With
Next Name

Application.ScreenUpdating = True

End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

Forum statistics

Threads
1,224,258
Messages
6,177,477
Members
452,782
Latest member
ZCapitao

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top