gpt4 book ai didi

vba - 如何在执行 outlook 规则后运行宏?

转载 作者:行者123 更新时间:2023-12-05 04:19:20 28 4
gpt4 key购买 nike

我在 outlook 上设置了一个规则来永久删除收到的电子邮件(基于主题条件)。
我还设置了一个 vba 宏来转发传入的电子邮件(因为转发电子邮件的任何规则都被 IT 禁用)。
问题: (forward incoming emails) 的宏在执行 outlook 规则之前自动运行。
在 outlook 上执行并完成该规则后,我需要运行我的宏。
我不喜欢将规则本身转移到 vba 代码中。
预先感谢您的支持。

Private Sub objInboxItems_ItemAdd(ByVal item As Object)             'Forward Incoming emails
Dim objMail As Outlook.MailItem
Dim objForward As Outlook.MailItem

If Not TypeOf item Is MailItem Then Exit Sub

Set objMail = item
Set objForward = objMail.Forward

With objForward
.Recipients.Add ("***@yahoo.com")
.Recipients.ResolveAll
.Send
End With

Set objMail = Nothing
Set objForward = Nothing
End Sub

最佳答案

强制规则在启动时运行。

假设这将在 ItemAdd 代码之前运行。

在这个 OutlookSession 中

Private Sub Application_Startup()
Dim st As Store
Dim myRules As Rules
Dim rl As Rule
Dim ruleName As String

Dim k As Long

' get store (can be any store)
Set st = Session.defaultStore
Debug.Print st.DisplayName

' get rules from specified store
Set myRules = st.GetRules()

ruleName = "name of rule to delete incoming email based on subject"

For k = 1 To myRules.count

Set rl = myRules(k)
Debug.Print myRules(k).name

If myRules(k).name = ruleName Then
' Regardless of rule store,
' the applicable Folder is by default the inbox of the default store
' Designate a folder if not the default inbox
rl.Execute ShowProgress:=True
Debug.Print ruleName & " has run."
Exit For
End If

Next

End Sub

关于vba - 如何在执行 outlook 规则后运行宏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74833945/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com