VBA Code outlook attach file error message

mrsushi

Board Regular
Joined
Nov 18, 2006
Messages
180
Office Version
  1. 2010
Hi

I have a script which basically produces a message if the file doesn't exist in the folder. However, the code seems to carry on and onto this part of the code and flags the error message. How can this be tweaked pls?

.Attachments.Add "S:\Bloomberg\Bloomberg.xls"

1649789695194.png






Sub MailMacro()

'On Error GoTo Errorhandler

Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim lastRow As Long
Dim Datestamp As String
Datestamp = Sheets("Input II").Range("E1")


Dim Path As String
Dim Filename As String
Dim Datestamp2 As String

Datestamp2 = Format(Sheets("Input II").Range("E1"), "yyyymmdd")
Path = "S:\Bloomberg\"
Filename = "Bloomberg"

lastRow = Range("A" & Rows.Count).End(xlUp).Row

Application.DisplayAlerts = False
'ActiveWorkbook.Save

Set rng = Nothing
' Only send the visible cells in the selection.

'On Error Resume Next
Set rng = Range("A1:I" & lastRow).SpecialCells(xlCellTypeVisible)
'Set rng = Union(Range("A2:P" & lastRow), Range("T2:T" & lastRow)).SpecialCells(xlCellTypeVisible)


'On Error GoTo 0

If rng Is Nothing Then
MsgBox "The selection is not a range or the sheet is protected. " & _
vbNewLine & "Please correct and try again.", vbOKOnly
Exit Sub
End If

With Application
.EnableEvents = False
.ScreenUpdating = False
End With

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)


'Exit if file does not exist
If Len(Dir("S:\Bloomberg\Bloomberg.xls", vbDirectory)) = 0 Then _
MsgBox Datestamp & " File not found, proceed to attach manually", vbOKOnly


With OutMail
'.SentOnBehalfOfName = "Pricing@lgim.com"
.To = "Martin.Sew@lgim.com"
.CC = ""
.BCC = ""
.Subject = "Bloomberg file have been saved down as at " & Datestamp
.HTMLBODY = "<BODY style=font-size:13pt;font-family:Calibri<p>" & _
"<i><b>Bloomberg.xls</b></i>" & " have been saved down." & _
"<BR>" & _
"<BR><BR>" & _
RangetoHTML(rng)
.Attachments.Add "S:\Bloomberg\Bloomberg.xls"
.Display

End With
On Error GoTo 0

With Application
.EnableEvents = True
.ScreenUpdating = True
End With

Set OutMail = Nothing
Set OutApp = Nothing

Application.DisplayAlerts = True

End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I have a script which basically produces a message if the file doesn't exist in the folder. However, the code seems to carry on and onto this part of the code and flags the error message.
You don't exit after the message is displayed.
VBA Code:
'Exit if file does not exist
If Len(Dir("S:\Bloomberg\Bloomberg.xls")) = 0 Then
   MsgBox Datestamp & " File not found, proceed to attach manually", vbOKOnly
   Exit Sub
End If
Please use VBA code tags.
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,214
Members
449,074
Latest member
cancansova

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