VBA Stuck in Loop

NascarBaritone

Registered User.
Local time
Today, 03:48
Joined
Sep 23, 2008
Messages
75
I have some code that occurs on the click on a button. It outputs a couple of reports and tables and then uses the Lotus Notes SendNotesMail sub to send an email through Lotus Notes. 9 times out of 10 everything works perfectly fine. However, on two occasions I have click the button and everything starts out fine, but the SendNotesMail seems to get stuck in a loop and sends email after email (last time I got almost 1,000 copies of the same email). The only way to kill it was to force an End Process on Access. Could someone take a look at my code and see if they can spot something that might have caused this in those two instances?

Button Code:
Code:
Private Sub Command202_Click()
Dim strFileName As String, strFileNameClient As String, strFileNameLetter As String, strFileNameAudit As String
strFileName = CurrentProject.Path & "\Audit Responses.xlsx"
strFileNameClient = CurrentProject.Path & "\Client File Responses.xlsx"
strFileNameLetter = CurrentProject.Path & "\" & Me.Branch_Number & " " & Me.A_FirstName & " " & Me.A_LastName & " 2011 Audit Exit Letter.pdf"
strFileNameAudit = CurrentProject.Path & "\" & Me.Branch_Number & " " & Me.A_FirstName & " " & Me.A_LastName & " 2011 Audit.pdf"
If IsNull(Me.Branch_Number.Value) = True Or Me.Branch_Number.Value = "" Then
MsgBox "Please enter a branch number", vbInformation + vbOKOnly, "Branch Number Required"
Me.Branch_Number.SetFocus
Exit Sub
Else
If MsgBox("This will finalize your audit, create an exit letter, and email audit results/attachments to the Home Office." _
        & vbCrLf & vbCrLf & "Are you sure you want to continue?", vbExclamation + vbYesNo, "Finalize Audit") = vbYes Then
DoCmd.OpenForm "frmAttachments", acNormal, , , , acHidden
DoCmd.OpenReport "rptPrint", acViewReport, , , acHidden
DoCmd.OutputTo acOutputReport, "rptPrint", acFormatPDF, strFileNameAudit
DoCmd.OutputTo acOutputReport, "rptExitLetter", acFormatPDF, strFileNameLetter
DoCmd.OutputTo acOutputTable, "tblAuditResponses", acFormatXLSX, strFileName
DoCmd.OutputTo acOutputTable, "tblClientFileResponses", acFormatXLSX, strFileNameClient
Call SendNotesMail(Me.A_FirstName & Me.A_LastName & "2011 Branch Office Audit", Forms!frmAttachments!subfrmAttachments!Attachment, "[EMAIL="me@myemail.com"]me@myemail.com[/EMAIL]", "", True)
MsgBox "Audit results have been sent to [EMAIL="me@myemail.com"][COLOR=#0000ff]me@myemail.com[/COLOR][/EMAIL]", vbInformation + vbOKOnly, "Audit Results Sent"
Kill strFileName
Kill strFileNameClient
DoCmd.Close acForm, "frmAttachments"
DoCmd.Close acReport, "rptPrint"
Else
Exit Sub
End If
End If
End Sub

SendNotesMail code:
Code:
Option Compare Database
Option Explicit
'Public Sub SendNotesMail(Subject as string, attachment as string,
'recipient as string, bodytext as string,saveit as Boolean)
'This public sub will send a mail and attachment if neccessary to the
'recipient including the body text.
'Requires that notes client is installed on the system.
Public Sub SendNotesMail(Subject As String, Attachment As String, Recipient As String, BodyText As String, SaveIt As Boolean)
'Set up the objects required for Automation into lotus notes
    Dim Maildb As Object 'The mail database
    Dim UserName As String 'The current users notes name
    Dim MailDbName As String 'THe current users notes mail database name
    Dim MailDoc As Object 'The mail document itself
    Dim AttachME As Object 'The attachment richtextfile object
    Dim Session As Object 'The notes session
    Dim EmbedObj As Object 'The embedded object (Attachment)
    Dim strFileNameAudit As String, strFileNameLetter As String
    strFileNameAudit = CurrentProject.Path & "\" & Forms!frmAuditpg5!Branch_Number & " " & Forms!frmAuditpg5!A_FirstName & " " & Forms!frmAuditpg5!A_LastName & " 2011 Audit.pdf"
    strFileNameLetter = CurrentProject.Path & "\" & Forms!frmAuditpg5!Branch_Number & " " & Forms!frmAuditpg5!A_FirstName & " " & Forms!frmAuditpg5!A_LastName & " 2011 Audit Exit Letter.pdf"
    'Start a session to notes
    Set Session = CreateObject("Notes.NotesSession")
    'Get the sessions username and then calculate the mail file name
    'You may or may not need this as for MailDBname with some systems you
    'can pass an empty string or using above password you can use other mailboxes.
    UserName = Session.UserName
    MailDbName = Left$(UserName, 1) & Right$(UserName, (Len(UserName) - InStr(1, UserName, " "))) & ".nsf"
    'Open the mail database in notes
    Set Maildb = Session.GETDATABASE("", MailDbName)
     If Maildb.ISOPEN = True Then
          'Already open for mail
     Else
         Maildb.OPENMAIL
     End If
    'Set up the new mail document
    Set MailDoc = Maildb.CREATEDOCUMENT
    MailDoc.Form = "Memo"
    MailDoc.sendto = Recipient
    MailDoc.Subject = Subject
    MailDoc.Body = BodyText
    MailDoc.SAVEMESSAGEONSEND = SaveIt
    'Set up the embedded object and attachment and attach it
On Error GoTo ErrHandler:
    If Attachment <> "" Then
 
        Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment1")
        Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", Forms!frmAttachments!subfrmAttachments!Attachment, "Attachment")
Label1:
On Error GoTo ErrHandler2:
        Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment2")
        Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", Forms!frmAttachments!subfrmAttachments!Attachment1, "Attachment")
Label2:
On Error GoTo ErrHandler3:
        Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment3")
        Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", Forms!frmAttachments!subfrmAttachments!Attachment2, "Attachment")
Label3:
On Error GoTo ErrHandler4:
        Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment4")
        Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", Forms!frmAttachments!subfrmAttachments!Attachment3, "Attachment")
Label4:
On Error GoTo ErrHandler5:
        Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment5")
        Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", Forms!frmAttachments!subfrmAttachments!Attachment4, "Attachment")
Label5:
On Error GoTo ErrHandler6:
        Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment6")
        Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", Forms!frmAttachments!subfrmAttachments!Attachment5, "Attachment")
Label6:
On Error GoTo ErrHandler7:
        Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment7")
        Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", Forms!frmAttachments!subfrmAttachments!Attachment6, "Attachment")
Label7:
On Error GoTo ErrHandler8:
        Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment8")
        Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", Forms!frmAttachments!subfrmAttachments!Attachment7, "Attachment")
Label8:
On Error GoTo ErrHandler9:
        Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment9")
        Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", Forms!frmAttachments!subfrmAttachments!Attachment8, "Attachment")
Label9:
On Error GoTo ErrHandler10:
        Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment10")
        Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", Forms!frmAttachments!subfrmAttachments!Attachment9, "Attachment")
Label10:
On Error GoTo ErrHandler11:
        Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment11")
        Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", Forms!frmAttachments!subfrmAttachments!Attachment10, "Attachment")
Label11:
On Error GoTo ErrHandler12:
        Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment12")
        Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", CurrentProject.Path & "\Client File Responses.xlsx", "Attachment")
Label12:
On Error GoTo ErrHandler13:
        Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment13")
        Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", strFileNameAudit, "Attachment")
Label13:
On Error GoTo ErrHandler14:
        Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment14")
        Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", strFileNameLetter, "Attachment")
ErrHandler:
    Resume Label1:
ErrHandler2:
    Resume Label2:
ErrHandler3:
    Resume Label3:
ErrHandler4:
    Resume Label4:
ErrHandler5:
    Resume Label5:
ErrHandler6:
    Resume Label6:
ErrHandler7:
    Resume Label7:
ErrHandler8:
    Resume Label8:
ErrHandler9:
    Resume Label9:
ErrHandler10:
    Resume Label10:
ErrHandler11:
    Resume Label11:
ErrHandler12:
    Resume Label12:
ErrHandler13:
    Resume Label13:
ErrHandler14:
    Resume Label14:
Label14:
    End If
    'Send the document
    MailDoc.PostedDate = Now() 'Gets the mail to appear in the sent items folder
    MailDoc.SEND 0, Recipient
    'Clean Up
    Set Maildb = Nothing
    Set MailDoc = Nothing
    Set AttachME = Nothing
    Set Session = Nothing
    Set EmbedObj = Nothing
End Sub
 
The loop will occur on a error in the part after label14 as the errorhandler stil jumps to the ErrHandler14 label.
As jou don't use the errorcode why not simply use "on error resume next" for the part where jou have the 14 error handlers.
 

Users who are viewing this thread

Back
Top Bottom