简体   繁体   English

Powershell中的COM(MS Office)事件

[英]COM (MS Office) eventing in Powershell

This question is strongly related to my prior one here: Handling COM event cancelation in VBScript 这个问题与我的上一个问题密切相关: 在VBScript中处理COM事件取消

I would like to write a script, that sends an E-Mail via our companys SMTP-Server using CDO. 我想编写一个脚本,该脚本使用CDO通过我们公司的SMTP服务器发送电子邮件。

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

So now I try to use the regular Outlook-Mail mask to prepare the mail first and then catch the send-item event via Powershell to give it's content to my CDO script. 因此,现在我尝试使用常规的Outlook-Mail掩码先准备邮件,然后通过Powershell捕获send-item事件,以将其内容提供给我的CDO脚本。 I tried it via VBScript before (see link above) but that doesn't seem to work as intended. 我之前通过VBScript尝试过(请参阅上面的链接),但这似乎无法按预期工作。

I'm rather new to powershell-scripting but in the past I already got things to work that wouldn't work in VBScript so I hope to get my problem solved with it again. 我对Powershell脚本还不是很陌生,但是过去我已经可以使用VBScript中无法使用的功能,因此希望再次解决它。 Right now, my script looks like this: 现在,我的脚本如下所示:

$WSH = new-Object -ComObject "WScript.Shell"
$Outlook = new-Object -ComObject "Outlook.Application"
$olMailItem = 0

register-objectevent -InputObject $Outlook -EventName "ItemSend"

$MailObject = $Outlook.CreateItem($olMailItem)
$MailObject.Display()

function Outlook_ItemSend([object] $Item, [ref] $Cancel)
{
    $Cancel = $TRUE
    $WSH.PopUp("Event worked")
    return $Null
}

I didn't come as far as I was hoping yet, because I can't figure out how to fire the event at all. 我没有达到我希望的程度,因为我根本不知道如何触发该事件。 You can find plenty of examples for register-objectevent online, but everytime it appears to be very very specific for some special COM object. 您可以在线找到大量关于register-objectevent的示例,但是每次它对于某些特殊的COM对象来说似乎都是非常特定的。 I couldn't find any example to register an MS-Office COM event that requires parameters so far... 到目前为止,我找不到任何示例来注册需要参数的MS-Office COM事件...

代替使用事件,您可以模态显示消息-调用MailItem.Display并将true作为参数传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM