Mail a different file(s) to each person in a range
Important read this :
The code on this page is only working with Outlook and not with Outlook Express or Windows Mail.
If you not use Outlook see the examples in the first section on my mail index page.
Copy the code in a Standard module, if you just started with VBA see this page.
http://www.rondebruin.nl/code.htm
Check out this page for Tips If you want to change the code on this page.
http://www.rondebruin.nl/mail/tips2.htm
Example 1
Make a list in Sheets("Sheet1") with :
In column A : Names of the people
In column B : E-mail addresses
In column C:Z : Filenames like this C:\Data\Book2.xls (don't have to be Excel files)
The Macro will loop through each row in "Sheet1" and if there is a E-mail address in column B
and file name(s) in column C:Z it will create a mail with this information and send it.
Early Binding
If you want to use the the Intellisense help showing you the properties and methods of the objects as you
type you can use Early binding. (bit faster but have problems when you distribute your workbooks)
See Dick's site for a explanation
http://www.dicks-clicks.com/excel/olBinding.htm
Add a reference to the Microsoft outlook Library
1) Go to the VBA editor, Alt -F11
2) Tools>References in the Menu bar
3) Place a Checkmark before Microsoft Outlook ? Object Library
? is the Excel version number
Then replace this three lines in the code
Dim OutApp As Object
Dim OutMail As Object
Set OutMail = OutApp.CreateItem(0)
With this three
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Set OutMail = OutApp.CreateItem(olMailItem)
The code on this page is only working with Outlook and not with Outlook Express or Windows Mail.
If you not use Outlook see the examples in the first section on my mail index page.
Copy the code in a Standard module, if you just started with VBA see this page.
http://www.rondebruin.nl/code.htm
Check out this page for Tips If you want to change the code on this page.
http://www.rondebruin.nl/mail/tips2.htm
Example 1
Make a list in Sheets("Sheet1") with :
In column A : Names of the people
In column B : E-mail addresses
In column C:Z : Filenames like this C:\Data\Book2.xls (don't have to be Excel files)
The Macro will loop through each row in "Sheet1" and if there is a E-mail address in column B
and file name(s) in column C:Z it will create a mail with this information and send it.
Sub Send_Files() 'Working in 2000-2010 Dim OutApp As Object Dim OutMail As Object Dim sh As Worksheet Dim cell As Range, FileCell As Range, rng As Range With Application .EnableEvents = False .ScreenUpdating = False End With Set sh = Sheets("Sheet1") Set OutApp = CreateObject("Outlook.Application") For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants) 'Enter the file names in the C:Z column in each row Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1") If cell.Value Like "?*@?*.?*" And _ Application.WorksheetFunction.CountA(rng) > 0 Then Set OutMail = OutApp.CreateItem(0) With OutMail .To = cell.Value .Subject = "Testfile" .Body = "Hi " & cell.Offset(0, -1).Value For Each FileCell In rng.SpecialCells(xlCellTypeConstants) If Trim(FileCell) <> "" Then If Dir(FileCell.Value) <> "" Then .Attachments.Add FileCell.Value End If End If Next FileCell .Send 'Or use Display End With Set OutMail = Nothing End If Next cell Set OutApp = Nothing With Application .EnableEvents = True .ScreenUpdating = True End With End Sub
Early Binding
If you want to use the the Intellisense help showing you the properties and methods of the objects as you
type you can use Early binding. (bit faster but have problems when you distribute your workbooks)
See Dick's site for a explanation
http://www.dicks-clicks.com/excel/olBinding.htm
Add a reference to the Microsoft outlook Library
1) Go to the VBA editor, Alt -F11
2) Tools>References in the Menu bar
3) Place a Checkmark before Microsoft Outlook ? Object Library
? is the Excel version number
Then replace this three lines in the code
Dim OutApp As Object
Dim OutMail As Object
Set OutMail = OutApp.CreateItem(0)
With this three
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Set OutMail = OutApp.CreateItem(olMailItem)