Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 813
Matt Rowles Avatar asked Dec 16 '25 15:12

Matt Rowles


1 Answers

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
like image 108
brettdj Avatar answered Dec 19 '25 06:12

brettdj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!