Matt Rowles
Matt Rowles

Reputation: 8070

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

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

Dim mobjOutlook, mobjActiveExp, 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 = "[email protected]"
    .Subject = "theSubject"
    .Body = "some text"
    .Attachments.Add "C:/The/File/Path.doc"
    '*I need to check here if the above line worked*
    .Send
End With

How can I test if the attachment works before sending? Is this possible? For some reason even if it doesn't, it still sends the email without the attachment.

I was thinking of somehow utilizing the '.Save' option.

Any thoughts or suggestions are much appreciated, thanks.

Upvotes: 3

Views: 3101

Answers (1)

brettdj
brettdj

Reputation: 55702

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 = "[email protected]"
    .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

Upvotes: 5

Related Questions