通过“规则和警报”中的宏发送电子邮件

时间:2014-04-30 15:41:44

标签: vba outlook rules

我想通过“规则和提醒”发送自定义电子邮件。在Outlook 2007中。

Sub SpirentBoxcarEmail()

Dim objOutlook As Object
Dim objOutlookMsg As Object

Set objOutlook = CreateObject("Outlook.Application")
Set objOutlookMsg = objOutlook.CreateItem(0)

With objOutlookMsg
    .To = "me@email.com"
    .Subject = "Test"
    .Body = "Test"
    .Send
End With

Set objOutlookMsg = Nothing
Set objOutlook = Nothing
End Sub

我知道我需要添加类似于' item As Outlook.MailItem'进入子标题的(),以显示在' Scripts'

下的宏

但是,我是否真的需要为此项'?

分配任何内容

如果没有'项目,宏运行得很好。添加,但一旦我添加项目作为Outlook.MailItem'并且规则执行脚本(宏),电子邮件不会被发送出去。

3 个答案:

答案 0 :(得分:0)

非常奇怪,对我来说有收到的消息规则。您是否可以在过程中设置断点并查看脚本是否实际执行?

答案 1 :(得分:0)

我认为outlook正在寻找特定的功能签名。规则期望将当前邮件项目传递给宏。试试这个。

Sub SpirentBoxcarEmail(ByRef t As MailItem)

Dim objOutlook As Object
Dim objOutlookMsg As Object

Set objOutlook = CreateObject("Outlook.Application")
Set objOutlookMsg = objOutlook.CreateItem(0)

With objOutlookMsg
    .To = "me@email.com"
    .Subject = "Test"
    .Body = "Test"
    .Send
End With

Set objOutlookMsg = Nothing
Set objOutlook = Nothing
End Sub

答案 2 :(得分:0)

您已编写RunAScript代码,好像它不依赖于正在接收的项目。

打开mailitem并运行SpirentBoxcarEmail_RunAScript_Test

Sub SpirentBoxcarEmail_RunAScript(item As mailitem)

'Dim objOutlook As Object
'Dim objOutlookMsg As Object

Dim objOutlookMsg As mailitem

' Normally the trigger item is processed in some way.

MsgBox "The subject of the received mail that triggered the script is: " & _
    vbCr & vbCr & item.Subject

'  In this case the trigger item will now be ignored.

'Set objOutlook = CreateObject("Outlook.Application")
Set objOutlookMsg = CreateItem(0)

With objOutlookMsg
    .To = "me@email.com"
    .Subject = "Test"
    .body = "Test"
    .Send
End With

Set objOutlookMsg = Nothing
'Set objOutlook = Nothing
End Sub


Private Sub SpirentBoxcarEmail_RunAScript_Test()

' Open mail that fulfills the conditions of the rule
' This simulates mail being received

Dim currItem As mailitem
Set currItem = ActiveInspector.currentItem
SpirentBoxcarEmail_RunAScript currItem

End Sub