Private oApp As Outlook.Application
Private oMail As Outlook.MailItem
'----------
Public Sub SendXlFiles()
'----------
Dim vTo, vSubj, vBody, vFile, vName
Dim i As Integer
Const vDIR = "C:\TEMP\"
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.createitem(olmailitem)
Range("A2").Select
While ActiveCell.Value <> ""
vName = ActiveCell.Offset(0, 0).Value
vTo = ActiveCell.Offset(0, 1).Value
vSubj = "Todays file " & Date
vBody = "Here's your file"
vFile = vDIR & vName & ".xlsx"
call Email1 (vTo, vSubj, vBody, vFile)
ActiveCell.Offset(1, 0).Select 'next row
Next
Set oMail = Nothing
Set oApp = Nothing
End Sub
'----------
Public Function Email1(ByVal pvTo, ByVal pvSubj, ByVal pvBody, Optional ByVal pvFile) As Boolean
'----------
On Error GoTo ErrMail
'****
'**** NOTE : YOU MUST HAVE THE OUTLOOK REFERENCE CHECKED IN VBE; ctl-G, menu,tools, references
'****
With oMail
.To = pvTo
.Subject = pvSubj
If Not IsNull(pvBody) Then .Body = pvBody
If Not IsMissing(pvFile) Then .Attachments.Add pvFile, olByValue, 1
.Display True
'.Send
End With
Email1 = True
endit:
Exit Function
ErrMail:
MsgBox Err.Description, vbCritical, Err
Resume endit
End Function