Excel VBA to automatically add Cell Value to Outlook Subject Line

kmclaugh

New Member
Joined
Feb 7, 2018
Messages
3
Hello – I have a spreadsheet with columns “A thru AG” and ~1000 rows where I need to send daily emails for all late orders. I am currently using Ron De Bruins macro where it loops through the spreadsheet and pulls all the unique email addresses from Column “D” and copies them to a new worksheet and then send an email with all the late order information using the “Range to html” macro.

I am also trying to pull in a column value (Order Number - Column “A”) into the Subject Line of Outlook to reference the late order information being sent. Below is an example of the macro. I have been searching for a way to add this, but am at a complete loss. Any help would be greatly appreciated. Thanks in advance!
Order Number
Line Number
Deliver ToEmail
Late
40028253​
1​
Test 1test1@gmail.com
12​
40031001​
1​
Test 2test3@gmail.com
7​
50000932​
4​
Test 3text3@gmail.com
2​
11000765​
2​
Test 4test1@gmail.com
5​

VBA Code:
Sub Send_Row_Or_Rows_2()

'For Tips see: 'http://www.rondebruin.nl/win/winmail/Outlook/tips.htm' Excel Automation - Ron de Bruin
'Don't forget to copy the function RangetoHTML in the module.
'Working in Excel 2000-2016
Dim OutApp As Object
Dim OutMail As Object
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 DefaultSignature 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 e-mail addresses)
Set FilterRange = Ash.Range("A1:AG" & Ash.Rows.Count)
FieldNum = 4 'Filter column = D because the filter range start in column 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 (column 4)
FilterRange.AutoFilter Field:=FieldNum, _
Criteria1:=Cws.Cells(Rnum, 1).Value

'If the unique value is a mail address create a mail
If Cws.Cells(Rnum, 1).Value Like "?*@?*.?*" Then

'Copy the visible data in a new workbook
With Ash.AutoFilter.Range
On Error Resume Next
Set rng = .SpecialCells(xlCellTypeVisible)
On Error GoTo 0
End With

Set OutMail = OutApp.CreateItem(0)
On Error Resume Next

With OutMail
.to = Cws.Cells(Rnum, 1).Value
.Subject = "Follow-up Regarding PO" & rng.Cells(Rnum, 1).Value
.Display 'Or use Send
.HTMLBody = "Hello - I'm following up on late orders, and before I contact the vendor, I wanted to check in with you to see if you received and/or completed the following orders. Thank you" & _
RangetoHTML(rng) & .HTMLBody

End With

On Error GoTo 0
Set OutMail = Nothing

End If

'Close AutoFilter

Ash.AutoFilterMode = False

Next Rnum

End If

MsgBox "Email has sent successfully!"

cleanup:
Set OutApp = Nothing
Application.DisplayAlerts = False
Cws.Delete
Application.DisplayAlerts = True

With Application
.EnableEvents = True
.ScreenUpdating = True

End With

End Sub

Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook

TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

'Copy the range and create a new workbook to past the data in
rng.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
End With

'Publish the sheet to a htm file
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

'Read all data from the htm file into RangetoHTML
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=")
'Close TempWB
TempWB.Close savechanges:=False

'Delete the htm file we used in this function
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function
 
Last edited by a moderator:

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hello - I was able to add the column "A" value to the subject line with " .Subject = "Follow-up Regarding PO " & Ash.Cells(Rnum, 1).Value", however, it is not pulling the correct order number with the unique filter on email addresses. Can anyone help me with being able to have the order numbers added to the subject line that matches the email address for each (also add multiple order numbers for those that have more than 1? I've stepped through the code line by line, but still can not figure it out...I'm truly at a loss with this:(
 
Upvote 0

Forum statistics

Threads
1,212,929
Messages
6,110,743
Members
448,295
Latest member
Uzair Tahir Khan

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