VBA to loop through rows sending email

Zhaki

New Member
Joined
Oct 5, 2017
Messages
11
Hey,

i have entered the following code and have absolutely no trouble executing, however, i dont know how to move it to the next row and perform the macro again. it basically is for sending emails....also i would like to update the excel sheet once the email is sent automatically...to say email sent.
Code:
Sub Email_Notification()

Dim OutlookApp As Object
Dim OutlookMail As Object
Dim ToName As String
Dim CCName As String
Dim SendEmail As Integer
Dim cell As Range

ToName = Range("N9").Value
CCName = Range("P9").Value
mAttName = Range("C9").Value

Application.ScreenUpdating = False



SendEmail = MsgBox("Do you want to send the following Email?" _
& vbNewLine & vbNewLine & "Subject: " & SubName & vbNewLine & vbNewLine & _
"To: " & ToName & vbNewLine & _
"CC: " & CCName & vbNewLine & _
"With the following Attachments: " & vbNewLine & Range("C9").Value, _
vbQuestion + vbYesNo, "Send Email")


If SendEmail = vbYes Then

End If

Set OutlookApp = CreateObject("Outlook.application")
Set OutlookMailitem = OutlookApp.CreateItem(0)

With OutlookMailitem
.Display
.To = Range("N9").Value
.CC = Range("P9").Value
.Subject = "TMT-KSD File Review Request"
.HTMLBody = Body & Attachment
.Body = "Kindly review the attached file for TMT-KSD upload." & vbNewLine & "Regards."
.Attachments.Add Range("L9").Value
.send
 
End With

Set OutlookMail = Nothing
Set OutlookApp = Nothing
Application.ScreenUpdating = True



End Sub
your help would be greatly appreciated.
 
Last edited by a moderator:
Re: issues with VBA, please help!

I am getting a 'type mismatch' for this line ....cant figure out why...

.Body = "Dear" & Worksheets(Sheet1).Range("J" & x) & vbNewLine & vbNewLine & "Your request for file/document review and upload have been processed." & vbNewLine & "Following is the Status update:" & Range("F" & x) & Range("G" & x) & vbNewLine & vbNewLine & "Regards."

I am using the same code as above tweeking a few things here and there....
sorry for being annoying!
 
Last edited:
Upvote 0

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Re: issues with VBA, please help!

You're welcome, glad it's resolved

guys one more thing, I am getting a 'type mismatch error' for this line

.Body = "Dear" & Worksheets(Sheet1).Range("J" & x) & vbNewLine & vbNewLine & "Your request for file/document review and upload have been processed." & vbNewLine & "Following is the Status update:" & Range("F" & x) & Range("G" & x) & vbNewLine & vbNewLine & "Regards."
I am using the same code as above...

sorry for being so annoying. thanks.
 
Upvote 0
Re: issues with VBA, please help!

guys one more thing, I am getting a 'type mismatch error' for this line

.Body = "Dear" & Worksheets(Sheet1).Range("J" & x) & vbNewLine & vbNewLine & "Your request for file/document review and upload have been processed." & vbNewLine & "Following is the Status update:" & Range("F" & x) & Range("G" & x) & vbNewLine & vbNewLine & "Regards."
I am using the same code as above...

sorry for being so annoying. thanks.


urrmmmm...figured it out.... was getting a issue with Worksheets(Sheet1).Range("J" & x)
 
Upvote 0
Re: issues with VBA, please help!

("Sheet1").

Already found I see.
 
Last edited:
Upvote 0
Re: issues with VBA, please help!

You're welcome, glad it's resolved


ok so I have another problem, here's the code same as above...with only a few minor differences...cell references...

Sub Email_Requestor()
Dim OutlookApp As Object
Dim OutlookMail As Object
Dim ToName As String
Dim CCName As String
Dim SendEmail As Integer
Dim cell As Range
Dim strMsg As String

Dim x As Long
Dim LR As Long

Application.ScreenUpdating = False
LR = Cells(Rows.Count, 2).End(xlUp).Row
For x = 9 To LR
ToName = Range("L" & x).Value
CCName = Range("M" & x).Value
BCCName = Range("N" & x).Value
mattname = Range("B" & x).Value
SubName = "TMT-KSD File"

If Range("O" & x).Value <> "Yes" Then
strMsg = "Do you want to send the following Email?@1@1Subject: " & SubName & _
"@1@1To: " & ToName & "@1CC: " & CCName
strMsg = Replace(strMsg, "@1", vbCrLf)


If MsgBox(strMsg, vbQuestion + vbYesNo, "Send Email") = vbYes Then

Set OutlookApp = CreateObject("Outlook.application")
Set OutlookMailitem = OutlookApp.CreateItem(0)


With OutlookMailitem

.To = ToName
.CC = CCName
.BCC = BCCName
.Subject = "TMT-KSD File Review Request"
.Body = "Dear Requestor" & vbNewLine & vbNewLine & "Your request for file/document review and upload have been processed." & vbNewLine & "Following is the Status update: " & Range("F" & x) & vbNewLine & "Comments: " & Range("G" & x) & vbNewLine & vbNewLine & "Kind Regards."
.Display

End With

Set OutlookMail = Nothing
Set OutlookApp = Nothing

Range("O" & x).Value = "Yes"
Else
Range("O" & x).Value = "No"
End If

End If
Next x
Application.ScreenUpdating = True

End Sub
<strike></strike>

now the thing is once the I click on the command button the msgbox and the email goes crazy on a loop......I don't know how to insert a screen shot here. but the msgbox keeps repeating one after the other even without any data in the row and without clicking on the command button.

help...:|
 
Upvote 0
Re: issues with VBA, please help!

ok so I have another problem, here's the code same as above...with only a few minor differences...cell references...

Sub Email_Requestor()
Dim OutlookApp As Object
Dim OutlookMail As Object
Dim ToName As String
Dim CCName As String
Dim SendEmail As Integer
Dim cell As Range
Dim strMsg As String

Dim x As Long
Dim LR As Long

Application.ScreenUpdating = False
LR = Cells(Rows.Count, 2).End(xlUp).Row
For x = 9 To LR
ToName = Range("L" & x).Value
CCName = Range("M" & x).Value
BCCName = Range("N" & x).Value
mattname = Range("B" & x).Value
SubName = "TMT-KSD File"

If Range("O" & x).Value <> "Yes" Then
strMsg = "Do you want to send the following Email?@1@1Subject: " & SubName & _
"@1@1To: " & ToName & "@1CC: " & CCName
strMsg = Replace(strMsg, "@1", vbCrLf)


If MsgBox(strMsg, vbQuestion + vbYesNo, "Send Email") = vbYes Then

Set OutlookApp = CreateObject("Outlook.application")
Set OutlookMailitem = OutlookApp.CreateItem(0)


With OutlookMailitem

.To = ToName
.CC = CCName
.BCC = BCCName
.Subject = "TMT-KSD File Review Request"
.Body = "Dear Requestor" & vbNewLine & vbNewLine & "Your request for file/document review and upload have been processed." & vbNewLine & "Following is the Status update: " & Range("F" & x) & vbNewLine & "Comments: " & Range("G" & x) & vbNewLine & vbNewLine & "Kind Regards."
.Display

End With

Set OutlookMail = Nothing
Set OutlookApp = Nothing

Range("O" & x).Value = "Yes"
Else
Range("O" & x).Value = "No"
End If

End If
Next x
Application.ScreenUpdating = True

End Sub
<strike></strike>

now the thing is once the I click on the command button the msgbox and the email goes crazy on a loop......I don't know how to insert a screen shot here. but the msgbox keeps repeating one after the other even without any data in the row and without clicking on the command button.

help...:|




Resolved....I added the wrong cell reference.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,857
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