Mail a small message
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 : send a small text message
Example 2 : send the text from a txt file in the body of the mail
Example 1
The following subroutine sends a small text in an e-mail message.
Change the mail address and subject in the macro before you run it.
Example 2
The following subroutine sends the text from a txt file in the body of the mail.
Change the mail address, subject and path/name of the txt file in the macro before you run it.
I use C:\test.txt in this example. Note: this example also use the GetBoiler function, you can
find it below the macro. Copy this function also in your standard module.
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 : send a small text message
Example 2 : send the text from a txt file in the body of the mail
Example 1
The following subroutine sends a small text in an e-mail message.
Change the mail address and subject in the macro before you run it.
Sub Mail_small_Text_Outlook() 'Working in Office 2000-2010 Dim OutApp As Object Dim OutMail As Object Dim strbody As String Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.CreateItem(0) strbody = "Hi there" & vbNewLine & vbNewLine & _ "This is line 1" & vbNewLine & _ "This is line 2" & vbNewLine & _ "This is line 3" & vbNewLine & _ "This is line 4" On Error Resume Next With OutMail .To = "ron@debruin.nl" .CC = "" .BCC = "" .Subject = "This is the Subject line" .Body = strbody 'You can add a file 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
The following subroutine sends the text from a txt file in the body of the mail.
Change the mail address, subject and path/name of the txt file in the macro before you run it.
I use C:\test.txt in this example. Note: this example also use the GetBoiler function, you can
find it below the macro. Copy this function also in your standard module.
Sub Mail_Text_From_Txtfile_Outlook() 'Working in Office 2000-2010 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 = GetBoiler("C:\test.txt") 'You can add a file 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 Function GetBoiler(ByVal sFile As String) As String 'Dick Kusleika Dim fso As Object Dim ts As Object Set fso = CreateObject("Scripting.FileSystemObject") Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2) GetBoiler = ts.readall ts.Close End Function
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)