【问题标题】:How to run outlook's rules with VBA?如何使用 VBA 运行 Outlook 规则?
【发布时间】:2020-03-03 21:20:02
【问题描述】:

我是 VBA 编码的新手。 我正在尝试在 Outlook 中创建规则,但它不起作用。我需要的工作流程是 1.Detect 主题邮件 = Approve.,2.Detect body mail = Approve.3.Detect sender E-mail and final Send E-mail to我的目标电子邮件。

我尝试通过回复脚本使用发送邮件的 python 脚本,但它不起作用并尝试使用 VBA 并发现许多解决方案也不起作用。请帮助我。

这是我的代码:

Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Dim oPA As Outlook.PropertyAccessor
Dim oContact As Outlook.ContactItem
Dim oSender As Outlook.AddressEntry
'==default local Inbox====================================================
Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
'MsgBox ("Request for ID Document")
End Sub
Public Sub Items_ItemAdd(ByVal item As Object)
On Error GoTo ErrorHandler
Dim Msg As Outlook.MailItem
Dim xEmployer As String
Dim xLine As String
Dim xMessage As String
Dim SenderID As String
Dim SenderEmail As String
Dim xBy As String
Dim xEmail As String
Dim xFunc As Boolean
Dim xRunFile As String
Dim olAddrList      As AddressList
Dim olAddrEntry     As AddressEntry
Dim olExchgnUser    As ExchangeUser

If TypeName(item) = "MailItem" Then
'=========================================================================
Set Msg = item
Set oPA = Msg.PropertyAccessor
SenderID = oPA.BinaryToString _
   (oPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0C190102"))
'Obtain AddressEntry Object of the sender
Set oSender = Application.Session.GetAddressEntryFromID(SenderID)

SenderEmail = oSender.Address

  Set OL = CreateObject("Outlook.Application")
  Set EmailItem = OL.CreateItem(0)
  Dim OL              As Object
  Dim EmailItem       As Object
  Dim StrFileName     As String

If (InStr(UCase(Msg.body), "Approve") > 0) And _
  (InStr(UCase(Msg.subject), "Approve") > 0) And _
  ((InStr(UCase(Msg.SenderEmailAddress), "CFGFIN006") > 0)) Then
   With EmailItem
       .subject = "AP_Subject"
       .body = "AP_Body"
    .To = "my_manager_name@example.com"
    .CC = ""
    .BCC = ""
    .Importance = 1
    .Send
    End With
Set Doc = Nothing
Set EmailItem = Nothing
Set OL = Nothing
SendMail = True
 End If
 End sub

【问题讨论】:

    标签: vba outlook


    【解决方案1】:

    比较文本时要更加小心。

    Option Explicit ' Consider this mandatory
    ' Tools | Options | Editor tab
    ' Require Variable Declaration
    
    Private WithEvents defInboxItems As Items
    
    Private Sub Application_Startup()
    
        Dim defInboxItems As Items
    
        '== default local Inbox items ===================================
        Set defInboxItems = Session.GetDefaultFolder(olFolderInbox).Items
    
    End Sub
    
    
    Public Sub defInboxItems_ItemAdd(ByVal Item As Object)
    
    Dim msg As MailItem
    
    Dim oPA As propertyAccessor
    
    Dim SenderID As String
    Dim oSender As AddressEntry
    Dim SenderEmail As String
    
    Dim EmailItem As MailItem
    
    If TypeName(Item) = "MailItem" Then
    
        Set msg = Item
        Set oPA = msg.propertyAccessor
    
        SenderID = oPA.BinaryToString(oPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0C190102"))
    
        'Obtain AddressEntry Object of the sender
        Set oSender = Session.GetAddressEntryFromID(SenderID)
    
        SenderEmail = oSender.Address
        Debug.Print " SenderEmail: " & SenderEmail
    
        ' Break If conditions to more readily see where a problem, if any, occurs
        If InStr(UCase(msg.Body), ("APPROVE")) > 0 Then
    
            ' You can use UCase / LCase on everything
            If InStr(UCase(msg.Subject), UCase("Approve")) > 0 Then
    
                ' You can use vbTextCompare
                If InStr(UCase(msg.SenderEmailAddress), "CFGFIN006", vbTextCompare) > 0 Then
    
                    Set EmailItem = CreateItem(olMailItem)
    
                    With EmailItem
                        .Subject = "AP_Subject"
                        ' ...
                        .Display
                    End With
    
                End If
            End If
        End If
    
    End If
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-27
      • 2014-09-09
      • 2015-08-20
      • 1970-01-01
      • 2018-09-23
      • 1970-01-01
      相关资源
      最近更新 更多