【问题标题】:How can I send an Excel file via email?如何通过电子邮件发送 Excel 文件?
【发布时间】:2019-02-05 22:14:53
【问题描述】:

我必须每月创建一个 Excel 文件并通过电子邮件发送给我的老板。 我想使用 VBA 代码将文件作为附件发送,但我的 VBA 代码不起作用并在确认后要求调试。

我的代码:

Sub EMail() 
ActiveWorkbook.SendMail Recipients:="user@gmail.com" 
End Sub

【问题讨论】:

标签: excel vba email email-attachments


【解决方案1】:

信用到期的信用...这直接来自Ron de Bruin网站。

Sub Mail_workbook_Outlook_1()
'Working in Excel 2000-2016
'This example send the last saved version of the Activeworkbook
'For Tips see: https://www.rondebruin.nl/win/s1/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

【讨论】:

    【解决方案2】:

    这是一个如何将活动工作簿作为附件发送的示例

    Option Explicit
    Sub EmailFile()
        Dim olApp As Object
        Dim olMail As Object
        Dim olSubject As String
    
    '   // Turn off screen updating
        Application.ScreenUpdating = False
    
        Set olApp = CreateObject("Outlook.Application")
        Set olMail = olApp.CreateItem(olMailItem)
    
        olSubject = "This Subject Line"
    
        With olMail
            .Display
        End With
    
        With olMail
            .To = "0m3r@EMail.com"
            .CC = ""
            .BCC = ""
            .Subject = olSubject
            .HTMLBody = "This Body Text " & .HTMLBody
            .Attachments.Add ActiveWorkbook.FullName
            '.Attachments.Add ("C:\test.txt") ' add other file
    '        .Send   'or use .Display
            .Display
        End With
    
    '   // Restore screen updating
        Application.ScreenUpdating = True
    
        Set olMail = Nothing
        Set olApp = Nothing
    
    End Sub
    

    【讨论】:

      【解决方案3】:

      您可以使用如下示例所示的 VBA 代码 sn-p:

      Sub SendEmailWithAttachment() 
       Dim myItem As Outlook.MailItem 
       Dim myAttachments As Outlook.Attachments
      
       Set myItem = Application.CreateItem(olMailItem) 
       Set myAttachments = myItem.Attachments 
       myAttachments.Add "C:\MyExcelFile.xls", olByValue, 1, "Test"
       myItem.To = "Recipient Address"
       myItem.Send
      
       'alternatively, you may display the item before sending
       'myItem.Display
      End Sub
      

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-13
        • 1970-01-01
        • 2016-01-05
        • 2011-06-02
        • 1970-01-01
        相关资源
        最近更新 更多