Try this...
youd have to tweek the code to add your folders and subject, body.
Put this in a module in the workbook1 with the emails list.
Add a button to the screen to run the macro: SendXlEmails
Option Explicit
'by Ranman256
Private mEwb As Workbook
Public Sub SendXlEmails()
Set mEwb = ActiveWorkbook
EmailAllFilesInDir "\\myfolder\myFolder2\"
Set mEwb = Nothing
End Sub
Private Sub EmailAllFilesInDir(ByVal pvDir)
Dim vFil
Dim i As Integer
Dim fso
Dim oFolder, oFile
On Error GoTo errImp
If Right(pvDir, 1) <> "\" Then pvDir = pvDir & "\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFolder = fso.GetFolder(pvDir)
For Each oFile In oFolder.Files
vFil = pvDir & oFile.Name
'ONLY DO EXCEL FILES
If InStr(vFil, ".xls") > 0 Then
CollectEmails pvDir, oFile.Name
End If
Next
Set fso = Nothing
Set oFile = Nothing
Set oFolder = Nothing
Exit Sub
errImp:
MsgBox Err.Description, vbCritical, "EmailAllFilesInDir()" & Err
Exit Sub
Resume
End Sub
Private Sub CollectEmails(ByVal pvDir, ByVal pvFile)
Dim vTo, vSubj, vBody
Dim vCode, vDir, vFullFile
Dim i As Long
'================
'send 1 email having everyones address code
'================
vFullFile = pvDir & pvFile
i = InStr(pvFile, ".x")
vCode = Left(pvFile, i - 1)
mEwb.Activate
Range("A1").Select 'assumes the codes are in Col A and...
While ActiveCell.Value <> ""
If ActiveCell.Value = vCode Then vTo = vTo & ActiveCell.Offset(0, 1).Value & ";" '... emails Col B
ActiveCell.Offset(1, 0).Select 'NEXT ROW
Wend
vSubj = "My subject"
'-------
'YOU MUST ADD THE OUTLOOK OBJECT LIB in vbe, TOOLS, REFERENCES!!! checkmark OUTLOOK OBJECTS in the vbE menu, Tools, References
'-------
MsgBox "add OUTLOOK to references", , "Then delete this msgbox"
Send1Email vTo, "My subject", vBody, vFullFile 'SEND EMAIL
End Sub
Public Function Send1Email(ByVal pvTo, ByVal pvSubj, ByVal pvBody, Optional pvFile) As Boolean
Dim oApp As Outlook.Application
Dim oMail As Outlook.MailItem
On Error GoTo ErrMail
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(olMailItem)
With oMail
.To = pvTo
.Subject = pvSubj
.Body = pvBody
If Not IsMissing(pvFile) Then .Attachments.Add pvFile, olByValue, 1
.Send
End With
Send1Email = True
Set oMail = Nothing
Set oApp = Nothing
Exit Function
ErrMail:
MsgBox Err.Description, vbCritical, Err
Resume Next
End Function
Bookmarks