I am sending an email using SendObject. Sometimes it works, and sometimes it makes the computer freeze up with no error message. I have tried this with Outlook running or not running, seems to make no difference.
I have added some MsgBox () to narrow down where it crashes. It is after 'Five' and Before 'Six'. On the line:
Set appOutlookRecip = .Recipients.Add(eMailAddress)
I am mystified as to why it works OK sometimes, and not others. The email address being used is valid.
Any suggestions?
Robert
Code:
[FONT=Arial]'The sub procedure below sends e-mail in response to a click on the Send button.[/FONT]
[FONT=Arial]Private Sub SendMessagesButton_Click()[/FONT]
[FONT=Arial] [/FONT]
[FONT=Arial] 'For Access, define some object variables and make connections.[/FONT]
[FONT=Arial] Dim myConnection As ADODB.Connection[/FONT]
[FONT=Arial] Set myConnection = CurrentProject.Connection[/FONT]
[FONT=Arial] Dim myRecordSet As New ADODB.Recordset[/FONT]
[FONT=Arial] myRecordSet.ActiveConnection = myConnection[/FONT]
[FONT=Arial] [/FONT]
[FONT=Arial] 'Define some object variables for Outlook[/FONT]
[FONT=Arial] Dim appOutlook As Outlook.Application 'Refers to Outlook's Application object.[/FONT]
[FONT=Arial] Dim appOutlookMsg As Outlook.MailItem 'Refers to an Outlook e-mail message.[/FONT]
[FONT=Arial] Dim appOutlookRecip As Outlook.Recipient 'Refers to an Outlook e-mail recipient.[/FONT]
[FONT=Arial] [/FONT]
[FONT=Arial] 'General variables.[/FONT]
[FONT=Arial] Dim mySQL As String, eMailAddress As String, whereClause As String[/FONT]
[FONT=Arial] Dim myMsg As String[/FONT]
[FONT=Arial] [/FONT]
[FONT=Arial] 'Create an Outlook session in the background.[/FONT]
[FONT=Arial] Set appOutlook = CreateObject("Outlook.Application")[/FONT]
[FONT=Arial] [/FONT]
[FONT=Arial] 'Get the e-mail address from current record of myRecordset.[/FONT]
[FONT=Arial] eMailAddress = "accounting@sectorsupply.com"[/FONT]
[FONT=Arial] [/FONT]
[FONT=Arial] 'Create a new, empty e-mail message.[/FONT]
[FONT=Arial] Set appOutlookMsg = appOutlook.CreateItem(olMailItem)[/FONT]
[FONT=Arial] [/FONT]
[FONT=Arial] With appOutlookMsg 'Using the new, empty message...[/FONT]
[FONT=Arial] ' Address the new message.[/FONT]
[FONT=Arial] MsgBox ("Five")[/FONT]
[FONT=Arial] Set appOutlookRecip = .Recipients.Add(eMailAddress)[/FONT]
[FONT=Arial] MsgBox ("Six")[/FONT]
[FONT=Arial] appOutlookRecip.Type = olTo 'Sets message to normal outgoing e-mail message.[/FONT]
[FONT=Arial] MsgBox ("Seven")[/FONT]
[FONT=Arial] ' Fill in the Subject line and main body of message.[/FONT]
[FONT=Arial] MsgBox ("Eight")[/FONT]
[FONT=Arial] .Subject = Me.Subject 'Fill in the subject line.[/FONT]
[FONT=Arial] MsgBox ("Nine")[/FONT]
[FONT=Arial] .Body = Me.MessageBody 'Fill in the message body.[/FONT]
[FONT=Arial] MsgBox ("Ten")[/FONT]
[FONT=Arial] [/FONT]
[FONT=Arial] .Send 'Send the completed message.[/FONT]
[FONT=Arial] MsgBox ("Eleven")[/FONT]
[FONT=Arial] End With[/FONT]
[FONT=Arial] [/FONT]
I have added some MsgBox () to narrow down where it crashes. It is after 'Five' and Before 'Six'. On the line:
Set appOutlookRecip = .Recipients.Add(eMailAddress)
I am mystified as to why it works OK sometimes, and not others. The email address being used is valid.
Any suggestions?
Robert