Rohith1324
Board Regular
- Joined
- Feb 27, 2018
- Messages
- 114
Hi,
I have the below code where the Mail is triggered even when the attachment ( File ) is not available in the specified path.
Can someone suggest how can the code ensure the mail is triggered only if the file/files are available in specified path.
If the value in If c.Offset(0, 9).Value = "SW" Then it should check the file from path - picName = c.Offset(0, 2).Value only if the file exists in the path it should trigger the mail with the attachment.
Else
it should check the files in both the path - "picName = c.Offset(0, 2).Value" & Zipname = c.Offset(0, 3).Value only if the both the files are existing then it should trigger the mail - even single file at one location is not available then it should not trigger the mail.
The code i'm using is as follow :
Dim picName As String
Dim Zipname As String
Dim c As Range
Dim OutLookApp As Object
Dim OutLookMailItem As Object
Dim i As Integer
On Error Resume Next
For Each c In Range("A2:A" & Cells(Rows.Count, "G").End(xlUp).Row).Cells
picName = c.Offset(0, 2).Value
Zipname = c.Offset(0, 3).Value
Debug.Print picName
Debug.Print Zipname
Set OutLookApp = CreateObject("Outlook.application")
Set OutLookMailItem = OutLookApp.CreateItem(0)
With OutLookMailItem
.To = c.Offset(0, 4).Value
.CC = c.Offset(0, 5).Value
.Subject = c.Offset(0, 1).Value
.Attachments.Add picName
.Attachments.Add Zipname
.HTMLBody = .HTMLBody & "<b><font size='03' color='black'>Hi</font></b>"
If c.Offset(0, 9).Value = "SW" Then
.HTMLBody = .HTMLBody & "<br><br><b><font size='05' color=#39036F>validation required </b></font>"
Else
.HTMLBody = .HTMLBody & "<br><br><b><font size='05' color=#FF00FF><span style=background-color:'yellow'>Validation complete</b></span></font>"
End If
.HTMLBody = .HTMLBody & "<br><br>Regards"
.HTMLBody = .HTMLBody & "<br>Rohith M"
'.Display
.Send
End With
If OutLookMailItem.Send Then
c.Offset(0, 10).Value = "success"
Else
c.Offset(0, 10).Value = "failed"
End If
Next c
End Sub
I have the below code where the Mail is triggered even when the attachment ( File ) is not available in the specified path.
Can someone suggest how can the code ensure the mail is triggered only if the file/files are available in specified path.
If the value in If c.Offset(0, 9).Value = "SW" Then it should check the file from path - picName = c.Offset(0, 2).Value only if the file exists in the path it should trigger the mail with the attachment.
Else
it should check the files in both the path - "picName = c.Offset(0, 2).Value" & Zipname = c.Offset(0, 3).Value only if the both the files are existing then it should trigger the mail - even single file at one location is not available then it should not trigger the mail.
The code i'm using is as follow :
Dim picName As String
Dim Zipname As String
Dim c As Range
Dim OutLookApp As Object
Dim OutLookMailItem As Object
Dim i As Integer
On Error Resume Next
For Each c In Range("A2:A" & Cells(Rows.Count, "G").End(xlUp).Row).Cells
picName = c.Offset(0, 2).Value
Zipname = c.Offset(0, 3).Value
Debug.Print picName
Debug.Print Zipname
Set OutLookApp = CreateObject("Outlook.application")
Set OutLookMailItem = OutLookApp.CreateItem(0)
With OutLookMailItem
.To = c.Offset(0, 4).Value
.CC = c.Offset(0, 5).Value
.Subject = c.Offset(0, 1).Value
.Attachments.Add picName
.Attachments.Add Zipname
.HTMLBody = .HTMLBody & "<b><font size='03' color='black'>Hi</font></b>"
If c.Offset(0, 9).Value = "SW" Then
.HTMLBody = .HTMLBody & "<br><br><b><font size='05' color=#39036F>validation required </b></font>"
Else
.HTMLBody = .HTMLBody & "<br><br><b><font size='05' color=#FF00FF><span style=background-color:'yellow'>Validation complete</b></span></font>"
End If
.HTMLBody = .HTMLBody & "<br><br>Regards"
.HTMLBody = .HTMLBody & "<br>Rohith M"
'.Display
.Send
End With
If OutLookMailItem.Send Then
c.Offset(0, 10).Value = "success"
Else
c.Offset(0, 10).Value = "failed"
End If
Next c
End Sub