I have VBA code in Excel to perform the following actions:
- retrieve order request
- pull SAP reports
- validate order request
- connect to SAP to do transaction
- send email
- loop (from #4) until all orders are done
The sending email part crashes probably 15% of the time on replying to the second email. I can continue the automation flow by acknowledging the below error, restart Outlook, and the script continues like nothing happened.
I'm thinking that it might be a memory issue with this particular bot as it's only this one that fails. I do comprehend that after the the code hits End Sub, then all the variables should be cleared from memory.
The code is only for replying. It gets called after the SAP transaction is done.
Sub EmailReply()
Application.ScreenUpdating = False
Call OpeningDuties
Dim olApp As Outlook.Application
Dim oLNameSpace As Outlook.Namespace
Dim objOwner As Outlook.Recipient
Dim topOlFolder As Outlook.MAPIFolder
Dim oLMail As Outlook.MailItem
Dim i As Long
Dim wdDoc As Word.Document
Dim EmailAddress As Object
Dim fdr_Unprocessed As Outlook.MAPIFolder
Dim fdr_Pending As Outlook.MAPIFolder
Dim fdr_Processed As Outlook.MAPIFolder
Set myNameSpace = Outlook.Application.GetNamespace("mapi")
Set objOwner = myNameSpace.CreateRecipient("retailrma@company.com")
objOwner.Resolve
If objOwner.Resolved Then
Set topOlFolder = myNameSpace.GetSharedDefaultFolder(objOwner, olFolderInbox)
End If
Set fdr_Unprocessed = topOlFolder.Folders("RMA - Unprocessed")
Set fdr_Pending = topOlFolder.Folders("RMA - Pending")
Set fdr_Processed = topOlFolder.Folders("RMA - Processed")
For Each oLMail In fdr_Unprocessed.Items
If (oLMail.Subject = Range("Email_Subject").Text And Format(oLMail.ReceivedTime, "Medium Time") = Format(Range("Email_Date").Text, "Medium Time") And oLMail.SenderEmailAddress = Range("Email_Address").Text) _
Or (oLMail.Subject = Range("Email_Subject").Text And Format(oLMail.ReceivedTime, "Medium Time") = Format(Range("Email_Date").Text, "Medium Time")) Then
'if email can be found then reply email or send email
'Define copy range on Email Template sheet as a word document
Dim CopyRange As Range
'Set wdDoc = oLMail.GetInspector.WordEditor
'Determining if the email should be responded in English or French
If Range("email_language") = "En" Then
FirstRow = 3
FirstColumn = 3
LastRow = 246
LastColumn = 9
ElseIf Range("email_language") = "Fr" Then
FirstRow = 3
FirstColumn = 11
LastRow = 246
LastColumn = 16
End If
Sheets("Email Template").Select
Sheets("Email Template").Range(Cells(FirstRow, FirstColumn), Cells(LastRow, LastColumn)).AutoFilter Field:=1, Criteria1:="Show"
Set ReplyAll = oLMail.ReplyAll
Set EmailAddress = Range("Email_Address")
Set CopyRange = Range(Cells(FirstRow, FirstColumn), Cells(LastRow, LastColumn)).SpecialCells(xlCellTypeVisible)
'Error handling if no email address
If EmailAddress = 0 Then
RMAStatus = "Non valid email address"
Application.ScreenUpdating = True
Exit Sub
End If
With ReplyAll
.To = EmailAddress
.CC = "retailrma@company.com"
.Display
.BodyFormat = olFormatHTML
Set wdDoc = oLMail.GetInspector.WordEditor
CopyRange.Copy
wdDoc.Application.Selection.PasteAndFormat Type:=wdFormatOriginalFormatting 'pastes the approved / non approved IMEIs into outlook reply email
.Send
End With
'move email to processed folder
oLMail.Move fdr_Processed
'Resets Email Template
Sheets("Email Template").Range(Cells(FirstRow, FirstColumn), Cells(LastRow, LastColumn)).AutoFilter Field:=1
GoTo ExitSendEmail
End If
Next oLMail
ExitSendEmail:
Application.ScreenUpdating = True
End Sub
