kshitij_dch
Active Member
- Joined
- Apr 1, 2012
- Messages
- 362
- Office Version
- 365
- 2016
- 2007
- Platform
- Windows
Hi,
I'm using Ron Debruin's AutoEmail Macro
I've been using it for a while with a lot of success, and now I'm migrating it to another task. It works perfectly, but I want one additional piece to make my life easier.
In the subject of the email, I want the value from Columns I. Now I know that these will have to be declared as a variables, so that's going to be maybe a little difficult.
If it helps,the value in Column I is always going to be the value that's being looked up in the mapping table in order to find the email address.
Below is the code i am using
I'm using Ron Debruin's AutoEmail Macro
I've been using it for a while with a lot of success, and now I'm migrating it to another task. It works perfectly, but I want one additional piece to make my life easier.
In the subject of the email, I want the value from Columns I. Now I know that these will have to be declared as a variables, so that's going to be maybe a little difficult.
If it helps,the value in Column I is always going to be the value that's being looked up in the mapping table in order to find the email address.
Below is the code i am using
Code:
Sub Send_Row_Or_Rows_1()
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim rng As Range
Dim Ash As Worksheet
Dim Cws As Worksheet
Dim Rcount As Long
Dim Rnum As Long
Dim FilterRange As Range
Dim FieldNum As Integer
Dim mailAddress As String
Dim mailAddress1 As String
On Error GoTo cleanup
Set OutApp = CreateObject("Outlook.Application")
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
'Set filter sheet, you can also use Sheets("MySheet")
Set Ash = ActiveSheet
'Set filter range and filter column (Column with names)
Set FilterRange = Ash.Range("A1:R" & Ash.Rows.Count)
FieldNum = 1 'Filter column = A because the filter range start in A
'Add a worksheet for the unique list and copy the unique list in A1
Set Cws = Worksheets.Add
FilterRange.Columns(FieldNum).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=Cws.Range("A1"), _
CriteriaRange:="", Unique:=True
'Count of the unique values + the header cell
Rcount = Application.WorksheetFunction.CountA(Cws.Columns(1))
'If there are unique values start the loop
If Rcount >= 2 Then
For Rnum = 2 To Rcount
'Filter the FilterRange on the FieldNum column
FilterRange.AutoFilter Field:=FieldNum, _
Criteria1:=Cws.Cells(Rnum, 1).Value
'Look for the mail address in the MailInfo worksheet
mailAddress = ""
On Error Resume Next
mailAddress = Application.WorksheetFunction. _
VLookup(Cws.Cells(Rnum, 1).Value, _
Worksheets("Mailinfo").Range("A1:B" & _
Worksheets("Mailinfo").Rows.Count), 2, False)
On Error GoTo 0
mailAddress1 = ""
On Error Resume Next
mailAddress1 = Application.WorksheetFunction. _
VLookup(Cws.Cells(Rnum, 1).Value, _
Worksheets("Mailinfo").Range("A1:C" & _
Worksheets("Mailinfo").Rows.Count), 3, False)
On Error GoTo 0
If mailAddress <> "" Then
With Ash.AutoFilter.Range
On Error Resume Next
Set rng = .SpecialCells(xlCellTypeVisible)
On Error GoTo 0
End With
Set OutMail = OutApp.CreateItem(olMailItem)
On Error Resume Next
With OutMail
.to = mailAddress
.CC = mailAddress1
.Subject = "**Please close the Lead**" & Range("I2").Value
.HTMLBody = RangetoHTML(rng)
.display 'Or use Send
End With
On Error GoTo 0
Set OutMail = Nothing
End If
'Close AutoFilter
Ash.AutoFilterMode = False
Next Rnum
End If
cleanup:
Set OutApp = Nothing
Application.DisplayAlerts = False
Cws.Delete
Application.DisplayAlerts = True
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub