【发布时间】:2021-06-28 13:19:35
【问题描述】:
我在 Excel 中有 VBA 代码来执行以下操作:
- 检索订单请求
- 提取 SAP 报告
- 验证订单请求
- 连接到 SAP 进行事务
- 发送电子邮件
- 循环(从 #4 开始)直到所有订单都完成
在回复第二封电子邮件时,发送电子邮件部分可能有 15% 的时间崩溃。我可以通过确认以下错误来继续自动化流程,重新启动 Outlook,然后脚本继续运行,就像什么都没发生一样。
我认为这可能是这个特定机器人的内存问题,因为只有这个机器人失败了。我确实理解在代码命中End Sub之后,所有变量都应该从内存中清除。
代码仅用于回复。它在 SAP 事务完成后被调用。
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
【问题讨论】:
-
可能不相关,但
Sheets("Email Template").Range(Cells(FirstRow, FirstColumn), Cells(LastRow, LastColumn)).AutoFilter Field:=1是 problematic。您需要为内部Cells调用限定工作表。 -
“Outlook 崩溃”是什么意思?您看到运行时错误?还是 Outlook 应用会死掉?
-
你有不同的变量名
Dim oLNameSpace和Set myNameSpace。由此我推测您没有使用Option Explicit。您应该 --- 它可以提醒您代码中的问题。 -
@xidgel Outlook 应用程序死机,导致 VBA 代码挂起等待下一个操作。这不是 VBA 脚本中的运行时错误。对于这个特定的代码模块,我没有使用 Option Explicit,但我会添加它。我还将查看使用的不同类型的变量,并返回我的发现......