Link to home
Start Free TrialLog in
Avatar of rvfowler2
rvfowler2Flag for United States of America

asked on

Auto Send Excel as Email Attachment

We have created a macro that imports a logon/logoff csv file into Excel and formats it.  We now want to send it as an attachment automatically.  The second macro does just that; however, it seems a bit long.  Directly below is the shortest version, but it shows the email box instead of sending it.  Is there another step that can just send it without user intervention?  Thank you.

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 5/26/2010 by
'
    ActiveWorkbook.Save
    Application.Dialogs(xlDialogSendMail).Show "randy@upcli.com", "Subject Matter"
 
End Sub

----------------------

Sub EmailExcel()


    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 = "randy@upcli.com"
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .Body = "Hi there"
        .Attachments.Add ActiveWorkbook.FullName
        'You can add other files also 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
ASKER CERTIFIED SOLUTION
Avatar of Patrick Matthews
Patrick Matthews
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 rvfowler2

ASKER

Yes, per your suggestion, the below worked perfectly... and you answered my next question (handling the Outlook warning) before I even asked?  Excellent!  Thanks, Patrick.

ThisWorkbook.SendMail "randy@upcli.com", "Logon/Logoff Data"
rvfowler2,

Glad to help :)

Patrick