Help with vbNewLine for Email to Loop through values in Column A

amitcohen

Board Regular
Joined
Jan 14, 2010
Messages
118
Hi
I'm trying to create code for the Email Body that will contain more the just one row.
I'm using the & vbNewLine & but when it come to the place where I need to refer to the cell in column A, VBA editor gives me error.
:(

Can you spot the problem?

Code:
Sub Mail_With_Loop()
Dim olApp As Outlook.Application
Dim olMail As MailItem

For i = 2 To 50 Step 1
Set olMail = olApp.CreateItem(olMailItem)
With olMail
.To = Range("H1").Value
.Body = "Hello" & vbNewLine & [COLOR=Red]Range(A" & i &").Value[/COLOR] & vbNewLine & "Text line" & vbNewLine & "Another text line" & vbNewLine & "And another one here"

.Display
End With
Next i
Set olMail = Nothing

Set olApp = Nothing

End Sub
Thanks,
Amit
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi Jon
Shalom ;)
Thank you for the quick replay.

Well, it seems to be fine now, as VBA parse the value..
However its Yellow highlighting the line code:
Code:
[COLOR=Red]Set olMail = olApp.CreateItem(olMailItem)[/COLOR]
Am (more) confuse now..

Amit
 
Upvote 0
Hi Amit
Change these
Code:
Dim olApp As Outlook.Application
Dim olMail As MailItem

TO

Dim olApp As Object
Dim olMail As Object
 
Upvote 0
Hi Michael,
Thanks, but changing the lines didn't help.


Have any idea how to go around it?
 
Upvote 0
If it's late-binding you're after...

Code:
    Dim olApp As Object
    Dim olMail As Object

    Set olApp = CreateObject("Outlook.Application")
    Set olMail = olApp.CreateItem(0)
 
Upvote 0
Ok, try
Code:
Sub Mail_With_Loop()
Dim olApp As Object
Dim olMail As Object

For i = 2 To 50 Step 1
        Set Mail_Object = CreateObject("Outlook.Application")
        With Mail_Object.CreateItem(o)
.To = Range("H1").Value
.Body = "Hello" & vbNewLine & Range("A" & i).Value & Chr(13) & "Text line" & Chr(13) & "Another text line" & Chr(13) & "And another one here"

.Display
End With
Next i
Set olMail = Nothing

Set olApp = Nothing

End Sub
 
Upvote 0
Excellent....glad it worked.
Thanks for the feedback !
 
Upvote 0

Forum statistics

Threads
1,214,391
Messages
6,119,239
Members
448,879
Latest member
VanGirl

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