【问题标题】:Excel macro, triggered via task scheduler, running on a virtual machine crashes OutlookExcel 宏,通过任务计划程序触发,在虚拟机上运行会导致 Outlook 崩溃
【发布时间】: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


【解决方案1】:

您只能在用户帐户下自动执行 Outlook,即在有人参与的环境下(登录后)。以下是 MS 的声明:

Microsoft 目前不推荐也不支持任何无人值守、非交互式客户端应用程序或组件(包括 ASP、ASP.NET、DCOM 和 NT 服务)的 Microsoft Office 应用程序自动化,因为 Office 可能表现出不稳定Office 在此环境中运行时出现的行为和/或死锁。

如果您要构建在服务器端上下文中运行的解决方案,您应该尝试使用已确保无人值守执行安全的组件。或者,您应该尝试找到允许至少部分代码在客户端运行的替代方案。如果您使用服务器端解决方案中的 Office 应用程序,该应用程序将缺少许多成功运行所需的功能。此外,您将在整体解决方案的稳定性方面承担风险。

Considerations for server-side Automation of Office 文章中了解更多信息。

作为一种可能的解决方法,您可以考虑使用 EWS 或 Graph API。请参阅Explore the EWS Managed API, EWS, and web services in Exchange 了解更多信息。

【讨论】:

  • 我不确定这是否有帮助,因为问题不在于服务器,该代码适用于任何其他电子邮件 BOT,但仅适用于这个特定的机器人。
猜你喜欢
  • 1970-01-01
  • 2020-01-28
  • 1970-01-01
  • 2020-05-19
  • 2022-10-13
  • 2014-07-30
  • 1970-01-01
  • 2012-10-23
  • 2023-03-29
相关资源
最近更新 更多