【问题标题】:Is it possible to identify through OUTLOOK triggers/events to which shared mail box has received a new email?是否可以通过 OUTLOOK 触发器/事件识别共享邮箱收到了新电子邮件?
【发布时间】:2021-01-07 11:59:37
【问题描述】:

我们正在尝试将新的邮件项目组件存储到 excel 中并分配 tkt id,已尝试使用单个共享邮箱进行此操作并成功,但我们希望为 20 个共享邮箱实现相同的操作。新电子邮件到达 20 个共享邮箱之一时,Outlook vba 事件/触发器如何识别。

这是仅适用于默认收件箱的代码:

Private Sub inboxItems_ItemAdd(ByVal Item As Object)

Dim Msg As Outlook.MailItem
Dim MessageInfo
Dim Result
If TypeName(Item) = "MailItem" Then
        Dim cn As Object
        Dim sCon As String
        Dim sSQL As String
        Dim bytHasAttachment As String
        Dim strAddress As String
        Dim objSender, exUser

        Dim olRecipient As Outlook.Recipient
        Dim strToEmails, strCcEmails, strBCcEmails As String

        For Each olRecipient In Item.Recipients
            Dim mail As String
            If olRecipient.AddressEntry Is Nothing Then
                    mail = olRecipient.Address
            ElseIf olRecipient.AddressEntry.GetExchangeUser Is Nothing Then
                    mail = olRecipient.Address
            Else
                    mail = olRecipient.AddressEntry.GetExchangeUser.PrimarySmtpAddress
            End If
    
            If olRecipient.Type = Outlook.OlMailRecipientType.olTo Then
                    strToEmails = strToEmails + mail & ";"
            ElseIf olRecipient.Type = Outlook.OlMailRecipientType.olCC Then
                    strCcEmails = strCcEmails + mail & ";"
            ElseIf olRecipient.Type = Outlook.OlMailRecipientType.olBCC Then
                    strBCcEmails = strBCcEmails + mail & ";"
            End If
        Next

        With Item
            If Item.Attachments.Count > 0 Then
                    bytHasAttachment = 1
            Else
                    bytHasAttachment = 0
            End If
        End With

    'On Error Resume Next 'PropertyAccessor can raise an exception if a property is not found
        If Item.SenderEmailType = "SMTP" Then
            strAddress = Item.SenderEmailAddress
        Else
            'read PidTagSenderSmtpAddress
        strAddress = Item.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001F")
            If Len(strAddress) = 0 Then
                Set objSender = Item.Sender
                If Not (objSender Is Nothing) Then
                'read PR_SMTP_ADDRESS_W
                    strAddress = objSender.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x39FE001F")
                    If Len(strAddress) = 0 Then
                            Set exUser = objSender.GetExchangeUser
                            If Not (exUser Is Nothing) Then
                                strAddress = exUser.PrimarySmtpAddress
                            End If
                    End If
                End If
            End If
        End If

    On Error GoTo ErrorHandler

    Set cn = CreateObject("ADODB.Connection")
    sCon = "Driver=MySQL ODBC 8.0 ANSI Driver;SERVER=localhost;UID=root;PWD={Platinum@123};DATABASE=liva_dev_gm;PORT=3306;COLUMN_SIZE_S32=1;DFLT_BIGINT_BIND_STR=1"
    cn.Open sCon

    sSQL = "INSERT INTO tbl_gmna_emailmaster_inbox (eMail_Icon, eMail_MessageID, eMail_Folder, eMail_Act_Subject, eMail_From, eMail_TO, eMail_CC, " & _
       "eMail_BCC, eMail_Body, eMail_DateReceived, eMail_TimeReceived, eMail_Anti_Post_Meridiem, eMail_Importance, eMail_HasAttachment) " & _
       "VALUES (""" & Item.MessageClass & """, " & _
       """" & Item.EntryID & """, " & _
       """Inbox""" & ", " & _
       """" & Item.Subject & """, " & _
       """" & strAddress & """, " & _
       """" & strToEmails & """, " & _
       """" & strCcEmails & """, " & _
       """" & strBCcEmails & """, " & _
       """" & Item.Body & """, " & "'" & Format(Item.ReceivedTime, "YYYY-MM-DD") & "', " & "'" & Format(Item.ReceivedTime, "hh:mm:ss") & "', " & "'" & Format(Item.ReceivedTime, "am/pm") & "', " & "'" & Item.Importance & "', " & "'" & bytHasAttachment & "')"
    cn.Execute sSQL
End If
ExitNewItem:
    bytHasAttachment = ""
    Exit Sub
ErrorHandler:
        MsgBox Err.Number & " - " & Err.Description
        Resume ExitNewItem
End Sub

【问题讨论】:

  • 相关代码在Application_Startup 中,您指明与inboxItems 关联的文件夹。
  • 所以这意味着我应该在 Application_Startup 中提及/声明 20 个 shared_inboxitems ?
  • 是 20 在启动中。调用Private Sub inboxItems1_ItemAdd(ByVal Item As Object) ... Private Sub inboxItems20_ItemAdd(ByVal Item As Object每个帖子中的代码,不要重复。
  • 我试过了,但我有点困惑,请你举个例子。并通知 brettdj 提供的用于处理不同邮箱的子链接无效。

标签: vba outlook


【解决方案1】:

如果 20 个共享邮箱在导航窗格中。

Option Explicit

Private WithEvents inboxItms As Items

Private WithEvents sharedInboxItms1 As Items
' ...
Private WithEvents sharedInboxItms20 As Items


Private Sub Application_Startup()

    Dim defaultInbox As Folder

    Dim sharedMailbox1 As Folder
    Dim sharedInbox1 As Folder
    ' ...
    Dim sharedMailbox20 As Folder
    Dim sharedInbox20 As Folder

    Set defaultInbox = Session.GetDefaultFolder(olFolderInbox)
    Set inboxItms = defaultInbox.Items

    Set sharedMailbox1 = Session.Folders("SharedMailbox1@somewhere.com")
    Set sharedInbox1 = sharedMailbox1.Folders("Inbox")

    ' typo fixed
    'Set sharedInboxItms1 = sharedInbox1.Folders("Inbox").Items
    Set sharedInboxItms1 = sharedInbox1.Items
    ' ...
    Set sharedMailbox20 = Session.Folders("SharedMailbox20@somewhere.com")
    Set sharedInbox20 = sharedMailbox20.Folders("Inbox")

    ' typo fixed
    'Set sharedInboxItms20 = sharedInbox20.Folders("Inbox").Items
    Set sharedInboxItms20 = sharedInbox20.Items

End Sub


Private Sub inboxItms_ItemAdd(ByVal Item As Object)
   ' current code for default inbox
End Sub

Private Sub sharedInboxItms1_ItemAdd(ByVal Item As Object)
    inboxItms_ItemAdd Item
End Sub

' ...

Private Sub sharedInboxItms20_ItemAdd(ByVal Item As Object)
     inboxItms_ItemAdd Item
End Sub

【讨论】:

  • 您好 Niton,感谢您的回复,非常感谢您延迟回复。我尝试了上面的代码,得到的错误是“运行时错误'-21472212333 (800401f)'”,代码行是“Set sharedInboxItms1 = sharedInbox1.Folders("Inbox").Items" in Application_Startup sub。因此我已更改为 Set sharedInboxItms1 = sharedInbox1.Items 并开始工作。以及对此的扩展,我正在使用 Sharemailbox 发送的项目也具有相同的概念。请你给点提示一下这个概念。
  • 要将相同的概念应用于已发送文件夹,请参阅 olFolderSentMail 并将“收件箱”更改为您在已发送文件夹中看到的文本。
  • 嗨 niton,感谢您让我理解了这个概念,并且代码已经部署,并且只有在 sharedmailbox 处于活动状态时它才能工作,否则它不会触发事件。例如,如果活动会话与 sharedmailbox1 一起,则不会为 sharedmailbox2 触发事件。我现在很困惑如何触发所有共享邮箱(20)的事件,即使会话处于非活动状态。我使用的是 Outlook 2016 版本。
  • 如果这些还不是配置文件中的 20 个附加帐户。我建议您可以创建另一个配置文件。添加一些作为帐户。 support.microsoft.com/en-us/office/…。第二个配置文件看起来与您现在拥有的配置文件相同,但会有一些不同的功能。 ItemAdd 行为可能会改变。如果没有,请发布有关此问题的另一个问题。
  • 谢谢您,先生,个人资料中已经有 20 个额外的帐户。但不触发。无论如何,如果不是,我会在周末尝试更多挖掘,我会按照你的建议发布其他问题。非常感谢你,我从你的代码中学到了很多......:)
猜你喜欢
  • 1970-01-01
  • 2020-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-20
  • 1970-01-01
相关资源
最近更新 更多