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

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

lrobbo314

Well-known Member
Joined
Jul 14, 2008
Messages
3,196
Office Version
  1. 365
Platform
  1. Windows
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:

Forum statistics

Threads
1,136,275
Messages
5,674,779
Members
419,524
Latest member
helensesc

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
Top