Domanda

E 'possibile in Microsoft Outlook VBA per catturare l'evento Open di qualsiasi elemento di posta che viene aperto? Mi piacerebbe aggiungere un'etichetta categoria a qualsiasi elemento di posta elettronica ho aperto, di avere un'opzione alternativa 'non letto' ho potuto script su qualcos'altro. Ho provato questo:

Private Sub MailItem_Open()
    MsgBox "test"
End Sub
È stato utile?

Soluzione

Forse qualcosa sulla falsariga di:

Public WithEvents myOlInspectors As Outlook.Inspectors
Public myInspectorsCollection As New Collection

Private Sub Application_Startup()
    Initialize_handler
End Sub

Public Sub Initialize_handler()
    Set myOlInspectors = Application.Inspectors
End Sub

Private Sub myOlInspectors_NewInspector(ByVal Inspector As Outlook.Inspector)
If (Inspector.CurrentItem.Class = olMail) Then

    If Inspector.CurrentItem.Parent = "Inbox" Then
        strCats = Inspector.CurrentItem.Categories

        If InStr(strCats, "Read") = 0 Then
            If Not strCats = vbNullString Then
                strCats = strCats & ","
            End If
            strCats = strCats & "Read"
            Inspector.CurrentItem.Categories = strCats
            Inspector.CurrentItem.Save
        End If
    End If
End If
End Sub

È possibile che questo dovrebbe andare in ThisOutlookSession. Sarà necessario garantire che i livelli di sicurezza consentono le macro.

Altri suggerimenti

La risposta accettata identifica correttamente una e-mail aperto, ma ha un problema in quanto fallirà se c'è un'altra categoria che contiene quello che viene aggiunto. Ad esempio se la lista categoria contiene Read Later come una voce, non verrà aggiunto Read.

Inoltre, il separatore di elenco è difficile codificato, quando in realtà Outlook utilizza quello impostato nelle impostazioni internazionali.

Per risolvere entrambi questi approcci che è possibile utilizzare Split() per rompere l'elenco, cercare la lista per il valore, quindi Join() di rimetterlo insieme. Questo può essere fatto in collaborazione con il separatore di elenco corretto, come letto dal Registro di sistema.

Esempio di codice:

Public WithEvents myOlInspectors As Outlook.Inspectors
Public myInspectorsCollection As New Collection

Private Sub Application_Startup()
    Initialize_handler
End Sub

Public Sub Initialize_handler()
    Set myOlInspectors = Application.Inspectors
End Sub

Private Sub myOlInspectors_NewInspector(ByVal Inspector As Outlook.Inspector)
    If (Inspector.CurrentItem.Class = olMail) Then
        If Inspector.CurrentItem.Parent = "Inbox" Then
            AddCategory Inspector.CurrentItem, "Read"
            Inspector.CurrentItem.Save
        End If
    End If
End Sub

Sub AddCategory(aMailItem As MailItem, newCategory As String)
    Dim categories() As String
    Dim listSep As String

    ' Get the current list separator from Windows regional settings
    listSep = CreateObject("WScript.Shell").RegRead("HKEY_CURRENT_USER\Control Panel\International\sList")

    ' Break the list up into an array
    categories = Split(aMailItem.categories, listSep)

    ' Search the array for the new cateogry, and if it is missing, then add it
    If UBound(Filter(categories, newCategory)) = -1 Then
        ReDim Preserve categories(UBound(categories) + 1)
        categories(UBound(categories)) = newCategory
        aMailItem.categories = Join(categories, listSep)
    End If
End Sub
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top