Как я могу отправить файл Excel по электронной почте?

Я должен создавать и отправлять файл Excel каждый месяц по электронной почте своему начальнику. Я хочу использовать код VBA для отправки файла в виде вложения, но мой код VBA не работает и запрашивает отладку после подтверждения.

Мой код:

Sub EMail() 
ActiveWorkbook.SendMail Recipients:="[email protected]" 
End Sub

person Abhinn Wasav    schedule 06.03.2016    source источник
comment
msdn.microsoft.com/en-in/library/office/ff869553. aspx   -  person Stupid_Intern    schedule 06.03.2016
comment
Пожалуйста, уточните, что вы подразумеваете под не работает, и запросите отладку после подтверждения. Этот код работает для меня.   -  person David Zemens    schedule 09.03.2016


Ответы (3)


Вот пример того, как отправить активную книгу в виде вложения

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 = "[email protected]"
        .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
person 0m3r    schedule 08.03.2016

Кредит там, где должен быть кредит... Это прямо с сайта 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 = "[email protected]"
        .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
person Chrismas007    schedule 09.03.2016

Вы можете использовать фрагмент кода VBA, как показано в следующем примере:

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

Надеюсь, это может помочь.

person Alexander Bell    schedule 06.03.2016