简体   繁体   English

使用 VBA 根据票证 ID 创建 Outlook email 路由规则

[英]Create Outlook email routing rule based on ticket ID using VBA

I tried to create email routing rule with below scenario.我尝试使用以下场景创建 email 路由规则。

  • Incoming email will be located at Inbox/Active folder.传入的 email 将位于收件箱/活动文件夹中。 Subject of the email will contain the ticket ID and content email 的主题将包含票证 ID 和内容
  • Once new email coming to Active subfolder, Outlook will get the email subject and create the subfolder with format "ticket ID - content" eg: "123123 - issue with outlook"一旦新的 email 进入 Active 子文件夹,Outlook 将获得 email 问题主题并创建格式为“ticket ID - content”的子文件夹,例如:“1
  • Then a rule will be created to route this incoming email with ticket ID to the subfolder that I just created然后将创建一个规则,将带有票证 ID 的传入 email 路由到我刚刚创建的子文件夹

Below is my code but it did not work.下面是我的代码,但它不起作用。 Only subfolder is created as expected.仅按预期创建子文件夹。 Please help me to review if any idea.如果有任何想法,请帮助我审查。 Thanks谢谢

Option Explicit
Private WithEvents inboxItems As Outlook.Items
Private Sub Application_Startup()
  Dim olapp As Outlook.Application
  Dim olnamespace As Outlook.NameSpace
  
  Set olapp = Outlook.Application
  Set olnamespace = olapp.GetNamespace("MAPI")
  Set inboxItems = olnamespace.GetDefaultFolder(olFolderInbox).Folders("Filter").Items
End Sub

Private Sub inboxItems_ItemAdd(ByVal Item As Object)
On Error GoTo ErrorHandler

Dim olapp As Outlook.Application
Dim olnamespace As Outlook.NameSpace
Dim olActivefolder As Folder
Dim ticketnumber As String
Dim rightsubject As String
Dim leftsubject As String
Dim extsubject As String

 Dim colRules As Outlook.Rules 
 Dim oRule As Outlook.Rule 
 Dim colRuleActions As Outlook.RuleActions 
 Dim oMoveRuleAction As Outlook.MoveOrCopyRuleAction 
 Dim oFromCondition As Outlook.ToOrFromRuleCondition 
 Dim oExceptSubject As Outlook.TextRuleCondition 
 Dim oInbox As Outlook.Folder 
 Dim oMoveTarget As Outlook.Folder 
  
Set olapp = Outlook.Application
Set olnamespace = olapp.GetNamespace("MAPI")
Set olActivefolder = olnamespace.GetDefaultFolder(olFolderInbox).Folders("Active")

If TypeName(Item) = "MailItem" Then
   Debug.Print "triggered"
   ticketnumber = Item.Subject
   rightsubject = Right(ticketnumber, 16)
   leftsubject = Left(ticketnumber, 60)
   olActivefolder.Folders.Add (rightsubject & " - " & leftsubject)
End If

 Set oInbox = Application.Session.GetDefaultFolder(olFolderInbox).Folders("Active")
 Set oMoveTarget = oInbox.Folders(rightsubject & " - " & leftsubject) 

 Set colRules = Application.Session.DefaultStore.GetRules()  
 Set oRule = colRules.Create(rightsubject, olRuleReceive) 

 Set oFromCondition = oRule.Conditions.Subject
 With oFromCondition 
 .Enabled = True 
 .Text = rightsubject
End With 

 Set oMoveRuleAction = oRule.Actions.MoveToFolder 
 With oMoveRuleAction 
 .Enabled = True 
 .Folder = oMoveTarget 
 End With 

colRules.Save 

ExitNewItem:
    Exit Sub
ErrorHandler:
    MsgBox Err.Number & " - " & Err.Description
    Resume ExitNewItem
End Sub

The subject condition should look like this:主题条件应如下所示:

'Dim oFromCondition As Outlook.ToOrFromRuleCondition
'Set oFromCondition = oRule.Conditions.subject
'With oFromCondition
'    .Enabled = True
'    .Text = rightSubject
'End With

Dim oSubjectCondition As TextRuleCondition
Set oSubjectCondition = oRule.Conditions.subject
With oSubjectCondition
    .Enabled = True
    .Text = Array(rightSubject)
End With

There is likely no need for rules.可能不需要规则。

Private Sub inboxItems_ItemAdd_Test()
    inboxItems_ItemAdd ActiveInspector.CurrentItem
End Sub

Private Sub inboxItems_ItemAdd(ByVal Item As Object)

' Folder created for first mail
' No folder created for subsequent mail

Dim oInbox As folder
Dim oActivefolder As folder
Dim oMoveTarget As folder
Dim oFolder As folder

Dim ticketNumber As String

Set oInbox = Session.GetDefaultFolder(olFolderInbox)
Set oActivefolder = oInbox.Folders("Active")

If TypeName(Item) = "MailItem" Then

    Debug.Print "triggered"
    
    ' For testing
    ticketNumber = "123123"
    
    For Each oFolder In oActivefolder.Folders
        If oFolder.Name = ticketNumber Then
            Set oMoveTarget = oActivefolder.Folders(ticketNumber)
            Debug.Print " Folder exists: " & oMoveTarget.Name
            Exit For
        End If
    Next
    
    If oMoveTarget Is Nothing Then
        Set oMoveTarget = oActivefolder.Folders.Add(ticketNumber)
        Debug.Print " Folder added: " & oMoveTarget.Name
    End If
        
    Item.Move oMoveTarget

End If

Debug.Print "Done."

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用脚本在 Outlook 中处理基于规则的电子邮件 - rule based email handling in outlook using scripts 使用VBA创建一个规则,以在传出的Outlook电子邮件中添加密件抄送地址,具体取决于所使用的帐户 - Using VBA create a rule to add a BCC address on outgoing Outlook email, depending on the account used 使用 Access VBA 创建动态 Outlook 电子邮件 - Create a Dynamic Outlook Email using Access VBA 使用 Email ID 或带有 VBA 的扩展属性查找 Outlook Email - Find Outlook Email using Email ID or Extended Property with VBA 使用VBA根据传入的Outlook电子邮件发送警报电子邮件 - Send an alert email based on incoming outlook email using VBA 如何使用 VBA 根据电子邮件主题提取 Outlook 电子邮件数据? - How to Extract outlook email data based on subject of the email using VBA? 使用 vba 创建/复制(所有帐户上的此规则)Outlook - Create/Copy (this rule on all accounts) outlook using vba vba Outlook根据用户表单的数据和模板创建电子邮件 - vba Outlook create an email based on a userform's data and a template 如何使用基于主题的 Python 在 Outlook 中创建规则 - How to Create a Rule in Outlook Using Python Based on Subject Excel VBA创建一个Outlook电子邮件规则(在最新更新之后被破坏) - Excel VBA to create an Outlook Email Rule (broken after most recent update)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM