This is what I have got so far but I am not sure how to adapt the concated field:
Option Explicit
Sub EMAILnSAVE()
Dim Sourcewb As Object
Dim Destwb As Object
Dim cell As Long
Dim NR As Long
Dim lCol As Long
Dim wsData As Worksheet
Dim SaveStr As String
Dim tagerror As String
Dim Email_Send_To, Email_Send_From, Email_Subject, Email_Body As String
Dim strUserEmail As String
Dim strFirstClassPassword As String
Dim errPar As String
Dim iMsg As Object
Dim iConfig As Object
Dim sConfig As Variant
Dim Deskstr As String
Dim rRng As Range
Dim LastRw As Long
strUserEmail = "me@myemail.ac.uk"
strFirstClassPassword = "password"
Set iMsg = CreateObject("CDO.Message")
Set iConfig = CreateObject("CDO.Configuration")
iConfig.Load -1
Set sConfig = iConfig.Fields
With sConfig
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "192.168.0.5" 'Name or IP of remote SMTP server
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 'Server Port
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = strUserEmail
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = strFirstClassPassword
.Update
End With
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set Sourcewb = ThisWorkbook
With Sourcewb
Set wsData = .Sheets("OUTPUT")
End With
Set Destwb = Application.Workbooks.Add
ActiveSheet.Name = "EXPENSES_CSV"
With wsData
NR = .Cells(.Rows.Count, 1).End(xlUp).Row
.Range(.Cells(2, 1), .Cells(.Rows.Count, 8).End(xlUp)).Copy Destination:=Destwb.Worksheets("OVERTIME_CSV").Range("a2")
End With
With ActiveSheet
.Name = "EXPENSES_CSV"
LastRw = .UsedRange.Rows.Count
Set rRng = .Range(.Cells(2, 9), .Cells(LastRw + 1, 9))
rRng.FormulaR1C1 = "=TEXT(RC[-2],""dd/mm/yy"") &""-"" &TEXT(RC[-1],""dd/mm/yy"")"
.Columns(9).Value = Columns(9).Value
.Columns("G:H").Delete Shift:=xlToLeft
End With
With wsData
.Range(.Cells(2, 8), .Cells(.Rows.Count, 8).End(xlUp)).Copy Destination:=Destwb.Worksheets("OVERTIME_CSV").Range("H2")
End With
With Destwb.Worksheets("EXPENSES_CSV")
For lCol = 1 To 8
.Cells(1, lCol) = Choose(lCol, "EMP_ID", "DESCRIPTION", "ID_UNITS", "ID_RATE", "ID_VALUE", _
"PAYROLL_ID", "GEN_CODE", "ID_DATE")
Next lCol
End With
Deskstr = CreateObject("WScript.Shell").SpecialFolders("Desktop") _
& Application.PathSeparator & "EXPENSES BACKUP"
If Dir(Deskstr, vbDirectory) = "" Then MkDir Deskstr
SaveStr = Deskstr & Application.PathSeparator & ActiveSheet.Name _
& " - " _
& Environ("USERNAME") _
& " - " _
& Format(Now, " d-m-yy h.mm AM/PM")
'-----------------------------------------------------------------------------
Email_Send_To = "me@myemail.ac.uk"
Email_Send_From = "me@myemail.ac.uk"
Email_Subject = "EXPENSES - OLASS " & Format(Now, "mm/yyyy")
Email_Body = "SENDERS DETAILS - " & Environ("USERNAME")
'------------------------------------------------------------------------------
With Destwb
.SaveAs Filename:=SaveStr & ".csv", FileFormat:=xlCSVWindows, CreateBackup:=False, local:=True
.Close SaveChanges:=False
On Error Resume Next
End With
With iMsg
Set .Configuration = iConfig
End With
iMsg.To = Email_Send_To
iMsg.From = Email_Send_From
iMsg.Subject = Email_Subject
iMsg.Textbody = Email_Body
iMsg.AddAttachment SaveStr
iMsg.Send
On Error GoTo tagerror
If ActiveSheet.Range("a1") = "" Then
Application.DisplayAlerts = False
ActiveSheet.Delete
Application.DisplayAlerts = True
Sourcewb.Activate
Sheets("INPUT").Select
Else
Exit Sub
End If
clean_up:
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Exit Sub
tagerror:
MsgBox "Error: (" & Err.Number & ") " & Err.Description & " at " & Err.Source, vbCritical
Resume clean_up
End Sub
Bookmarks