+ Reply to Thread
Results 1 to 5 of 5

Reg: Excel file autosending email using Microsoft Outlook

Hybrid View

  1. #1
    Registered User
    Join Date
    09-25-2014
    Location
    Hyderabad India
    MS-Off Ver
    Office 2019
    Posts
    11

    Unhappy Reg: Excel file autosending email using Microsoft Outlook

    Hi All,

    I am new to VBA and trying to learn the nitty-gritty of excel vba. My query is that I am trying to send an email automatically from my excel sheet through outlook 2007 application. It is a simple macro which checks for the condition for the first cell in row while using two other cell in the same row as email id and message bidy. The issue that I am facing is the loop which according to me is pretty simple but still it is giving the error "The object has been moved/deleted" while executing and sending the email successfully for the first cell in the range (Cells_M).

    Sub SendMail()
    
        Dim olApp As Outlook.Application
        Dim olMail As MailItem
        Dim Cell As Range
        Dim Cells_M As Range
        
            Set olApp = New Outlook.Application
            Set olMail = olApp.CreateItem(olMailItem)
        
            ActiveWorkbook.Save
        
            Set Cells_M = Range("A4:A6")
            
            For Each Cell In Cells_M
            
                If Cell.Value > Date Then
            
                    With olMail
                        .To = Cell.Offset(0, 4).Value 'I have tried Cell.Offset(0, 4).text also but same error". This cell has the email id
                        .Subject = "Pending Reports"
                        .Body = Cell.Offset(0, 1).Text 'This cell has text or the report which is pending
                        .Send
                    End With
            
                Else
                    
                End If
            
                Next Cell
    
        Set olMail = Nothing
        Set olApp = Nothing
    
    End Sub
    Attached Files Attached Files
    Last edited by abhimails; 09-25-2014 at 08:15 AM. Reason: moderator guiding me on various rules for posting

  2. #2
    Registered User
    Join Date
    09-25-2014
    Location
    Hyderabad India
    MS-Off Ver
    Office 2019
    Posts
    11

    Re: Reg: Excel file autosending email using Microsoft Outlook

    Fotis, Can you please let people post their answers or solutions in this post and remove your message... I am new to this forum pls

  3. #3
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Reg: Excel file autosending email using Microsoft Outlook

    Hi abhimails

    Welcome to the Forum!!!

    I'm not able to duplicate the issue but you may wish to move the two lines of Code as indicated.
    Option Explicit
    
    Sub SendMail()
    
      Dim olApp As Outlook.Application
      Dim olMail As MailItem
      'Dim Rng As Date
      Dim Cell As Range
      Dim Cells_M As Range
    
      '          Set olApp = New Outlook.Application       '<---Move this line
      '          Set olMail = olApp.CreateItem(olMailItem) '<---Move this line
    
      '  ActiveWorkbook.Save
    
      'Rng = ActiveSheet.Range("A2")
    
      Set Cells_M = Range("A2:A10")
    
      For Each Cell In Cells_M
    
        If Cell.Value > Date Then
          Set olApp = New Outlook.Application       '<-----to here
          Set olMail = olApp.CreateItem(olMailItem) '<-----to here
          With olMail
            .To = Cell.Offset(0, 4).Value
            '.CC = "[email protected]"
            .Subject = "Pending Reports"
            .Body = Cell.Offset(0, 1).Text
            .Send
            '        .Display
          End With
    
        Else
    
        End If
    
        'MsgBox ("Criteria not met")
    
      Next Cell
    
      Set olMail = Nothing
      Set olApp = Nothing
    
    End Sub
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  4. #4
    Registered User
    Join Date
    09-25-2014
    Location
    Hyderabad India
    MS-Off Ver
    Office 2019
    Posts
    11

    Reg: Excel file autosending email using Microsoft Outlook

    Hurrah ! Thanks a ton buddy.. m so happy

    I knew it has to be a minor change.

  5. #5
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Reg: Excel file autosending email using Microsoft Outlook

    You're welcome...glad I could help.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA to Verify an Email Address is Valid in Microsoft Outlook
    By Larry.LeBlanc@O in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-16-2014, 10:07 AM
  2. Birthday and work anniversary reminder from excel file to outlook email
    By paulbochniak in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-03-2014, 09:21 PM
  3. [SOLVED] Email excel file using outlook
    By cpoduri in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-31-2012, 07:35 PM
  4. outlook macro to pull in a table from excel file to email.
    By chewwy in forum Outlook Programming / VBA / Macros
    Replies: 0
    Last Post: 08-18-2009, 03:10 PM
  5. Replies: 2
    Last Post: 05-01-2006, 06:50 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1