【发布时间】: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
【问题讨论】: