Outlook attachment & Message box via Excel VBA

Sushiboy

New Member
Joined
Jan 18, 2019
Messages
24
Good evening,

I have the following VBA code which opens up from Excel, an outlook outgoing mail and attaches a specific excel attachment located in the S drive.

How can I incorporate a message box (in the below code) to notify if the attachment cannot be found in the location?

Many thanks
Martin




Sub GRD()


On Error GoTo Errorhandler
Dim OutApp, OutMail As Object
Dim DateStamp As String
Dim DateStamp2 As String
DateStamp = Range("C1")
DateStamp2 = Format(Range("C1"), "ddmmyyyy")
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = "Sushibo@hotmai.com"
.Cc = ""
.Subject = "GRD's as " & DateStamp
.HTMLBody = ""
.ATTACHMENTS.Add "S:\Operations\Archive" & DateStamp2 & " GDR.xlsx"
Errorhandler:
.Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Re: Outlook attachement & Message box via Excel VBA

Something like this.

Code:
Sub GRD()


On Error GoTo Errorhandler
Dim OutApp, OutMail As Object
Dim DateStamp As String
Dim DateStamp2 As String
DateStamp = Range("C1")
DateStamp2 = Format(Range("C1"), "ddmmyyyy")
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)


'New Code ****
Dim Path As String
Path = "S:\Operations\Archive" & DateStamp2 & " GDR.xlsx"




With OutMail
.To = "Sushibo@hotmai.com"
.Cc = ""
.Subject = "GRD's as " & DateStamp
.HTMLBody = ""
'New Code ***
If Path <> "" Then
    .ATTACHMENTS.Add Path
Else
    MsgBox "File not found", vbOKOnly, "Error"
End If


Errorhandler:
.Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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