Link to home
Start Free TrialLog in
Avatar of jwagman
jwagmanFlag for United States of America

asked on

Using VBA - send email from Word

I'd like to construct some VBA which will automate the sending of a word document via email from within word. I can do it by hand simply by creating the document (btw, I'm saving the doc as filtered HTML, and it has tables in it) and doing File>Send To>Mail Recipient. I try to "record" this as a macro, but it won't let me record the mailing portion of it. Can I do it programatically? I did try the sendmail method, but it didn't preserve any ot the formatting, tables, etc. TIA.
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

Hello jwagman,

The attached code should get you a decent start.

Regards,

Patrick
Dim olApp As Object
Dim olMsg As Object 
Set olApp = CreateObject("Outlook.Application")
Set olMsg = olApp.CreateItem(0)
With olMsg
    .To = "X@z.com"
    .CC = "y@z.com"
    .BCC = "z@z.com"
    .Subject = "subject"
    .Body = "body"
    .Attachments.Add "c:\folder\subfolder\foo.doc"
    .Send
End With 
Set olMsg = Nothing
Set olApp = Nothing

Open in new window

Avatar of jwagman

ASKER

Hi,

I need to send the doc in the body of the message, not as attachment. I've tried a few different ways but usually the tables, etc. from the doc don't come through in the email.

Any ideas?
ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of badtenant
badtenant

This works for me...

    Dim OutApp As Object
    Dim OutMail As Object
 
    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    Set OutMail = OutApp.CreateItem(0)
 
    On Error Resume Next
    With OutMail
        .To = "email@email.com"
        .CC = ""
        .BCC = ""
        .Subject = "Weekly Ticket Update"
        .HtmlBody = "<html><b>Hi there</b></html>"
        '.Attachments.Add ActiveWorkbook.FullName
        'You can add other files also like this
        '.Attachments.Add ("c:\")
        .Send   'or use .Display
    End With
    On Error GoTo 0
 
    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub
What is the vb macro in word if you want to send the active/open Word doc via email with an e-mail macro ?