Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / March 2008

Tip: Looking for answers? Try searching our database.

mailing from excel - For each cell - already in use - error

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Eddy Stan - 22 Mar 2008 18:13 GMT
hi
i am trying to send mail from excel file
Name   mail id                    check   body
John 1  xxxx@xxxx.com      yes    this is to remind you...1
john 2  xxxx@xxxx.com       yes    this is to remind you...2
i have edited the code from http://www.rondebruin.nl/mail/tips2.htm
i get For each cell ... next error for the strbody loop, giving the code below

------------------------------------

Sub TestFile()
   Dim OutApp As Object
   Dim OutMail As Object
   Dim cell As Range
   Dim strto As String
   Dim strbody As String
   
   Application.ScreenUpdating = False
   Set OutApp = CreateObject("Outlook.Application")
   OutApp.Session.Logon

   On Error GoTo cleanup
'    For Each cell In
Sheets("Sheet1").Columns("B").Cells.SpecialCells(xlCellTypeConstants)
       If cell.Value Like "?*@?*.?*" And LCase(cell.Offset(0, 1).Value) =
"yes" Then
               
     ' TO CHOOSE DIFFERENT TO-MAIL ID
               For Each cell In ThisWorkbook.Sheets("Sheet1") _
                   .Range("B3:B5").Cells.SpecialCells(xlCellTypeConstants)
                   If cell.Value Like "?*@?*.?*" Then
                       strto = strto & cell.Value & ";"
                   End If

' this loop is giving error as the For Each cell is already in use
'*********************************************
     ' TO CHOOSE MATTER TO PRINT IN MAIL BODY
               For Each cell In ThisWorkbook.Sheets("Sheet1").Range("D3:D5")
                   strbody = strbody & cell.Value & vbNewLine
               Next
           
           Set OutMail = OutApp.CreateItem(0)

           On Error Resume Next
           With OutMail
               .To = strto
               .Subject = "Reminder"
               .body = strbody
               
      '         .bODY = "Dear " & cell.Offset(0, -1).Value & vbNewLine &
vbNewLine & _
      '                 "Please contact us to discuss bringing your account
up to date"
      '         You can add files also like this
      '         .Attachments.Add ("C:\test.txt")
               
               .Send  'Or use Display
 
           End With

           On Error GoTo 0
               
               If Len(strto) > 0 Then strto = Left(strto, Len(strto) - 1)
           
           Set OutMail = Nothing
'        End If
      Next cell
   End If
   

cleanup:
   Set OutApp = Nothing
   Application.ScreenUpdating = True
End Sub

------------------------------------------------

can u please help to get out of this loop

advance thanks
Leith Ross - 22 Mar 2008 18:51 GMT
On Mar 22, 9:13 am, Eddy Stan <EddyS...@discussions.microsoft.com>
wrote:
> hi
> i am trying to send mail from excel file
[quoted text clipped - 75 lines]
>
> advance thanks

Hello Eddy Stan,

The For Each Loop above the one generating the error has the same
control value name CELL. Change the name of the Control value in the
loop causing the error. For example...
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
     ' TO CHOOSE MATTER TO PRINT IN MAIL BODY
               For Each C In
ThisWorkbook.Sheets("Sheet1").Range("D3:D5")
                   strbody = strbody & cell.Value & vbNewLine
               Next C
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sincerely,
Leith Ross
Eddy Stan - 24 Mar 2008 15:16 GMT
> Hello Eddy Stan,
>
[quoted text clipped - 11 lines]
> Sincerely,
> Leith Ross

Sub TestFile()
   Dim OutApp As Object
   Dim OutMail As Object
   Dim cell As Range
   Dim strto As String
   Dim strbody As String
   
   Application.ScreenUpdating = False
   Set OutApp = CreateObject("Outlook.Application")
   OutApp.Session.Logon

   On Error GoTo cleanup
'    For Each cell In
Sheets("Sheet1").Columns("B").Cells.SpecialCells(xlCellTypeConstants)
       If cell.Value Like "?*@?*.?*" And LCase(cell.Offset(0, 1).Value) =
"yes" Then
               
     ' TO CHOOSE DIFFERENT TO-MAIL ID
               For Each cell In ThisWorkbook.Sheets("Sheet1") _
                   .Range("B3:B5").Cells.SpecialCells(xlCellTypeConstants)
                   If cell.Value Like "?*@?*.?*" Then
                       strto = strto & cell.Value & ";"
                   End If
     ' TO CHOOSE MATTER TO PRINT IN MAIL BODY
               For Each C In ThisWorkbook.Sheets("Sheet1").Range("D3:D5")
                   strbody = strbody & cell.Value & vbNewLine
               Next C
                           
           Set OutMail = OutApp.CreateItem(0)

           On Error Resume Next
           With OutMail
               .To = strto
               .Subject = "Reminder"
               .body = "Dear " & cell.Offset(0, -1).Value & vbNewLine &
vbNewLine & _
                       "Please contact us to discuss bringing your account
up to date"
      '         You can add files also like this
      '         .Attachments.Add ("C:\test.txt")
               
               .Send  'Or use Display
 
           End With

           On Error GoTo 0
               
               If Len(strto) > 0 Then strto = Left(strto, Len(strto) - 1)
           
           Set OutMail = Nothing
'        End If

      Next cell
    End If
   

cleanup:
   Set OutApp = Nothing
   Application.ScreenUpdating = True
End Sub

I have changed as you have advised and the error is not coming but at the
same time the mail is being processed.

please into the above code.
Eddy Stan - 24 Mar 2008 15:43 GMT
Hi

I have changed as you have advised. The error is not coming up but at the
same time the mail is NOT being processed.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.