VBA: Error Msg if Attachment does not exist

Xlacs

Board Regular
Joined
Mar 31, 2021
Messages
105
Office Version
  1. 2016
Platform
  1. Windows
Hello Everyone,

Im looking for a solution for this particular problem right now.

Basically, what I wanted to happen is to have a msgbox prompt if the attachment for the below does not exist.
But, it seems my below code is not working.

Hoping someone would check what I am missing. Thank you

VBA Code:
Private Sub CommandButton2_Click()

Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Set OutApp = New Outlook.Application
Set OutMail = OutApp.CreateItem(0)

With OutMail
    .To = Me.txt2.Value
    .Subject = "Score Card"
    .Body = "Dear " & Me.txt3 & "," & vbCrLf & vbCrLf & "Please find attached of your  " & Me.Month.Caption & " Score Card"
    .Attachments.Add ("D:\" & Me.ComboBox1 & "_" & Me.txt3 & ".pdf")
    .Display
    
    
  If OutMail.Attachments <> ("D:\" & Me.ComboBox1 & "_" & Me.txt3 & ".pdf") Then
        MsgBox "No Attachment currently.", vbExclamation, "Message"
        Cancel = True
    End If
    
  
End With
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Question might be, why would the attachment not exist, since presumably the combo only lists valid items but an item was not chosen?
Then hide the button until a combo list item is chosen?
Or if the button is clicked, first check that a list item has been chosen, and if not, exit the sub?
You could also check the count of the attachments object, after you add, but that seems like the least desirable approach to me.
 
Upvote 0
Solution

Forum statistics

Threads
1,213,556
Messages
6,114,284
Members
448,562
Latest member
Flashbond

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top