How to send an email from Word using VBA
Article contributed by Astrid Zeelenberg
1. Using the Routing Slip method
The easiest way to send a document by email is to use the Word's built-in RoutingSlip method. With this method you can send the document to one or more recipients and you can set the subject for the message, and choose whether it should be send to all the recipients at once, or be routed from one to the next.
The advantages of using the RoutingSlip are that:
- The code will work with all email programs, so you don't need to know whether the system that's running this code has any specific email program installed
- The document can be sent even if it hasn't been saved
- Your code will run faster than if you automate Outlook (unless Outlook already happens to be open when your code runs).
Disadvantages:
- The email message has default body text, which you can't change from within code.
- You can't use the text inside the document as the body of the email; you can only send the document as an attachment.
- You can't set any recipients for the bcc field.
The code for using the RoutingSlip method is:
Activedocument.HasRoutingSlip =
True
With Activedocument.RoutingSlip
.Subject = "New subject goes here"
.AddRecipient "Firstaddress@Mail.com"
.AddRecipient "Secondaddress@Mail.com"
.Delivery = wdAllAtOnce
End With
Activedocument.Route
2. Automating Outlook
The other option is to automate Outlook to send your document. The main disadvantage, of course is that you need to be absolutely sure that the system that's running your code has Outlook installed.
Another disadvantage is that if you want to send the document as an attachment the document needs to have been saved at least once before you can send it. This is because you need a path and filename for the file in the code.
If Outlook is not already open when your code runs, this method will also be slower.
Finally, the code is also a bit more complex then using .Route method. You'll need to set a reference (Tools-References in the Visual Basic Editor) to the Outlook type Library to get this code to work:
Sub SendDocumentInMail()
Dim bStarted As Boolean
Dim oOutlookApp As
Outlook.Application
Dim oItem As
Outlook.MailItem
On Error Resume Next
'Get Outlook if it's running
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
'Outlook wasn't running, start it
from code
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If
'Create a new mailitem
Set oItem =
oOutlookApp.CreateItem(olMailItem)
With oItem
'Set the recipient for the new email
.To = "recipient@mail.com"
'Set the recipient for a copy
.CC = "recipient2@mail.com"
'Set the subject
.Subject = "New subject"
'The content of the document is used
as the body for the email
.Body = ActiveDocument.Content
.Send
End With
If bStarted Then
'If we started Outlook from code,
then close it
oOutlookApp.Quit
End If
'Clean up
Set oItem = Nothing
Set oOutlookApp = Nothing
End Sub
That sends the text in the document as the content of the email (not as an attachment); and it sends it as plain text, so all formatting is lost.
You can send the document as attachment using Outlook provided the document has been saved at least once:
Sub SendDocumentAsAttachment()
Dim bStarted As Boolean
Dim oOutlookApp As
Outlook.Application
Dim oItem As
Outlook.MailItem
On Error Resume Next
If Len(ActiveDocument.Path) = 0
Then
MsgBox "Document needs to be saved first"
Exit Sub
End If
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
Set oOutlookApp =
CreateObject("Outlook.Application")
bStarted = True
End If
Set oItem = oOutlookApp.CreateItem(olMailItem)
With oItem
.To = "recipient@mail.com"
.Subject = "New subject"
'Add the document as an attachment,
you can use the .displayname property
'to set the description that's used in the message
.Attachments.Add Source:=ActiveDocument.FullName,
Type:=olByValue, _
DisplayName:="Document as attachment"
.Send
End With
If bStarted Then
oOutlookApp.Quit
End If
Set oItem = Nothing
Set oOutlookApp = Nothing
End Sub