为接收的电子邮件和文件名附件自动分配类别

时间:2019-04-25 07:08:32

标签: vba outlook outlook-vba

我制作了一个脚本,根据主题中的一些首字母,正文中的某些词,发件人,...

,为所有选定的电子邮件分配类别。

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

2 个答案:

答案 0 :(得分:0)

类似的东西

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

答案 1 :(得分:0)

尝试以下-

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