发送电子邮件时运行宏 [英] Run macro when email is sent
问题描述
我试图编写一个宏,该宏在用户单击发送"按钮时会查看电子邮件的主题行.
I am trying to write a macro that looks at the subject line of an email whenever the user hits the Send button.
但是我找不到监听该按钮的任何文档.现在,我只是想让它在发送电子邮件时发送带有主题的MsgBox
.有没有办法听(以DOM的方式思考)此按钮并在点击时触发宏.
However I can't find any documentation that listens to that button. For right now I am just trying to get it to send a MsgBox
with the subject when the email is sent. Is there a way to listen (thinking in terms of DOMs) to this button and fire a macro on the click.
推荐答案
如Siddharth所建议:
As suggested by Siddharth:
我写了一个小演示,它检查一些条件以确定是否应该取消发送操作.可以将其扩展为执行其他操作,例如插入日期,将邮件保存到某个文件夹,...
I have written a small demo which checks some conditions to decide, if the send operation should be canceled. This could be extended to do other things like inserting dates, saving the mails to some folder, ...
Option Explicit
Private Sub Application_ItemSend(ByVal objItem As Object, Cancel As Boolean)
Dim mi As MailItem
If TypeName(objItem) = "MailItem" Then
Set mi = objItem
Debug.Print mi.Subject
check Cancel, Trim(mi.Subject) <> "", "Subject is empty!"
check Cancel, Not isRecipient(mi, "John@Doe.net"), _
"John is on our embargo list!"
End If
End Sub
Private Sub check(ByRef Cancel As Boolean, cond As Boolean, msg As String)
If Not (Cancel Or cond) Then
Cancel = (MsgBox(msg & vbCrLf & "Cancel send operation?", _
vbYesNoCancel, "Confirm?") <> vbNo)
End If
End Sub
Private Function isRecipient(mi As MailItem, forbidden As String) As Boolean
Dim ret As Boolean
Dim rc As Recipient
ret = False
For Each rc In mi.recipients
If StrComp(rc.Address, forbidden, vbTextCompare) = 0 Then
ret = True
Exit For
End If
Next
isRecipient = ret
End Function
这篇关于发送电子邮件时运行宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!