How To Send An Email From Word Using VBA - Word MVP Site

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 = TrueWith Activedocument.RoutingSlip    .Subject = "New subject goes here"    .AddRecipient "[email protected]"    .AddRecipient "[email protected]"    .Delivery = wdAllAtOnceEnd WithActivedocument.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 BooleanDim oOutlookApp As Outlook.ApplicationDim oItem As Outlook.MailItemOn Error Resume Next'Get Outlook if it's runningSet oOutlookApp = GetObject(, "Outlook.Application")If Err <> 0 Then    'Outlook wasn't running, start it from code    Set oOutlookApp = CreateObject("Outlook.Application")    bStarted = TrueEnd If'Create a new mailitemSet oItem = oOutlookApp.CreateItem(olMailItem)With oItem    'Set the recipient for the new email   .To = "[email protected]"    'Set the recipient for a copy    .CC = "[email protected]"    'Set the subject    .Subject = "New subject"    'The content of the document is used as the body for the email    .Body = ActiveDocument.Content    .SendEnd WithIf bStarted Then    'If we started Outlook from code, then close it    oOutlookApp.QuitEnd If'Clean upSet oItem = NothingSet oOutlookApp = NothingEnd 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 BooleanDim oOutlookApp As Outlook.ApplicationDim oItem As Outlook.MailItemOn Error Resume NextIf Len(ActiveDocument.Path) = 0 Then    MsgBox "Document needs to be saved first"    Exit SubEnd IfSet oOutlookApp = GetObject(, "Outlook.Application")If Err <> 0 Then    Set oOutlookApp = CreateObject("Outlook.Application")    bStarted = TrueEnd IfSet oItem = oOutlookApp.CreateItem(olMailItem)With oItem    .To = "[email protected]"    .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"    .SendEnd WithIf bStarted Then    oOutlookApp.QuitEnd IfSet oItem = NothingSet oOutlookApp = NothingEnd Sub

Từ khóa » Visual Basic Send Email Outlook Attachment