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
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
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
ID | DateOfReturn | Reason | RETURNBARCODE | CTM_NBR | ADDRESSEE_NAME | CO | DEPT | A1 | A2 | A3 | A4 | A5 | AZIP | ASTATE | ACOUNTRY | COPIES | FOI | |||||
8947 | 20/12/2021 08:43:22 | Addressee gone away | 46078/0000848////000000000000 | 0 | Mr Pete Stewart | Gowling WLG (UK) LLP | 100 Waterhouse Square | 61 Holborn | London | EC1N 2QW | UK | 1 | V37 I1 | Arbitration European | sample@sample.com | |||||||
9642 | 02/04/2022 07:41 | Addressee gone away | 48879/0001577////000000010890 | 10890 | Mr John Alun | Roberts Building & Danger Consultants Ltd | 221 Banstead Road | Ewell | KT17 9HN | UK | 1 | V37 I03 | Arbitration Italian | sample@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