How to check if .Attachment.Add “filename” is successful before send

后端 未结 1 1800
误落风尘
误落风尘 2021-01-20 14:24

I have some code that creates a Mail object (Outlook), attaches a file and sends it.

Dim mobjOutlook, mobjActiveExp, mobjNewMail As Object

\'Create Outlook          


        
相关标签:
1条回答
  • 2021-01-20 14:31

    You could just test the number of attachments in the email were > 0

    Also

    Dim mobjOutlook, mobjActiveExp, mobjNewMail As Object
    will dim the first two variables as variants, so I have recut this below

    Sub Test()
    Dim mobjOutlook As Object
    Dim mobjActiveExp As Object
    Dim mobjNewMail As Object
    
    'Create Outlook objects
    Set mobjOutlook = CreateObject("Outlook.Application")
    Set mobjActiveExp = mobjOutlook.ActiveExplorer
    Set mobjNewMail = mobjOutlook.CreateItem(olMailItem)
    
    'Setup and send email
    With mobjNewMail
        .To = "someone@test.com"
        .Subject = "theSubject"
        .Body = "some text"
        .attachments.Add "C:\temp\step1.png"
        If .attachments.Count > 0 Then
            .Send
        Else
            MsgBox "No attachment", vbCritical
        End If
    End With
    End Sub
    
    0 讨论(0)
提交回复
热议问题