在 Outlook 中发送电子邮件时如何自动运行宏? [英] How can I automatically run a macro when an email is sent in Outlook?

查看:122
本文介绍了在 Outlook 中发送电子邮件时如何自动运行宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的脚本运行良好,但每次打开 Outlook 时我都必须手动运行 Initialize_handler 例程才能使其工作.

The script below works great but I have to manually run the Initialize_handler routine every time I open Outlook for it to work.

Public WithEvents myOlApp As Outlook.Application

Public Sub Initialize_handler()
Set myOlApp = CreateObject("Outlook.Application")
End Sub

Private Sub myOlApp_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

就我可以看到的自动工作而言,我应该能够将以下脚本添加到 ThisOutlookSession.但是,这不起作用,我不知道为什么.

As far as I can see to make this work automatically I should be able to add the below script to ThisOutlookSession. However this doesn't work and I have no idea why.

我的宏安全设置正确,它在启动时运行代码,但由于某种原因它不起作用,直到我手动打开 VBA 编辑器单击 Initialize_handler 并按 F5.

My macro security is set properly and it is running the code on startup but for some reason it doesn't work until I manually open the VBA editor click into Initialize_handler and press F5.

Private Sub Application_Startup()
  Initialize_handler
End Sub

推荐答案

这里描述的复杂方法 https://msdn.microsoft.com/en-us/library/office/ff865076.aspx 表示示例代码必须放在类模块中".

The convoluted method described here https://msdn.microsoft.com/en-us/library/office/ff865076.aspx indicates "The sample code must be placed in a class module".

我建议你只使用特殊的类模块 ThisOutlookSession.你可以用你自己的类模块进行试验,但如果你只是想让它工作,那么在 ThisOutlookSession 中用它替换你的所有代码.

I suggest you use the special class module ThisOutlookSession only. You could experiment with your own class module but if you just want this to work then replace all your code with this in ThisOutlookSession.

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

这篇关于在 Outlook 中发送电子邮件时如何自动运行宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆