【问题标题】:Automatic Category assignment for received email + File name attachment收到电子邮件的自动类别分配 + 文件名附件
【发布时间】:2025-12-10 20:50:02
【问题描述】:

我编写了一个脚本,根据主题中的一些首字母、正文中的一些词、发件人……为所有选定的电子邮件分配一个类别...

Public Sub autocategories()
    Dim olItem As Object
    For Each olItem In Application.ActiveExplorer.Selection
        If InStr(1, olItem.Subject, "=SUB1=", vbTextCompare) > 0 Then
            olItem.Categories = "SUB1"
        ElseIf InStr(1, olItem.Subject, "=SUB2=", vbTextCompare) > 0 Then
            olItem.Categories = "SUB2"
        ElseIf InStr(1, olItem.Sender, "SEN1", vbTextCompare) > 0 Then
            olItem.Categories = "SEN1"
        ElseIf InStr(1, olItem.Sender, "SEN2", vbTextCompare) > 0 Then
            olItem.Categories = "SEN2"
        ElseIf InStr(1, olItem.Body, "BOD1", vbTextCompare) > 0 Then
            olItem.Categories = "BOD1"
        ElseIf InStr(1, olItem.Body, "BOD2", vbTextCompare) > 0 Then
            olItem.Categories = "BOD2"
        End If
        olItem.Save
    Next olItem
    Set olItem = Nothing
End Sub

我制作了第二个脚本来自动为所有发送的电子邮件分配一个类别。

Private Sub Application_ItemSend(ByVal olItem As Object, Cancel As Boolean)
    With olItem
        If InStr(1, olItem.Subject, "=SUB1=", vbTextCompare) > 0 Then
            olItem.Categories = "SUB1"
            olItem.Save
        ElseIf InStr(1, olItem.Subject, "=SUB2=", vbTextCompare) > 0 Then
            olItem.Categories = "SUB2"
            olItem.Save
        ElseIf InStr(1, olItem.Body, "BOD1", vbTextCompare) > 0 Then
            olItem.Categories = "BOD1"
            olItem.Save
        ElseIf InStr(1, olItem.Body, "BOD2", vbTextCompare) > 0 Then
            olItem.Categories = "BOD2"
            olItem.Save
        Else: End If
    End With
lbl_Exit:
    Exit Sub
End Sub

对于收到的电子邮件:
- 我希望自动完成分配,而不必选择电子邮件并单击宏按钮
- 使用规则不是一个选项,因为它需要更新我公司禁止的密钥注册表。

对于接收和发送的电子邮件:
- 我想识别附件的文件名
- 我试过这个:

ElseIf InStr(1, olItem.Attachemnts, "[NAME1]", vbTextCompare) > 0 Then
    olItem.Categories = "[NAME1]"
    olItem.Save

【问题讨论】:

    标签: vba outlook


    【解决方案1】:

    试试下面的 -

    Option Explicit
    Private WithEvents Items As Outlook.Items
    Private Sub Application_Startup()
        Dim olNs As Outlook.NameSpace
        Dim Inbox  As Outlook.MAPIFolder
    
        Set olNs = Application.GetNamespace("MAPI")
        Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
        Set Items = Inbox.Items
    End Sub
    
    Private Sub Items_ItemAdd(ByVal Item As Object)
        If TypeOf Item Is Outlook.MailItem Then
            autocategories Item
        End If
    End Sub
    
    Public Sub autocategories(ByVal olItem As Object)
            If InStr(1, olItem.Subject, "=SUB1=", vbTextCompare) > 0 Then
                olItem.Categories = "SUB1"
            ElseIf InStr(1, olItem.Subject, "=SUB2=", vbTextCompare) > 0 Then
                olItem.Categories = "SUB2"
            ElseIf InStr(1, olItem.Sender, "SEN1", vbTextCompare) > 0 Then
                olItem.Categories = "SEN1"
            ElseIf InStr(1, olItem.Sender, "SEN2", vbTextCompare) > 0 Then
                olItem.Categories = "SEN2"
            ElseIf InStr(1, olItem.body, "BOD1", vbTextCompare) > 0 Then
                olItem.Categories = "BOD1"
            ElseIf InStr(1, olItem.body, "BOD2", vbTextCompare) > 0 Then
                olItem.Categories = "BOD2"
            End If
            olItem.Save
        Set olItem = Nothing
    End Sub
    
    
    Private Sub TestMsg()
        Dim olMsg As Outlook.MailItem
        Set olMsg = ActiveExplorer.selection.Item(1)
        FwdItem olMsg
    End Sub
    

    【讨论】:

    • 您好,感谢您的评论。实际上,我希望我的第一个脚本识别名称附件并根据它分配一个类别。我应该如何包含它?所以像我写的第一个代码中包含的东西: ElseIf InStr(1, Atmt.FileName, "example", vbTextCompare) > 0 Then olItem.Categories = "CAT1" 保持相同的宏按钮对我来说非常重要,所以我可以对基于电子邮件的主题、正文、发件人和文件名附件进行排序。自动化并不那么重要
    【解决方案2】:

    类似的东西

    Option Explicit
    Private WithEvents inboxItems As Outlook.Items
    Private WithEvents colSentItems As Outlook.Items
    
    Private Sub Application_Startup()
      Dim outlookApp As Outlook.Application
      Dim objectNS As Outlook.NameSpace
    
      Set outlookApp = Outlook.Application
      Set objectNS = outlookApp.GetNamespace("MAPI")
      Set inboxItems = objectNS.GetDefaultFolder(olFolderInbox).Items
      Set colSentItems = objectNS.GetDefaultFolder(olFolderSentMail).Items
    End Sub
    
    Private Sub inboxItems_ItemAdd(ByVal Item As Object)
    On Error GoTo ErrorHandler
    Dim Msg As Outlook.MailItem
    Dim MessageInfo
    Dim Result
    Dim objAtt As Outlook.Attachment
    If TypeName(Item) = "MailItem" Then
        'MessageInfo = "" & _
            "Sender : " & Item.SenderEmailAddress & vbCrLf & _
            "Sent : " & Item.SentOn & vbCrLf & _
            "Received : " & Item.ReceivedTime & vbCrLf & _
            "Subject : " & Item.Subject & vbCrLf & _
            "Size : " & Item.Size & vbCrLf & _
            "Message Body : " & vbCrLf & Item.Body
        'Result = MsgBox(MessageInfo, vbOKOnly, "New Message Received")
    
                If InStr(1, Item.Subject, "=SUB1=", vbTextCompare) > 0 Then
                    Item.Categories = "SUB1"
                ElseIf InStr(1, Item.Subject, "=SUB2=", vbTextCompare) > 0 Then
                    Item.Categories = "SUB2"
                ElseIf InStr(1, Item.Sender, "SEN1", vbTextCompare) > 0 Then
                    Item.Categories = "SEN1"
                ElseIf InStr(1, Item.Sender, "SEN2", vbTextCompare) > 0 Then
                    Item.Categories = "SEN2"
                ElseIf InStr(1, Item.Body, "BOD1", vbTextCompare) > 0 Then
                    Item.Categories = "BOD1"
                ElseIf InStr(1, Item.Body, "BOD2", vbTextCompare) > 0 Then
                    Item.Categories = "BOD2"
                End If
                For Each objAtt In Item.Attachments
                    'objAtt.SaveAsFile saveFolder & "\" & Item.Parent & "\" & objAtt.DisplayName
                    If InStr(1, objAtt.DisplayName, "[NAME1]", vbTextCompare) > 0 Then
                        Item.Categories = "[NAME1]"
                        Item.Save
                    End If
                    Set objAtt = Nothing
                Next
                Item.Save
    End If
    ExitNewItem:
        Exit Sub
    ErrorHandler:
        MsgBox Err.Number & " - " & Err.Description
        Resume ExitNewItem
    End Sub
    
    
    
    Private Sub colSentItems_ItemAdd(ByVal Item As Object)
        If Item.Class = olMail Then
           'Item.ShowCategoriesDialog
            If InStr(1, Item.Subject, "=SUB1=", vbTextCompare) > 0 Then
                Item.Categories = "SUB1"
            ElseIf InStr(1, Item.Subject, "=SUB2=", vbTextCompare) > 0 Then
                Item.Categories = "SUB2"
            ElseIf InStr(1, Item.Body, "BOD1", vbTextCompare) > 0 Then
                Item.Categories = "BOD1"
            ElseIf InStr(1, Item.Body, "BOD2", vbTextCompare) > 0 Then
                Item.Categories = "BOD2"
            End If
            Item.Save
        End If
    End Sub
    

    附件

    For Each objAtt In itm.Attachments
        objAtt.SaveAsFile saveFolder & "\" & itm.Parent & "\" & objAtt.DisplayName
    Next
    

    【讨论】:

    • 不需要outlookApp,代码已经在Outlook中-只需使用Application
    • “使用应用程序”是什么意思?
    • 嘿,抱歉,代码不起作用。我将其复制粘贴到“ThisOutlookSession”中,并用我需要的内容进行了更新,但没有结果。你能告诉我,我的初始脚本如何添加一行以便它可以识别文件名附件吗?那会有所帮助!谢谢
    • item.Attachments(0).Di​​splayName
    • 喜欢这个? ElesIf InStr(1, olItem.Attachments(0).DisplayName, "example", vbTextCompare) > 0 Then olItem.Categories = "CAT1"