Tips for the Outlook object model examples
If you want to change the SendMail examples from my site see
this tips page.
http://www.rondebruin.nl/mail/tips1.htm
Change the To, CC and BCC line
You can use this to send to more then one person.
.To = "ron@debruin.nl;jelle@debruin.nl"
Or you can use a mail address in a cell like this, you can do the same
for the CC,BCC or the subject.
.To = ThisWorkbook.Sheets("Sheet1").Range("C1").Value
Or Send to Outlook Distribution Group
Instead of .To "ron@debruin.nl" use
.To = "GroupName"
Or use this for a group name or contact
.Recipients.Add "GroupName"
Send to all E-mail addresses in a range and check if the mail address is correct.
Add the code below to the macro and change the To line to this: .To = strto
If you only want to use mail addresses with the word "yes" in the column next to it you can replace
If cell.Value Like "?*@?*.?*" Then
With
If cell.Value Like "?*@?*.?*" And LCase(cell.Offset(0, 1).Value) = "yes" Then
Note: I use ThisWorkbook in the examples above to point to the worksheets in the workbook with the code.
Change the Save line
You can change the TempFileName string in the code to change the file name.
It is also possible to use a cell reference.
TempFileName = "YourFileName"
TempFileName = "YourFileName " & Format(Now, "dd-mmm-yy h-mm-ss")
TempFileName = ThisWorkbook.Sheets("Sheet1").Range("C1").Value
Important: Use error checking to verify that a file with that name doesn't already exist or isn't already open.
In the examples in this article the file name includes the date and time so that the chance the file name
already exists is very small.
Copy the cells as values
If you want to paste as values the sheet must be unprotected!!!!!
Or Unprotect and Protect the sheet in the Sub also.
In most examples there is commented code that you can use to do this.
See this page for example code if you need more information
http://www.rondebruin.nl/values.htm
Change the Body line
If you want to add more text to the body then
instead of .Body = "Hi there" use this.
Or try this if you want to use cell values
Or this
Change the Body line to .Body = strbody to use the string.
Use HTMLBody instead of Body
Formatted text (HTML)
If you want to create emails that are formatted you can use HTMLBody (Office 2000 and up) instead of Body.
You can find a lot of WebPages on the internet with HTML tags examples.
If you want to send a range or worksheet as HTML see this two examples.
Range/Selection
http://www.rondebruin.nl/mail/folder3/mail4.htm
Whole worksheet
http://www.rondebruin.nl/mail/folder3/mail2.htm
Other options
There are a lot of other options that you can add to the code. You can find a few of them below.
To find out all the options change the code example that you use to Early binding.
On the bottom of every Outlook example page you can see how.
The Intellisense help show you the properties and methods then that you can use when you
type the code. You can also use the VBA help after you change the code to Early binding.
Change sender name and reply address
If you want to change the sender name and reply address add this code line
'The receiver can see the original mail address in the properties if he want
.SentOnBehalfOfName = """SenderName"" <Reply@Address.com>"
Change Account (only Excel/Outlook 2007)
Use the Account you want in mail macro in Excel/Outlook 2007
http://www.rondebruin.nl/mail/account.htm
Set Importance
' 0 = Low, 2 = High, 1 = Normal
.Importance = 2
ReadReceiptRequest
.ReadReceiptRequested = True
DeferredDeliveryTime
'Stay in the outbox untill this date and time
.DeferredDeliveryTime = "1/6/2007 10:40:00 AM"
'Wait four hours
.DeferredDeliveryTime = DateAdd("h", 4, Now)
Add Signature to mail
See this page
http://www.rondebruin.nl/mail/folder3/signature.htm
http://www.rondebruin.nl/mail/tips1.htm
Change the To, CC and BCC line
You can use this to send to more then one person.
.To = "ron@debruin.nl;jelle@debruin.nl"
Or you can use a mail address in a cell like this, you can do the same
for the CC,BCC or the subject.
.To = ThisWorkbook.Sheets("Sheet1").Range("C1").Value
Or Send to Outlook Distribution Group
Instead of .To "ron@debruin.nl" use
.To = "GroupName"
Or use this for a group name or contact
.Recipients.Add "GroupName"
Send to all E-mail addresses in a range and check if the mail address is correct.
Add the code below to the macro and change the To line to this: .To = strto
Dim cell As Range
Dim strto As String
For Each cell In ThisWorkbook.Sheets("Sheet1").Range("A1:A10")
If cell.Value Like "?*@?*.?*" Then
strto = strto & cell.Value & ";"
End If
Next cell
If Len(strto) > 0 Then strto = Left(strto, Len(strto) - 1)
If you only want to use mail addresses with the word "yes" in the column next to it you can replace
If cell.Value Like "?*@?*.?*" Then
With
If cell.Value Like "?*@?*.?*" And LCase(cell.Offset(0, 1).Value) = "yes" Then
Note: I use ThisWorkbook in the examples above to point to the worksheets in the workbook with the code.
Change the Save line
You can change the TempFileName string in the code to change the file name.
It is also possible to use a cell reference.
TempFileName = "YourFileName"
TempFileName = "YourFileName " & Format(Now, "dd-mmm-yy h-mm-ss")
TempFileName = ThisWorkbook.Sheets("Sheet1").Range("C1").Value
Important: Use error checking to verify that a file with that name doesn't already exist or isn't already open.
In the examples in this article the file name includes the date and time so that the chance the file name
already exists is very small.
Copy the cells as values
If you want to paste as values the sheet must be unprotected!!!!!
Or Unprotect and Protect the sheet in the Sub also.
In most examples there is commented code that you can use to do this.
See this page for example code if you need more information
http://www.rondebruin.nl/values.htm
Change the Body line
If you want to add more text to the body then
instead of .Body = "Hi there" use this.
Dim strbody As String
strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"
Or try this if you want to use cell values
Dim cell As Range
Dim strbody As String
For Each cell In ThisWorkbook.Sheets("Sheet1").Range("C1:C60")
strbody = strbody & cell.Value & vbNewLine
Next
Or this
Dim strbody As String
With ThisWorkbook.Sheets("Sheet1")
strbody = "Hi there" & vbNewLine & vbNewLine & _
.Range("A1") & vbNewLine & _
.Range("A2") & vbNewLine & _
.Range("A3") & vbNewLine & _
.Range("A4")
End With
Change the Body line to .Body = strbody to use the string.
Use HTMLBody instead of Body
Formatted text (HTML)
If you want to create emails that are formatted you can use HTMLBody (Office 2000 and up) instead of Body.
You can find a lot of WebPages on the internet with HTML tags examples.
strbody = "<H3><B>Dear Customer</B></H3>" & _
"Please visit this website to download the new version.<br>" & _
"Let me know if you have problems.<br>" & _
"<A HREF=""http://www.rondebruin.nl/"">Ron's Excel Page</A>" & _
"<br><br><B>Thank you</B>"
Use .HTMLBody = strbody then instead of
.Body = strbodyIf you want to send a range or worksheet as HTML see this two examples.
Range/Selection
http://www.rondebruin.nl/mail/folder3/mail4.htm
Whole worksheet
http://www.rondebruin.nl/mail/folder3/mail2.htm
Other options
There are a lot of other options that you can add to the code. You can find a few of them below.
To find out all the options change the code example that you use to Early binding.
On the bottom of every Outlook example page you can see how.
The Intellisense help show you the properties and methods then that you can use when you
type the code. You can also use the VBA help after you change the code to Early binding.
Change sender name and reply address
If you want to change the sender name and reply address add this code line
'The receiver can see the original mail address in the properties if he want
.SentOnBehalfOfName = """SenderName"" <Reply@Address.com>"
Change Account (only Excel/Outlook 2007)
Use the Account you want in mail macro in Excel/Outlook 2007
http://www.rondebruin.nl/mail/account.htm
Set Importance
' 0 = Low, 2 = High, 1 = Normal
.Importance = 2
ReadReceiptRequest
.ReadReceiptRequested = True
DeferredDeliveryTime
'Stay in the outbox untill this date and time
.DeferredDeliveryTime = "1/6/2007 10:40:00 AM"
'Wait four hours
.DeferredDeliveryTime = DateAdd("h", 4, Now)
Add Signature to mail
See this page
http://www.rondebruin.nl/mail/folder3/signature.htm