为什么使用SentOnBehalfOfName时发件人字段未更新?

时间:2019-02-18 13:49:28

标签: outlook outlook-vba

我已经制作了一个VBA脚本来处理我们的支持邮件,因此,如果接收者是支持帐户,则所有回复都将从支持帐户发送出去。不让Outlook处理此问题的原因是,我们处理了来自ERP系统的所有支持邮件,该系统将传入邮件存储为.eml文件。

因此,这里的窍门是使用事件Inspectors_NewInspector,因为从ERP系统中打开所有邮件时,它们都是在单独的窗口(检查器)中打开的。

先决条件

  1. 已将所有支持用户委派给SendOnBehalfOf访问 Exchange Server中的支持帐户。
  2. 所有支持用户都已添加Microsoft VBScript正则表达式5.5作为VBA中的参考
  3. 从ERP系统生成的所有新电子邮件在“主题”字段中都有唯一的值。

由于所有答复,ReplyAll和Forward也都会生成一个新的检查器,如果这是检查器调用的源,我们将跳过该代码。 我在模块ThisOutlookSession中输入所有代码。

该代码似乎运行良好,但有一个主要烦人之处:

如果我们以主题中正确的值开始新电子邮件,则将“发件人”字段设置为支持帐户。

然后我们回复邮件,并且“发件人”字段将按预期设置为支持帐户。

如果我们现在开始发送新电子邮件,有时会在“发件人:-”字段中以默认帐户显示,但是如果我们在不更改“发件人:-”字段中发送邮件的情况下,发件人就是我们收到电子邮件时的支持帐户

有人知道为什么会这样吗?

更新: 似乎没有触发“错误”的特殊命令。它似乎只是随机出现。

我创建了一个带有邮件链接的Word文档,其主题为“ GLB123456#”,它会触发新邮件场景,并使用本地邮件文件测试回复和转发事件。

我将完整的代码发布在这里,因为我怀疑这可能不是Outlook中的缺陷,而是我的代码:

Option Compare Text
Public WithEvents ChangeAcctMsg As MailItem
Public WithEvents myInspectors As Outlook.Inspectors
Public WithEvents myObj As MailItem
Public theSupportMail As String
Public ReplyAllForward As Boolean
Private Sub Application_Startup() 'initializing variables
    On Error Resume Next
    ReplyAllForward = False
    theSupportMail = "testsupport@domain.com"  'this MUST be changed to an account for which you have been delegated sendOnBehalfOf-rights
    Set myInspectors = Application.Inspectors     'setting up an eventlistener for inspectors
End Sub
Private Sub Application_Quit()
    On Error Resume Next
    'Clean up all objects used by this macro
    Set myInspectors = Nothing
    Set ChangeAcctMsg = Nothing
    Set myObj = Nothing
End Sub
'Event handlers for Reply, Reply All and Forward
Private Sub ChangeAcctMsg_Reply(ByVal Response As Object, Cancel As Boolean)
    On Error Resume Next
    Dim i As Integer
    ReplyAllForward = True
    If ChangeAcctMsg.To Like "*Testsupport*" Then
        Response.SentOnBehalfOfName = theSupportMail
    End If
End Sub
Private Sub ChangeAcctMsg_ReplyAll(ByVal Response As Object, Cancel As Boolean)
    On Error Resume Next
    ReplyAllForward = True
    If ChangeAcctMsg.To Like "*Testsupport*" Then
        Response.SentOnBehalfOfName = theSupportMail
    End If
     For i = 1 To Response.Recipients.Count
        If Response.Recipients(i).Name Like "*Testsupport*" Then
            Response.Recipients.Remove (i)
        End If
    Next
End Sub
Private Sub ChangeAcctMsg_Forward(ByVal Forward As Object, Cancel As Boolean)
    On Error Resume Next
    ReplyAllForward = True
    If ChangeAcctMsg.To Like "*Testsupport*" Then
        Forward.SentOnBehalfOfName = theSupportMail
    End If
End Sub
Private Sub myInspectors_NewInspector(ByVal Inspector As Outlook.Inspector)
    On Error Resume Next
                                'test for whether the inspector was initiated from a reply or forward
    If Not ReplyAllForward Then 'Run the code below UNLESS the new window came from a Reply, Reply All or Forward
        Set myObj = Inspector.CurrentItem
        If TypeName(myObj) = "MailItem" Then 'Do nothing UNLESS the item in the inspector is a MailItem
            Dim regEx As New RegExp          'Set up a regular expression object to hold the unique subject value
            Dim theRecipients As Recipients
            Dim toSupport As Boolean         'preparing the variable used to test which account the original mail was sent to
            toSupport = False
            With regEx
                .Pattern = "^#GLB[0-9]{6,8} #" 'this pattern is supposed to match strings that start with #GLB followed by 6 to 8 digits between 0-9, a space and #
                .IgnoreCase = True ' because it should ignore case when testing i.e. GLB=glb
            End With
            Set theRecipients = myObj.Recipients  'check to see if support was one of the receipients
            For Each Recipient In theRecipients
                If Recipient.Name Like "*Testsupport*" Then
                    toSupport = True
                End If
            Next
            If toSupport Then
                Set ChangeAcctMsg = myObj 'create object with event-listener only if the mail was sent to support
            Else                          'ok what if the mail was NOT sent to support
                If regEx.Test(myObj.Subject) Then 'testing the Subject of myObj against the pattern from above.
                    myObj.SentOnBehalfOfName = theSupportMail ' if there's a match, i.e. a new e-mail that should be sent from support.
                End If
            End If
            Set regEx = Nothing           'cleaning up variables
            Set theRecipients = Nothing
        End If
    Else
        ReplyAllForward = False 'If the new window was generated by the Reply, Reply All or Forward event then ReplyAllForward was set to true.
                                'Thus we set the ReplyAllForward variable back to False
    End If
End Sub

0 个答案:

没有答案