Outlook可以一次处理多个宏/事件处理程序吗?我可以将2个宏组合成一个吗?

时间:2016-10-20 23:39:53

标签: vba outlook eventhandler

这些代码,在ThisOutlookSession中,当我自己粘贴它们时,按预期工作,但如果我一起尝试它们,我会收到错误。

下面的第一个代码使得每当我点击发送电子邮件时,我都会弹出一个问题,询问我是否要继续发送:

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    If MsgBox("Do you want to continue sending the mail?", vbOKCancel) <> vbOK Then
        Cancel = True
End Sub

下面的第二个代码使得一旦发送电子邮件(一旦它发送到我发送的文件夹),我会弹出一个询问我是否要打印电子邮件的弹出窗口。

Private Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As
String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As
Long
Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
Dim Ns As Outlook.NameSpace
Dim Folder As Outlook.MAPIFolder
Set Ns = Application.GetNamespace("MAPI")
Set Folder = Ns.GetDefaultFolder(olFolderSentMail)
Set Items = Folder.Items
End Sub

Private Sub Items_ItemAdd(ByVal Item As Object)
If TypeOf Item Is Outlook.MailItem Then
If MsgBox("Print email?", vbYesNo Or vbQuestion) = vbYes
Then
Item.PrintOut
End If
End If

End Sub

我想在发送邮件时弹出窗口,在邮件发送后弹出第二个窗口。我尝试将这两个代码全部粘贴到ThisOutlookSession中。我收到此错误

enter image description here

有没有人对如何使其发挥作用有任何见解,或者这是否可能?

1 个答案:

答案 0 :(得分:1)

我将代码丢弃到代码错误中,并弹出一些错误。别担心。它们很容易修复。它看起来像一行上的一些原始代码被分成多行,无法正确读取。试试这个:

yaws_api:embedded_start_conf/1,2,3,4

现在解释一下。原始代码中的以下内容实际上是1行代码:

Private Declare Function ShellExecute Lib "shell32.dll" Alias _
    "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
    ByVal lpFile As String, ByVal lpParameters As String, _
    ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private WithEvents Items As Outlook.Items


Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    If MsgBox("Do you want to continue sending the mail?", vbOKCancel) <> vbOK Then
        Cancel = True
    End If
End Sub



Private Sub Application_Startup()
    Dim Ns As Outlook.NameSpace
    Dim Folder As Outlook.MAPIFolder
    Set Ns = Application.GetNamespace("MAPI")
    Set Folder = Ns.GetDefaultFolder(olFolderSentMail)
    Set Items = Folder.Items
End Sub

Private Sub Items_ItemAdd(ByVal Item As Object)
    If TypeOf Item Is Outlook.MailItem Then
        If MsgBox("Print email?", vbYesNo Or vbQuestion) = vbYes _
           Then
            Item.PrintOut
        End If
    End If

End Sub

您可以将多行代码拆分为多行,并在行尾添加空格和下划线。有一些像这样的地方缺少空间和下划线。您可以将它们添加到行尾或将文本移动到一行。我做了两点。

同样的问题发生在这里:

Private Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As
String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As
Long

这需要一行代码。因此,以下两种方法都有效,但第一种方法更常见,更容易阅读:

If MsgBox("Print email?", vbYesNo Or vbQuestion) = vbYes
Then

......与

相同
If MsgBox("Print email?", vbYesNo Or vbQuestion) = vbYes Then

最后,那些不是&#34; sub&#34;的部分,如&#34;私有声明......&#34;和#34; Private WithEvents ......&#34;需要位于脚本的顶部,在所有潜艇上方。