【问题标题】:Handling COM event cancelation in VBScript在 VBScript 中处理 COM 事件取消
【发布时间】:2019-01-11 12:53:11
【问题描述】:

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

首先我尝试为此目的编写一个 HTA 应用程序,但是让它变得足够舒适以便其他人可以很好地处理它变得相当繁琐(因为正确的接收者解析)。

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

现在,我的代码如下所示:

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 都会使用我的常规邮件地址发送我的邮件。你能告诉我,我做错了什么吗?

非常感谢您!

圭多

【问题讨论】:

    标签: events outlook vbscript wsh cdo.message


    【解决方案1】:

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

    【讨论】:

    • 遗憾的是,这并没有改变我脚本的行为......据我所知,无论如何 ByRef 是 VBScript 中的默认修饰符。
    • 否,默认为 ByVal。请出示您更新后的代码。
    • 我想我明白了,为什么它不起作用。我有类似的情况,我尝试使用 VBS 中的 byRef-Parameter 调用对象方法。它根本没有通过Ref处理它。我花了一段时间才找到答案(实际上是在 stackoverflow 上):传递变量 byRef 要求变量与函数参数的类型相同。但是 VBS 只支持Variant 数据类型。这就是为什么当将 byRef 传递给 MS Office 中的方法时它们的值不会改变的原因。我必须在 powershell(实际上支持所有数据类型)中编写代码才能使其工作......
    【解决方案2】:

    根据要求更新了代码: 昏暗的前景应用程序 暗淡的邮件项 暗CDODone:CDODone = False 常量 olDiscard = 1 常量 olMailItem = 0

    Set OutlookApplication = WScript.CreateObject("Outlook.Application", "Outlook_")
    Set MailItem = OutlookApplication.CreateItem(olMailItem)
    MailItem.UserProperties.Add "CDOFlag", 20, false, false
    MailItem.Display
    
    '(...) some code to add recipients, subject, text, etc... depending on the given WScript.Arguments
    
    While Not CDODone Is Nothing
        'keep the script alive
        WScript.Sleep 1
    WEnd
    MailItem.Close olDiscard
    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, byRef Cancel)
        If Not Item.UserProperties.Find(CDOFlag) Is Nothing Then
            Cancel = True
    
            If CDOSendMessage() then
                CDODOne = True
            Else
                Cancel = False
                MsgBox "Sending message via CDO failed."
                WScript.Quit
            End If
        End If
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2011-08-14
      • 2019-06-16
      • 2012-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-27
      • 1970-01-01
      相关资源
      最近更新 更多