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

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
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,214,957
Messages
6,122,472
Members
449,087
Latest member
RExcelSearch

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