Do While code error

Jingles3X

New Member
Joined
Oct 20, 2021
Messages
34
Office Version
  1. 2013
Platform
  1. Windows
Hello! I am new to vba (learning fairly quickly i think),am stuck and am hoping someone can help me. I keep getting an 'Object Required' error and I am not sure why/what I am missing.
I don't think it's anything major, but it's screwing me up and I am getting frustrated.
The code is:


VBA Code:
Sub Email_123()
Dim i As Integer
Dim w As Workbook
Dim ws As Worksheet
Dim xOTApp As Object
Dim myattachments As Object

 With CreateObject("Outlook.Application").CreateItem(0)

'Do While Not sContacts.Cells(i, column).Value = ""
             '  xEmailAddr = sContacts.Cells(i, column).Value & ";" & xEmailAddr
             '  i = i + 1
   ' Loop
   ' Set i = xOTApp.CreateItem(0)
    .To = " "
    .CC = " "
    .BCC = Range("B4").Value
    .Subject = Range("D4").Value
    .body = Range("B11")
    .attachments.Add Range("b7").Value
    .Display
  
 End With
End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Do you mean you get the error when the Loop is uncommented?
For one thing, it looks like you are trying to reference an object named "sContacts", but I do not that being defined anywhere in your code.
 
Upvote 0
The error i seem to be receiving is "Object required" with the Title of the sub highlighted yellow, and further down, the " Set i = " statement with I= in blue.
VBA Code:
Sub Email_123()
Dim i As Integer
Dim w As Workbook
Dim ws As Worksheet
Dim xOTApp As Object


 With CreateObject("Outlook.Application").CreateItem(0)

Do While Not ws("Contacts").Cells(i, column).Value = ""
               xEmailAddr = wsContacts.Cells(i, column).Value & ";" & xEmailAddr
               i = i + 1
    Loop
     Set i = xOTApp.CreateItem(0)
    .To = " "
    .CC = " "
    .BCC = Range("B4").Value
    .Subject = Range("D4").Value
    .body = Range("B11")
    .attachments.Add Range("b7").Value
    .Display
    
 End With
End Sub
 
Upvote 0
You have declared "i" to be an Integer here:
VBA Code:
Dim i As Integer
You do NOT use the "Set" command with numbers, only objects.

So this line of code is valid:
VBA Code:
i = i + 1
but this one is not:
VBA Code:
Set i = xOTApp.CreateItem(0)
as you cannot use "Set" with numbers.
 
Upvote 0
You have several undeclared variables and VBA will not know what to do with those. When you declare objects such as the worksheet object, they need to be "Set" to the appropriate reference. Since I don't know exactly what you are trying to accomplish or the structure of your file, I have stripped out the parts that are not related to my corrections. There may still be issues with the Outlook part that I haven't addressed.

VBA Code:
Dim iRow As Integer
Dim iCol As Integer
Dim ws As Worksheet

Set ws = Worksheets("Contacts")
iRow = 4
iCol = 1    'this is column A

    Do While Not ws.Cells(iRow, iCol).Value = ""
        xEmailAddr = wsContacts.Cells(iRow, iCol).Value & ";" & xEmailAddr
        iRow = iRow + 1
    Loop

Hope that helps.

Regards,
Ken
 
Upvote 0
Please see the code below. This was something I wrote for a colleague a few years ago. It creates and sends email from Outlook. The sample used only three columns: Email address, Subject, and Body text. It could be expanded by uncommenting the placeholders in code and editing to direct the code to the correct columns.

VBA Code:
Sub SendOutlookMail()

Dim rTo As Range
Dim strTo As String, strCc As String, strBcc As String, strSubject As String, strBody As String
Dim OutApp As Object
Dim OutMail As Object

Application.ScreenUpdating = False

Set OutApp = CreateObject("Outlook.Application")

Set rTo = Worksheets("Sheet1").Range("A2")

Do While Not IsEmpty(rTo.Value)
    Set OutMail = OutApp.CreateItem(0)
    strTo = rTo.Value
    'strCc = ""
    'strBcc = ""
    strSubject = rTo.Cells(1, 2)
    strBody = rTo.Cells(1, 3)
    
    With OutMail
        .Subject = strSubject
        .To = strTo
        '.cc = strCc
        '.BCC = strBcc
        .Body = strBody
        .send
    End With
    Set rTo = rTo.Cells(2, 1)
Loop

MsgBox "Your message has been sent."

Application.ScreenUpdating = True

End Sub

Hope that helps.

Regards,
Ken
 
Upvote 0
Please see the code below. This was something I wrote for a colleague a few years ago. It creates and sends email from Outlook. The sample used only three columns: Email address, Subject, and Body text. It could be expanded by uncommenting the placeholders in code and editing to direct the code to the correct columns.

VBA Code:
Sub SendOutlookMail()

Dim rTo As Range
Dim strTo As String, strCc As String, strBcc As String, strSubject As String, strBody As String
Dim OutApp As Object
Dim OutMail As Object

Application.ScreenUpdating = False

Set OutApp = CreateObject("Outlook.Application")

Set rTo = Worksheets("Sheet1").Range("A2")

Do While Not IsEmpty(rTo.Value)
    Set OutMail = OutApp.CreateItem(0)
    strTo = rTo.Value
    'strCc = ""
    'strBcc = ""
    strSubject = rTo.Cells(1, 2)
    strBody = rTo.Cells(1, 3)
   
    With OutMail
        .Subject = strSubject
        .To = strTo
        '.cc = strCc
        '.BCC = strBcc
        .Body = strBody
        .send
    End With
    Set rTo = rTo.Cells(2, 1)
Loop

MsgBox "Your message has been sent."

Application.ScreenUpdating = True

End Sub

Hope that helps.

Regards,
Ken
Thank you Ken. What I am hoping to accomplish with this is to be able to keep multiple email lists (one per cell) in multiple columns. For example Column A Might be 1st Shift, Column B might be 2nd Shift, and so on. I would like to have sheet 1 be where i enter the subject, body, and filepath of the attachment, and sheet 2 to be where the email lists are kept. I will play around with your code, as it does seem to be a bit easier to read. Much appreciated.
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,192
Members
449,072
Latest member
DW Draft

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