在VBScript中处理COM事件取消 [英] Handling COM event cancelation in VBScript

查看:91
本文介绍了在VBScript中处理COM事件取消的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个脚本,该脚本使用CDO通过我们公司的SMTP服务器发送电子邮件.

I would like to write a script, that sends an E-Mail via our companys SMTP-Server using CDO.

首先,我试图为此目的编写一个HTA应用程序,但是它变得很讨人喜欢,以使其足够舒适,以便其他人可以很好地处理它(由于正确的收件人解析).

First I tried to write an HTA-application for that purpose, but it became rather fiddly to make it cormfortable enough so that other people may handle it well (because of proper recipient resolving).

因此,现在我尝试使用常规的Outlook-Mail掩码先准备邮件,然后通过VBScript捕获send-item事件,以将其内容提供给我的CDO脚本.

So now I try to use the regular Outlook-Mail mask to prepare the mail first and then catch the send-item event via VBScript to give it's content to my CDO script.

现在,我的代码如下:

Dim OutlookApplication
Dim MailItem
Const olDiscard = 1
Const olMailItem = 0

Set OutlookApplication = WScript.CreateObject("Outlook.Application", "Outlook_")
Set MailItem = OutlookApplication.CreateItem(olMailItem)
MailItem.Display

'(...) some code to add recipients, subject, text, etc... depending on the given WScript.Arguments

While Not MailItem Is Nothing
    'keep the script alive
    WScript.Sleep 1
WEnd

Function CDOSendMessage()
    'some code to send the data to our smtp server, return true if successfull
    CDOSendMessage = True
End Function

Sub Outlook_ItemSend(byVal Item, Cancel)
    If Item.body = MailItem.body Then 'Any more fail proof suggestions on how to check if it's the correct mailitem I'm handling with this event? While the script is alive, it fires for EVERY mail I send via outlook
        Cancel = True

        If CDOSendMessage() then
            Set MailItem = Nothing
            MailItem.Close olDiscard
        Else
            Cancel = False
            MsgBox "Sending message via CDO failed."
        End If
    End If
End Sub

主要问题是,Cancel = True根本不起作用.无论如何,Outlook都会使用我的常规邮件地址发送我的邮件.你能告诉我,我做错了吗?

The main problem is, that Cancel = True simply does not work. Outlook will send my mail using my regular mail adress no matter what. Can you tell me, what I'm doing wrong please?

非常感谢您!

Guido

推荐答案

必须使用ByRef修饰符声明Cancel参数.

The Cancel parameter must be declared with the ByRef modifier.

这篇关于在VBScript中处理COM事件取消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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