Results 1 to 4 of 4
  1. #1
    mercapto is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jan 2012
    Location
    Spain
    Posts
    91

    Sending several emails causes Outlook crash

    I have this code for sending emails via Outlook. I saw it somewhere and made some changes.



    Code:
    Public Sub EnviarEmail(strEmail As String, strAsunto As String, srtCuerpoEmail As String, Optional strAdjunto As String)
     Dim olApp As Object
     Dim objMail As Object
     Dim aArchivosAdjuntos() As String
    
    On Error Resume Next 'Keep going if there is an error
    
     Set olApp = GetObject(, "Outlook.Application") 'See if Outlook is open
    
     If Err Then 'Outlook is not open
     Set olApp = CreateObject("Outlook.Application") 'Create a new instance of Outlook
     End If
    
     'Create e-mail item
     Set objMail = olApp.CreateItem(olMailItem)
    
     With objMail
    
     'Set body format to HTML
     .BodyFormat = olFormatHTML
     .To = strEmail
     .Subject = strAsunto
     .HTMLBody = srtCuerpoEmail
     If Not IsNull(strAdjunto) Then
            aArchivosAdjuntos = Split(strAdjunto, ";")
            For i = LBound(aArchivosAdjuntos) To UBound(aArchivosAdjuntos)
                .Attachments.Add aArchivosAdjuntos(i), olByValue, , "My Displayname"
            Next i
     End If
     .Send
    
     End With
     
    Set olApp = Nothing
    Set objMail = Nothing
    
     MsgBox "Email enviado con éxito"
     
     End Sub
    It works fine, but when I send many emails, by the third or forth mail, it crashes. Access stays blocked and Outlook remains closing for a long time. Finally I always have to reset.

    Could it be that I try run the instance and it crashes because the object is still closing? How can I avoid this crash??

    Should I use "do events" while busy?

    I am running Windows Vista in "that" computer.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    It's a shot in the dark, but I would try adding

    DoEvents

    after the .Send line.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    mercapto is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jan 2012
    Location
    Spain
    Posts
    91
    Sorry for not giving feedback before, I didn't have the chance to send many mails.

    It worked.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    Glad it worked for you.

    Spent an enjoyable week or so in Spain a couple of years ago. Beautiful country!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Sending Emails and VBA coding
    By lucy1216 in forum Programming
    Replies: 4
    Last Post: 05-20-2013, 05:57 AM
  2. Replies: 1
    Last Post: 11-14-2012, 01:43 PM
  3. Sending emails from Acess Database
    By Lirizarry in forum Access
    Replies: 3
    Last Post: 01-26-2012, 10:04 PM
  4. Sending data from form to multiple emails
    By GeorgeB in forum Access
    Replies: 11
    Last Post: 01-09-2012, 04:23 PM
  5. Sending multiple emails
    By Petefured in forum Programming
    Replies: 0
    Last Post: 05-24-2011, 03:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums