英文:
Is there a way to intercept a SEND in Outlook to do further verification on the recipients?
问题
I need to add a macro to Outlook so that when I press SEND to send an e-mail, it makes a further check on the recipients and if at least one of these falls under a specific list, it asks me for further confirmation before sending the e-mail. This is to avoid that I accidentally send an email to someone who shouldn't receive it, as sometimes I don't notice that outlook has pre-filled one of the destination fields with a different email than I wanted, as I am visually impaired.
I'm not very experienced in VBA and I don't even know if it's possible to intercept a SEND, but I know Python well and it would be perfect if I could use a macro in Python with pywin32. Alternatively, however, I can get by with VBA as well. I need it to work with both Outlook Desktop and Outlook 365 from the browser.
Do you think it is possible? And if so, how?
英文:
I need to add a macro to Outlook so that when I press SEND to send an e-mail, it makes a further check on the recipients and if at least one of these falls under a specific list, it asks me for further confirmation before sending the e-mail. This is to avoid that I accidentally send an email to someone who shouldn't receive it, as sometimes I don't notice that outlook has pre-filled one of the destination fields with a different email than I wanted, as I am visually impaired.
I'm not very experienced in VBA and I don't even know if it's possible to intercept a SEND, but I know Python well and it would be perfect if I could use a macro in Python with pywin32. Alternatively, however, I can get by with VBA as well. I need it to work with both Outlook Desktop and Outlook 365 from the browser.
Do you think it is possible? And if so, how?
答案1
得分: 1
在一般情况下,这是可能的。您可以处理Outlook Application
类的ItemSend
事件,该事件在用户通过Inspector
(在检查员关闭之前,但在用户单击Send
按钮之后)或在程序中使用Outlook项目(例如MailItem
)的Send
方法发送项目时触发。例如,在VBA中,您可以使用以下代码(要正确添加事件处理程序,您需要从左侧的下拉列表中选择Application对象,然后在右侧选择ItemSend
事件):
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim prompt As String
prompt = "Are you sure you want to send " & Item.Subject & "?"
If MsgBox(prompt, vbYesNo + vbQuestion, "Sample") = vbNo Then
Cancel = True
End If
End Sub
正如您所见,Item
的实例,它代表即将发送的对象,作为参数传递给事件处理程序。您可以检查所有返回Recipients
集合的Recipients
属性,该集合代表Outlook项目的所有收件人。
如果你想在Python中使用pywin32宏,这将是完美的。
您可以从包括Python在内的任何编程语言中自动化Outlook。但是,每次您想处理发送的电子邮件时,都需要将代码与Outlook一起运行。只有VBA可以在Outlook启动时运行。此外,如果需要在多台计算机上部署解决方案,您可以考虑为Outlook创建一个COM插件。这就是引入COM插件的原因。
我需要它能够在Outlook桌面版和浏览器中的Outlook 365中使用。
VBA(以及COM插件)仅适用于Outlook桌面版。如果您需要支持多个平台(OWA或MacOS),您需要为Outlook创建一个Web插件,在其中您也可以处理ItemSend
事件,请参阅用于Outlook插件的On-send功能。
在构建您的第一个Outlook插件页面上了解有关Outlook Web插件的更多信息。
英文:
In general, it is possible. You can handle the ItemSend
event of the Outlook Application
class which is fired whenever an Microsoft Outlook item is sent, either by the user through an Inspector
(before the inspector is closed, but after the user clicks the Send
button) or when the Send
method for an Outlook item, such as MailItem
, is used in a program. For example, in VBA you can use the following code (to add an event handler correctly you need to choose the Application object from the drop-down list on the left side and the ItemSend
event on the right hand side):
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim prompt As String
prompt = "Are you sure you want to send " & Item.Subject & "?"
If MsgBox(prompt, vbYesNo + vbQuestion, "Sample") = vbNo Then
Cancel = True
End If
End Sub
As you can see an instance of the Item
which represents the object which is being sent is passed as a parameter to the event handler. You may check all the Recipients property which returns a Recipients
collection that represents all the recipients for the Outlook item.
> it would be perfect if I could use a macro in Python with pywin32.
You can automate Outlook from any programming language including Python. But you need to get the code running with Outlook every time you want to handle outgoing emails. Only VBA can be run with Outlook (when it starts). Also you may consider creating a COM add-in for Outlook if you need to deploy your solution on multiple machines. That is for COM add-ins were introduced.
> I need it to work with both Outlook Desktop and Outlook 365 from the browser.
VBA (as well as COM add-ins) is for Outlook Desktop only. If you need to support multiple platforms (OWA or MacOS) you need to create a web add-in for Outlook where you could also handle the ItemSend
event, see On-send feature for Outlook add-ins.
Read more about Outlook web add-ins on the Build your first Outlook add-in page.
答案2
得分: 0
使用 Application.ItemSend 事件 - 它传递正在发送的项(以便您可以枚举 MailItem.Recipients 集合并验证每个 Recipient 对象),并且您还可以(可选地)通过将 Cancel
参数设置为 true
来取消提交。
英文:
Use Application.ItemSend event - it passes the item beeing sent (so that you can enumerate MailItem.Recipients collection and validate each Recipient object) and you can also (optionally) cancel the submission by setting the Cancel
parameter to true
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论