【发布时间】:2021-10-01 20:47:53
【问题描述】:
代码的主要工作是将 Excel 中的一个部分复制粘贴到 Outlook 2016 中的电子邮件正文中,并将其发送到预定义的分发者列表。
问题是 Outlook“没有响应”并关闭,或者代码打印“错误消息”并且在我手动关闭并重新打开 Outlook 并恢复代码之前不允许代码运行。
代码在 24/7 的虚拟机 (VM) 上运行。仅当我未登录 VM 时才会出现此问题。
当机器人通过任务调度器触发时,代码会自动启动。
Sub EmailReply()
Application.ScreenUpdating = False
Call OpeningDuties
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim OutNameSpace As Outlook.Namespace
Dim OutOwner As Outlook.Recipient
Dim EmailAddress As Object
Dim i As Long
' The error usually happens at this part of the code:
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
Set OutNameSpace = OutApp.GetNamespace("mapi")
Set OutOwner = OutNameSpace.CreateRecipient("company@company.com")
OutOwner.Resolve
' the rest of the code:
Dim CopyRange As Range
Set wdDoc = OutMail.GetInspector.WordEditor
'Assign email title
SubjectText = "COMPANY RMA Results"
'Retrieve email address
Set EmailAddress = Range("Email_Address")
If EmailAddress = 0 Then
RMAStatus = "Non valid email address"
Application.ScreenUpdating = True
Exit Sub
End If
'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
'Filter template for correct email response
Sheets("Email Template").Select
Sheets("Email Template").Range(Cells(FirstRow, FirstColumn), Cells(LastRow, LastColumn)).AutoFilter Field:=1, Criteria1:="Show"
'Defines Range for Range
Sheets("Email Template").Select
Set CopyRange = Sheets("Email Template").Range(Cells(FirstRow, FirstColumn), Cells(LastRow, LastColumn)).SpecialCells(xlCellTypeVisible)
With OutMail
.To = EmailAddress
.CC = "RMA@company.com"
.SentOnBehalfOfName = "RMA@company.com"
.Subject = SubjectText
.Display
'Creating Email Summary Report
Workbooks(BOT_Filename).Activate
CopyRange.CopywdDoc.Application.Selection.PasteAndFormat Type:=wdFormatOriginalFormatting
'pastes the Text range
.Send
End With
On Error GoTo ExitSendEmail
ExitSendEmail:
Set CopyRange = Nothing
Set OutApp = Nothing
Set OutMail = Nothing
Set OutNameSpace = Nothing
Set OutOwner = Nothing
Application.ScreenUpdating = True
End Sub
【问题讨论】:
-
你是怎么开始的?
-
@Sam 当机器人通过任务调度器触发时自动启动
-
你有它。下面@Eugene Astafiev 的回答是对您问题的完整解释。如果您使用自动启动来运行它并以交互方式运行它,它可能会毫无问题地工作。
标签: excel vba outlook scheduled-tasks outlook-2016