Mail the whole workbook
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
The following subroutine sends the last saved version of the active workbook in an e-mail message.
Change the mail address and subject in the macro before you run it.
Example 2
This sub will send a newly created workbook (copy of the ActiveWorkbook).
It is saving the workbook before mailing it with a date/time stamp.
After the file is sent the workbook will be deleted from your hard disk.
Change the mail address and subject in the macro before you run it.
Note: In this example we open the new file that we create with SaveCopyAs and mail/close/delete it.
It is also possible to do it without opening the new file but I personally like to open it so it is easy
to add a few code lines after this line that opens the new file that we create with SaveCopyAs :
Set wb2 = Workbooks.Open(TempFilePath & TempFileName & FileExtStr)
We can add this for example to insert a text and Date in cell A1 of the first sheet in the workbook.
Other things you can think of are, delete a whole sheet or a range in the workbook you send.
wb2.Worksheets(1).Range("A1").Value = "Copy created on " & Format(Date, "dd-mmm-yyyy")
wb2.Save
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
The following subroutine sends the last saved version of the active workbook in an e-mail message.
Change the mail address and subject in the macro before you run it.
Sub Mail_workbook_Outlook_1() 'Working in 2000-2010 'This example send the last saved version of the Activeworkbook Dim OutApp As Object Dim OutMail As Object Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.CreateItem(0) On Error Resume Next With OutMail .To = "ron@debruin.nl" .CC = "" .BCC = "" .Subject = "This is the Subject line" .Body = "Hi there" .Attachments.Add ActiveWorkbook.FullName 'You can add other files also like this '.Attachments.Add ("C:\test.txt") .Send 'or use .Display End With On Error GoTo 0 Set OutMail = Nothing Set OutApp = Nothing End Sub
Example 2
This sub will send a newly created workbook (copy of the ActiveWorkbook).
It is saving the workbook before mailing it with a date/time stamp.
After the file is sent the workbook will be deleted from your hard disk.
Change the mail address and subject in the macro before you run it.
Sub Mail_workbook_Outlook_2() 'Working in 2000-2010 Dim wb1 As Workbook Dim wb2 As Workbook Dim TempFilePath As String Dim TempFileName As String Dim FileExtStr As String Dim OutApp As Object Dim OutMail As Object Set wb1 = ActiveWorkbook If Val(Application.Version) >= 12 Then If wb1.FileFormat = 51 And wb1.HasVBProject = True Then MsgBox "There is VBA code in this xlsx file, there will" & vbNewLine & _ "be no VBA code in the file you send. Save the" & vbNewLine & _ "file first as xlsm and then try the macro again.", vbInformation Exit Sub End If End If With Application .ScreenUpdating = False .EnableEvents = False End With 'Make a copy of the file/Open it/Mail it/Delete it 'If you want to change the file name then change only TempFileName TempFilePath = Environ$("temp") & "\" TempFileName = "Copy of " & wb1.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss") FileExtStr = "." & LCase(Right(wb1.Name, _ Len(wb1.Name) - InStrRev(wb1.Name, ".", , 1))) wb1.SaveCopyAs TempFilePath & TempFileName & FileExtStr Set wb2 = Workbooks.Open(TempFilePath & TempFileName & FileExtStr) Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.CreateItem(0) On Error Resume Next With OutMail .To = "ron@debruin.nl" .CC = "" .BCC = "" .Subject = "This is the Subject line" .Body = "Hi there" .Attachments.Add wb2.FullName 'You can add other files also like this '.Attachments.Add ("C:\test.txt") .Send 'or use .Display End With On Error GoTo 0 wb2.Close SaveChanges:=False 'Delete the file Kill TempFilePath & TempFileName & FileExtStr Set OutMail = Nothing Set OutApp = Nothing With Application .ScreenUpdating = True .EnableEvents = True End With End Sub
Note: In this example we open the new file that we create with SaveCopyAs and mail/close/delete it.
It is also possible to do it without opening the new file but I personally like to open it so it is easy
to add a few code lines after this line that opens the new file that we create with SaveCopyAs :
Set wb2 = Workbooks.Open(TempFilePath & TempFileName & FileExtStr)
We can add this for example to insert a text and Date in cell A1 of the first sheet in the workbook.
Other things you can think of are, delete a whole sheet or a range in the workbook you send.
wb2.Worksheets(1).Range("A1").Value = "Copy created on " & Format(Date, "dd-mmm-yyyy")
wb2.Save
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)