Using an email variable as part of a mailer in VBA

Rashie

Board Regular
Joined
Jun 5, 2015
Messages
55
Hey All,

I'm having a bit of trouble getting this mailer to work. Well, it works, but I want the email address to be based on a cell value in the sheet itself. I'm using a lookup on column R and I want the returned value to be the email Recipient. Here's some sample data. Can someone help explain how to do this? Doesn't seem to want to be stored as a variable for me :(

IDDateOfReturnReasonRETURNBARCODECTM_NBRADDRESSEE_NAMECODEPTA1A2A3A4A5AZIPASTATEACOUNTRYCOPIESFOI
8947​
20/12/2021 08:43:22Addressee gone away46078/0000848////000000000000
0​
Mr Pete StewartGowling WLG (UK) LLP100 Waterhouse Square61 HolbornLondonEC1N 2QWUK
1​
V37 I1Arbitration Europeansample@sample.com
9642​
02/04/2022 07:41​
Addressee gone away48879/0001577////000000010890
10890​
Mr John Alun Roberts Building & Danger Consultants Ltd221 Banstead RoadEwellKT17 9HNUK
1​
V37 I03Arbitration Italiansample@sample.com



Sub Mail_Selection()
'Working in Excel 2000-2016
'For Tips see: Excel Automation - Ron de Bruin

Dim To_Recipients As String
Dim Source As Range
Dim Dest As Workbook
Dim wb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim OutApp As Object
Dim OutMail As Object

Set Source = Nothing
On Error Resume Next
Set Source = Selection.SpecialCells(xlCellTypeVisible)
On Error GoTo 0

If Source Is Nothing Then
MsgBox "The source is not a range or the sheet is protected, please correct and try again.", vbOKOnly
Exit Sub
End If

If ActiveWindow.SelectedSheets.Count > 1 Or _
Selection.Cells.Count = 1 Or _
Selection.Areas.Count > 1 Then
MsgBox "An Error occurred :" & vbNewLine & vbNewLine & _
"You have more than one sheet selected." & vbNewLine & _
"You only selected one cell." & vbNewLine & _
"You selected more than one area." & vbNewLine & vbNewLine & _
"Please correct and try again.", vbOKOnly
Exit Sub
End If

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

Set wb = ActiveWorkbook
Set Dest = Workbooks.Add(xlWBATWorksheet)

Source.Copy
With Dest.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial Paste:=xlPasteValues
.Cells(1).PasteSpecial Paste:=xlPasteFormats
.Cells(1).Select
Application.CutCopyMode = False
End With

TempFilePath = Environ$("temp") & "\"
TempFileName = "Selection of " & wb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")

If Val(Application.Version) < 12 Then
'You use Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'You use Excel 2007-2016
FileExtStr = ".xlsx": FileFormatNum = 51
End If

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

With Dest
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
On Error Resume Next
With OutMail
.to = ???????????????????????????????
'To_Recipients = "email01"
.CC = ""
.BCC = ""
.Subject = "Returns/Invalid Address"
.Body = "Hello," & vbNewLine & "Please see the attached missing customers list." & vbNewLine & "Please review the attachment and confirm correct customer address information."
.Attachments.Add Dest.FullName
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Send 'or use .Display
End With
On Error GoTo 0
.Close savechanges:=False
End With

Kill TempFilePath & TempFileName & FileExtStr

Set OutMail = Nothing
Set OutApp = Nothing

With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
 

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.
Maybe a further explanation would help.
What I would like to happen is like the below sudo code.

Dim email As String
email = Columns(21).Cells.SpecialCells(xlCellTypeVisible).Cells(2).Select


With OutMail
.To_Recipients = email
.CC = ""
.BCC = ""
.Subject = "Returns/Invalid Address"
.Body = "Hello," & vbNewLine & "Please see the attached missing customers list." & vbNewLine & "Please review the attachment and confirm correct customer address information."
.Attachments.Add Dest.FullName
.Send

So the email is pulling from a cell. I'm not too used to VBA, do i need to cast it as a type?
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,271
Members
452,628
Latest member
dd2

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