How to add an attachment to an email using VBA in Excel

后端 未结 2 1525
半阙折子戏
半阙折子戏 2021-01-21 18:26

I have the following code but it is not working. I am fairly new to VBA as well. The code works to populate the email template but as soon as I add the .Attachment.Add it does n

2条回答
  •  耶瑟儿~
    2021-01-21 18:58

    This simple script should illustrate the point of how to add attachments to an email, and then send the email.

    Sub Mail_workbook_Outlook_1()
    'Working in Excel 2000-2016
    'This example send the last saved version of the Activeworkbook
    'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
        Dim OutApp As Object
        Dim OutMail As Object
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
    
        On Error Resume Next
        With OutMail
            .to = "ron@debruin.nl"
            .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
    

    https://www.rondebruin.nl/win/s1/outlook/amail1.htm

提交回复
热议问题