Sending mail using Outlook where the Send method fails

后端 未结 2 1507
攒了一身酷
攒了一身酷 2020-11-28 13:09

I use this code to send email from Excel:

Sub Mail_workbook_Outlook_1()
\'Working in Excel 2000-2013
\'This example send the last saved version of the Active         


        
相关标签:
2条回答
  • 2020-11-28 13:39

    Try this code.

    Sub Email_ActiveSheet_As_PDF()
    
    'Do not forget to change the email ID
    'before running this code
    
    Dim OutApp As Object
    Dim OutMail As Object
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim FileFullPath As String
    
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With
    
    ' Temporary file path where pdf
    ' file will be saved before
    ' sending it in email by attaching it.
    
    TempFilePath = Environ$("temp") & "\"
    
    ' Now append a date and time stamp
    ' in your pdf file name. Naming convention
    ' can be changed based on your requirement.
    
    TempFileName = ActiveSheet.Name & "-" & Format(Now, "dd-mmm-yy h-mm-ss") & ".pdf"
    
    'Complete path of the file where it is saved
    FileFullPath = TempFilePath & TempFileName
    
    'Now Export the Activesshet as PDF with the given File Name and path
    
     On Error GoTo err
    With ActiveSheet
        .ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=FileFullPath, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
    End With
    
    'Now open a new mail
    
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    
    On Error Resume Next
    With OutMail
    .To = StrToReceipent
    .CC = StrCCReceipent
    .BCC = StrBCCReceipent
    .Subject = StrSubject
    .Body = StrBody
        .Attachments.Add FileFullPath '--- full path of the pdf where it is saved
        .Send   'or use .Display to show you the email before sending it.
        .Display
    End With
    On Error GoTo 0
    
    'Since mail has been sent with the attachment
    'Now delete the pdf file from the temp folder
    
    Kill FileFullPath
    
    'set nothing to the objects created
    Set OutMail = Nothing
    Set OutApp = Nothing
    
    'Now set the application properties back to true
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
    MsgBox ("Email has been Sent Successfully")
    Exit Sub
    err:
        MsgBox err.Description
    
    End Sub
    
    0 讨论(0)
  • 2020-11-28 13:51

    Change .Send to .Display and put SendKeys "^{ENTER}" before the With OutMail line.

    0 讨论(0)
提交回复
热议问题