Hi guys,
I've made leaps and bounds with my current little project today, but there's a frustrating error which is keeping me from testing my procedure.
I have a form which contains 2 controls: a button "btnMailIt", and a subform, "qryMessagesMerge subform".
MsgBodyText is a text field in the subform.
I have some code to send report snapshots in a loop to names in a list. I decided to have all the info for the SendObject method in this subform for simplicity's sake, thus other subform fields include MsgSubject and EmailAddress. I wasn't sure it was possible to have merged fields within the MessageBody, so for each record, there is a separate MessageBody record customised to suit the recipient.
My problem is this: when the code tries to establish the MessageBody field, it produces an error. Below is the code up to and including this error. I know a version of the code worked previously, when the message details (To, Subj, Body etc) were stored in a separate table, so I wondered if it was an issue with subforms? I'm suspicious, though, because the code opens the subform in a new window anyway.
Also, and this might be a way of diagnosing more effectively, I'd like to implement more detailed error reporting. You'll see that currently all error numbers produce the same message: MsgBox "ERROR: Message body text field " + MsgBodyTextField + " does not exist or is empty." I want to put in lines e.g.
But where can I find a list of error numbers and messages for SendObject?
Thanks for your patience!
-Abby
I've made leaps and bounds with my current little project today, but there's a frustrating error which is keeping me from testing my procedure.
I have a form which contains 2 controls: a button "btnMailIt", and a subform, "qryMessagesMerge subform".
MsgBodyText is a text field in the subform.
I have some code to send report snapshots in a loop to names in a list. I decided to have all the info for the SendObject method in this subform for simplicity's sake, thus other subform fields include MsgSubject and EmailAddress. I wasn't sure it was possible to have merged fields within the MessageBody, so for each record, there is a separate MessageBody record customised to suit the recipient.
My problem is this: when the code tries to establish the MessageBody field, it produces an error. Below is the code up to and including this error. I know a version of the code worked previously, when the message details (To, Subj, Body etc) were stored in a separate table, so I wondered if it was an issue with subforms? I'm suspicious, though, because the code opens the subform in a new window anyway.
Also, and this might be a way of diagnosing more effectively, I'd like to implement more detailed error reporting. You'll see that currently all error numbers produce the same message: MsgBox "ERROR: Message body text field " + MsgBodyTextField + " does not exist or is empty." I want to put in lines e.g.
Code:
If Err.Number = 1 Then 'do this
If Err.Number = 2 Then 'do that etc.
But where can I find a list of error numbers and messages for SendObject?
Thanks for your patience!
-Abby
Code:
Option Compare Database
Option Explicit
Const MsgBodyTextField As String = "MsgBodyText" ' Name of form field in message table containing message text
Const MsgSubjectField As String = "MsgSubject" ' Name of form field in message table containing message subject
Const RecordsFormName As String = "qryMessagesMerge subform" ' Name of form that displays table of records which will be used in mail merge
Const EmailAddressField As String = "EmailAddress" ' Name of table field in mail merge record table that contains email addresses
' Name of error log file.
Const ErrorLogFile As String = "S:\Sales\MDB\Prospects Email Merge Logs\MailMergeErrorLog.txt"
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Const ETYPE As Integer = 0 ' Index of element in MsgElem array that indicates element type - field name or chunk of literal text
Const ETEXT As Integer = 1 ' Index of other element in MsgElem array. Contains actual field name or chunk of literal text
Const FIELDNAME As Boolean = True ' Code goes in ETYPE element to indicate text represents a field name
Const MESSAGECHUNK As Boolean = False ' Code goes in ETYPE element to indicate text is literal message text
Const FAILURE As Boolean = False ' General failure return code
Const SUCCESS As Boolean = True ' General success return code
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Code executed when you click the Mail It button
Private Sub btnMailIt_Click()
Dim MsgElem() As Variant
Dim strMsgToSend As String, _
strT As String
Dim rst As Object
Dim fld As Field
Dim obj As AccessObject
Dim ctl As Control
Dim bFoundForm As Boolean, _
bWasClosed As Boolean, _
bSendError As Boolean, _
bHadErrors As Boolean
' Ensure form data is in sync with underlying table
Me.Refresh
'Ensure the form that contains the mail merge records exists. Open it if necessary
bFoundForm = False
bWasClosed = False
For Each obj In Application.CurrentProject.AllForms
If obj.Name = RecordsFormName Then
bFoundForm = True
If obj.IsLoaded = False Then
DoCmd.OpenForm obj.Name, acFormDS
bWasClosed = True
DoEvents
Exit For
End If
End If
Next
If Not bFoundForm Then
MsgBox "Cannot find form " + RecordsFormName + ". Aborting"
Exit Sub
End If
' Ensure all required fields exist
On Error Resume Next
' Message body text field. Content is further validated in GetMessageText function
strT = Me.Controls(MsgBodyTextField)
If Err.Number <> 0 Then
MsgBox "ERROR: Message body text field " + MsgBodyTextField + " does not exist or is empty."
If bWasClosed Then DoCmd.Close acForm, obj.Name, acSaveNo
Exit Sub
End If